Android Pinch缩放Textview在滚动视图中不起作用

时间:2015-09-28 03:51:13

标签: android scrollview pinchzoom

我有下面的代码,它是放大和缩小文本视图的字体大小。但是只有当我有文本视图时它才会工作。如果我在我的布局中添加滚动视图,那么它就不会工作。如果有人知道怎么做解决。请发表你的答案

  public class Introduce extends Activity implements OnTouchListener{

     final static float STEP = 200;
     TextView mtxtRatio1,mtxtRatio2,mtxtRatio3,mtxtRatio4;
     float mRatio = 1.0f;
     int mBaseDist;
     float mBaseRatio;
     float fontsize = 13;

     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.introduce);

      mtxtRatio1 = (TextView)findViewById(R.id.intro1);
      mtxtRatio1.setTextSize(mRatio+13);  
     }

     public boolean onTouchEvent(MotionEvent event) {
      if (event.getPointerCount() == 2) {
       int action = event.getAction();
       int pureaction = action & MotionEvent.ACTION_MASK;
       if (pureaction == MotionEvent.ACTION_POINTER_DOWN) {
        mBaseDist = getDistance(event);
        mBaseRatio = mRatio;
       } else {
        float delta = (getDistance(event) - mBaseDist) / STEP;
        float multi = (float)Math.pow(2, delta);
        mRatio = Math.min(1024.0f, Math.max(0.1f, mBaseRatio * multi));
        mtxtRatio1.setTextSize(mRatio+13);
       }
      }
      return true; 
     }

     int getDistance(MotionEvent event) {
      int dx = (int)(event.getX(0) - event.getX(1));
      int dy = (int)(event.getY(0) - event.getY(1));
      return (int)(Math.sqrt(dx * dx + dy * dy));
     }

     public boolean onTouch(View v, MotionEvent event) {
      // TODO Auto-generated method stub
      return false;
     }
    }

xml layout

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
<ScrollView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="10dp"
   >

<TextView
            android:id="@+id/intro1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/description"
            android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView>
    </LinearLayout>

0 个答案:

没有答案