【问题标题】:How to make zoom in / out scrollview in android?如何在android中放大/缩小滚动视图?
【发布时间】:2016-05-11 12:28:54
【问题描述】:

在我的 android 应用程序中,我需要在我的应用程序中创建活动 zoomable.nut,其中几个活动以 scrollview 开头,并且此代码无法识别 scrollview。如何为可滚动活动进行捏缩放?这是我的布局之一。

还尝试了 ScaleGestureDetector 和 How to make zoomable scrollview?

想要放大/缩小滚动视图。感谢您的帮助,我们将不胜感激

提前致谢

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/horizontalScrollView"
android:layout_gravity="center"
        android:fillViewport="false">



    <LinearLayout
        android:id="@+id/wd_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <!-- Start Circle -->

        <TableRow
            android:id="@+id/row_circl1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginTop="30dp"
            android:background="@mipmap/ic_launcher" >

            <RelativeLayout
                android:id="@+id/circle_layout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@mipmap/ic_launcher" >

            </RelativeLayout>
        </TableRow>

        <ImageView
            android:id="@+id/img_engin_circle1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@mipmap/ic_launcher"
            android:contentDescription="TODO"
            android:layout_gravity="center_horizontal" />

        <TableRow
            android:id="@+id/row_name_circle"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:layout_marginTop="30dp" >

            <RelativeLayout
                android:id="@+id/circle_name_layout1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="-15dp"
                android:background="@mipmap/ic_launcher" >

                <ImageView
                    android:id="@+id/img_name_circle1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:background="@mipmap/ic_launcher"
                    android:contentDescription="TODO" />

                <ImageView
                    android:id="@+id/img_engin_circle2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"
                    android:contentDescription="TODO"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentStart="true" />
            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/circle_name_layout2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="-15dp"
                android:background="@mipmap/ic_launcher" >

                <ImageView
                    android:id="@+id/img_name_circle2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:background="@mipmap/ic_launcher"
                    android:contentDescription="TODO" />


            </RelativeLayout>
        </TableRow>

        <RelativeLayout
            android:id="@+id/circle_layout1"
            android:layout_width="match_parent"
            android:layout_height="172dp"
            android:background="@mipmap/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_gravity="right">

            <RelativeLayout
                android:id="@+id/relativeLayout"
                android:layout_width="match_parent"
                android:layout_height="172dp"
                android:background="@mipmap/ic_launcher"
                android:layout_gravity="right"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/circle_layout1"
            android:layout_width="match_parent"
            android:layout_height="172dp"
            android:background="@mipmap/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_gravity="right">

            <RelativeLayout
                android:id="@+id/relativeLaysdfout"
                android:layout_width="match_parent"
                android:layout_height="172dp"
                android:background="@mipmap/ic_launcher"
                android:layout_gravity="right"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/circle_laysdfout1"
            android:layout_width="match_parent"
            android:layout_height="172dp"
            android:background="@mipmap/ic_launcher"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_gravity="right">

            <RelativeLayout
                android:id="@+id/dsf"
                android:layout_width="match_parent"
                android:layout_height="172dp"
                android:background="@mipmap/ic_launcher"
                android:layout_gravity="right"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true" />
        </RelativeLayout>

        <!-- End Circle -->

    </LinearLayout>



</ScrollView>

这是我的活动

放大效果很好,但我不能像我们的照片库图像视图那样从左到右移动或反之亦然

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        scrollView = (ScrollView) findViewById(R.id.horizontalScrollView);
        horizontalScrollView = (HorizontalScrollView) findViewById(R.id.vertical); 

        gestureDetector = new GestureDetector(this, new GestureListener());    

        scaleGestureDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                float scale = 1 - detector.getScaleFactor();

                float prevScale = mScale;
                mScale += scale;

                if (mScale < 0.1f) // Minimum scale condition:
                    mScale = 0.1f;

                if (mScale > 10f) // Maximum scale condition:
                    mScale = 10f;
                ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
                scaleAnimation.setDuration(0);
                scaleAnimation.setFillAfter(true);

                scrollView.startAnimation(scaleAnimation);
                horizontalScrollView.startAnimation(scaleAnimation);
                return true;
            }
        });
    }

 override dispatchTouchEvent()
        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
            super.dispatchTouchEvent(event);
            scaleGestureDetector.onTouchEvent(event);
            gestureDetector.onTouchEvent(event);
            return gestureDetector.onTouchEvent(event);
        }  


        private class GestureListener extends GestureDetector.SimpleOnGestureListener {
            @Override
            public boolean onDown(MotionEvent e) {
                return true;
            }
            // event when double tap occurs
            @Override
            public boolean onDoubleTap(MotionEvent e) {
                // double tap fired.
                return true;
            }
        }
}

【问题讨论】:

  • 你好,你找到解决办法了吗?
  • 不,如果我愿意,仍然找不到合适的解决方案,我会分享@isha
  • 感谢@mehul 的回复..我需要相同的解决方案
  • @isha 嘿发现这个解决方案可以帮助你eminosoft.blogspot.in/2013/06/…
  • 谢谢@mehul .. 我会去看看

标签: android android-studio scroll scrollview pinchzoom


【解决方案1】:

在 Dispatch 触摸事件中使用 ActionType 并分离每个 MotionEventActions 并为需要执行特定操作的每种类型编写逻辑。

How to make a Scrollable Layout and Zoomable Layout in an Android App?

这可能对你有很大帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多