简体   繁体   English

如何在滚动视图中使用捏合缩放?

[英]How to use pinch zoom with scrollview?

I am writing a code for pinch zoom for textview. Since I am using soo many textviews.我正在为 textview 编写捏缩放代码。因为我使用了太多的文本视图。 I have to use scrollview.我必须使用滚动视图。 If I use scrollview pinch zoom is not working.如果我使用 scrollview 捏缩放不起作用。 Otherwise my code is working fine without scrollview.否则我的代码在没有滚动视图的情况下工作正常。 Is there any method to use pinch zoom along with scrollview是否有任何方法可以将捏缩放与滚动视图一起使用

You can create an AlertDialog, with a custom view (which can have whatever you want in it).您可以创建一个带有自定义视图的 AlertDialog(其中可以包含您想要的任何内容)。

Here is one (of many) links on how to do this: How to implement a custom AlertDialog View这是有关如何执行此操作的(许多)链接之一: How to implement a custom AlertDialog View

To have a ScrollView with pinch to zoom TextView, you have to capture the touch events on the ScrollView and assign the pinch zoom if 2 fingers are detected like so.要使 ScrollView 具有缩放功能 TextView,您必须捕获 ScrollView 上的触摸事件并在检测到 2 个手指时分配缩放缩放。

Java class Java class

import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.widget.NestedScrollView;

public class MyExampleClass extends AppCompatActivity {

    private NestedScrollView myScrollView;
    private TextView myTextView;
    private ScaleGestureDetector scaleGestureDetector;
    private ArrayList<TextView> textViews = new ArrayList<TextView>();

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

        /* instantiation */

        myTextView = findViewById(R.id.ourTextView1);
        myScrollView = findViewById(R.id.ourScrollview);
        scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());

        /* set a custom onTouchListener to the scrollview */

        myScrollView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                /* if 2 fingers are touching the screen, start text resizing */

                if (event.getPointerCount() > 1) {
                    myScrollView.requestDisallowInterceptTouchEvent(true);
                    scaleGestureDetector.onTouchEvent(event);
                    return true;
                }

                /* otherwise pass the touch event to the scrollView for scrolling */

                return false;
            }
        });

    /* add all out TextViews to an ArrayList */

    textViews.add(findViewById(R.id.ourTextView1));
    textViews.add(findViewById(R.id.ourTextView2));
    }

    /* inner class for handling pinch zoom */

    public class simpleOnScaleGestureListener extends
            ScaleGestureDetector.SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {

            /* get current text size */

            float size = myTextView.getTextSize();
            Log.d("TextSizeStart", String.valueOf(size));

            /* get how much user pinched */

            float factor = detector.getScaleFactor();
            Log.d("Factor", String.valueOf(factor));

            /* calculate how much to resize to */

            float product = size * factor;
            Log.d("TextSize", String.valueOf(product));

            /* set max size. if product equals more than 140, set product to 140 */

            if (product > 140) {
                product = 140;
            }

            /* set min size. if product equals less than 60, set product to 60 */

            if (product < 60) {
                product = 60;
            }

            /* set textSize in all textViews in arraylist to new size */

            for (TextView v: textViews) {
                v.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
            }

            /* set size to new textSize */

            size = myTextView.getTextSize();
            Log.d("TextSizeEnd", String.valueOf(size));
            return true;
        }
    }
}

xml file xml 档案

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ourScrollview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MyExampleClass">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <TextView
        android:id="@+id/ourTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="11dp"
        android:textSize="30sp"
        android:text="@string/long_text" />

    <TextView
        android:id="@+id/ourTextView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="11dp"
        android:textSize="30sp"
        android:text="@string/long_text" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM