Pinch Zooming on textview android

后端 未结 3 828
借酒劲吻你
借酒劲吻你 2020-12-24 10:25

Hello friend I have done pinch zoom textview and its work fine but the problem is when I zoom out the textview position changed and i want the position of textview is fixed

3条回答
  •  悲哀的现实
    2020-12-24 10:54

    I have done this way:

    activity_main.xml:

    
    
    
        
    
    
    

    MainActivity.java:

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.TextView;
    
    public class MyTextViewPinchZoomClass extends Activity implements OnTouchListener {
    
        final static float STEP = 200;
        TextView mytv;
        float mRatio = 1.0f;
        int mBaseDist;
        float mBaseRatio;
        float fontsize = 13;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mytv = (TextView) findViewById(R.id.mytv);
            mytv.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));
                    mytv.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) {
            return false;
        }
    }
    

    Done

提交回复
热议问题