欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 监听缩放和滑动事件

程序员文章站 2022-04-12 22:29:25
做了个手势控制的需求,比较简单,记录下上代码~public class CustomControlView extends View { private ScaleGestureDetector mScaleGestureDetector = null; private GestureDetector mGestureDetector; private float currentScale = 1f; private boolean canScale = true;...
做了个手势控制的需求,比较简单,记录下
上代码~
public class CustomControlView extends View {
    private ScaleGestureDetector mScaleGestureDetector = null;
    private GestureDetector mGestureDetector;
    private float currentScale = 1f;
    private boolean canScale = true;
    OnControlListener onControlListener;

    public CustomControlView(Context context) {
        super(context);
        initView(context);
    }

    public CustomControlView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                if (onControlListener != null)
                    onControlListener.onScroll(distanceX, distanceY);
                return true;
            }
        }, null, true);

        mScaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                currentScale = currentScale * detector.getScaleFactor();
                if (onControlListener != null)
                    onControlListener.onScale(currentScale);
                return true;
            }
        });
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (canScale)
            mScaleGestureDetector.onTouchEvent(event);
        mGestureDetector.onTouchEvent(event);
        return true;
    }

    public void setOnControlListener(OnControlListener onControlListener) {
        this.onControlListener = onControlListener;
    }

    public void setCanScale(boolean canScale) {
        this.canScale = canScale;
    }

使用xml放自定义view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.test.scaleapplication.CustomControlView
        android:id="@+id/ccv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent" />
</RelativeLayout>

设置监听

 CustomControlView customControlView = findViewById(R.id.ccv);
        customControlView.setCanScale(true);
        customControlView.setOnControlListener(new OnControlListener() {
            @Override
            public void onScroll(float distanceX, float distanceY) {
                Log.e(TAG, "onScroll: " + distanceX + "---" + distanceY);
            }

            @Override
            public void onScale(float scale) {
                Log.e(TAG, "onScale: "+scale );
            }
        });

ok!收工~

本文地址:https://blog.csdn.net/weixin_35732062/article/details/108130927