简体   繁体   English

setMovementMethod(滚动)会干扰textView中的scaleGestureDetector(缩放)

[英]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());" 我有一个可以缩放的"textview.setMovementMethod(new ScrollingMovementMethod());" ,当我添加"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 我从这里得到代码: 捏缩放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: 我只是解决了这个问题(使用相同的示例),在我的代码中只添加了一个“ if”:

    @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;
    }

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

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