Android中自定义View支持缩放

参考网址:https://blog.csdn.net/zqurapig/article/details/83414389

https://blog.csdn.net/u010648159/article/details/80854797

public class ScaleGestureDemoView extends View {
    private static final String TAG = "ScaleGestureDemoView";
 
    private ScaleGestureDetector mScaleGestureDetector;
 
    public ScaleGestureDemoView(Context context) {
        super(context);
    }
 
    public ScaleGestureDemoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initScaleGestureDetector();
    }
 
    private void initScaleGestureDetector() {
        mScaleGestureDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScaleBegin(ScaleGestureDetector detector) {
                return true;
            }
 
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                Log.i(TAG, "focusX = " + detector.getFocusX());       // 缩放中心,x坐标
                Log.i(TAG, "focusY = " + detector.getFocusY());       // 缩放中心y坐标
                Log.i(TAG, "scale = " + detector.getScaleFactor());   // 缩放因子
                return true;
            }
 
            @Override
            public void onScaleEnd(ScaleGestureDetector detector) {
            }
        });
    }
 
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mScaleGestureDetector.onTouchEvent(event);
        return true;
    }
}

 

你可能感兴趣的:(Android,Studio)