setMovementMethod(Scrolling)干扰textView中的scaleGestureDetector(缩放)

时间:2013-11-22 07:35:53

标签: textview scroll zooming

非常感谢你阅读。我有一个textView,你可以捏缩放,当我向我的代码添加"textview.setMovementMethod(new ScrollingMovementMethod());"时,我也可以滚动缩放的textview,这正是我需要的。问题是文本一旦缩放,只有滚动工作,但捏缩放不再工作,(除非我把我的一个手指放在textview外面,另一个放在里面)。任何帮助将非常感激。我从这里得到了代码: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;
        }
    }
}`

1 个答案:

答案 0 :(得分:2)

我只是解决了这个问题(使用相同的例子)只添加一个"如果"在我的代码中:

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