简体   繁体   中英

setMovementMethod (Scrolling) interferes with scaleGestureDetector (zooming) in a textView

thank you so much for reading. I have a textView that you can pinch zoom, and when I add "textview.setMovementMethod(new ScrollingMovementMethod());" to my code I can also scroll the zoomed textview which is exactly what I need. The problem is as soon as the text is zoomed, only the scrolling works but the pinch zoom doesnt work anymore, (unless I place one of my fingers outside the textview and the other inside). Any help will be really appreciated. I got the code from here: Pinch Zooming TextView

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.TextView;

public class MainActivity extends Activity {

    TextView scaleGesture;
    ScaleGestureDetector scaleGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scaleGesture = (TextView)findViewById(R.id.article);
        scaleGesture.setText("this is some text");
        scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        scaleGestureDetector.onTouchEvent(event);
        return true;
    }

    public class simpleOnScaleGestureListener extends
            SimpleOnScaleGestureListener {

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            // TODO Auto-generated method stub
            float size = scaleGesture.getTextSize();
            Log.d("TextSizeStart", String.valueOf(size));

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


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

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

I just resolve this problem (with the same example) only add one "if" in my code:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // get how many fingers is in touch (in that case, if are more
        // than 1, the user don't want do the scroll
        if (event.getPointerCount() > 1) {
           scaleGestureDetector.onTouchEvent(event);
           return true;
        }
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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