3.20MayBeAndroid

invalidate里放着ONDRAW的方法,当调用I时,会自动更新图片

如果上次记录的位置在-1,那么就更新为现在的位置,更新后就不再为-1了,就不再变了

也就是说是保留第一次触碰到的位置

public class CustomView extends View {
    private int mLastX, mLastY;
    private boolean isDragging = false;

    public CustomView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int currX = (int) event.getX();
        int currY = (int) event.getY();

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                if (event.getPointerCount() == 1) {
                    isDragging = true;
                }
                mLastX = currX;
                mLastY = currY;
                break;

            case MotionEvent.ACTION_MOVE:
                if (isDragging) {
                    int dx = currX - mLastX;
                    int dy = currY - mLastY;

                    // 拖拽
                    int left = getLeft() + dx;
                    int top = getTop() + dy;
                    int right = getRight() + dx;
                    int bottom = getBottom() + dy;
                    layout(left, top, right, bottom);
                } else {
                    int dx = currX - mLastX;
                    int dy = currY - mLastY;

                    // 拉伸
                    ViewGroup.LayoutParams layoutParams = getLayoutParams();
                    layoutParams.width += dx;
                    layoutParams.height += dy;
                    setLayoutParams(layoutParams);
                }

                mLastX = currX;
                mLastY = currY;
                break;

            case MotionEvent.ACTION_UP:
                isDragging = false;
                break;
        }

        return true;
    }
}

public class CustomView extends View {
    private int mLastX, mLastY;
    private boolean isDragging = false;
    private boolean isScaling = false;
    private float initialDistance;

    public CustomView(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        int currX = (int) event.getX();
        int currY = (int) event.getY();

        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                if (event.getPointerCount() == 1) {
                    isDragging = true;
                }
                mLastX = currX;
                mLastY = currY;
                break;

            case MotionEvent.ACTION_POINTER_DOWN:
                if (event.getPointerCount() == 2) {
                    isDragging = false;
                    isScaling = true;
                    initialDistance = getDistance(event);
                }
                break;

            case MotionEvent.ACTION_MOVE:
                if (isDragging) {
                    int dx = currX - mLastX;
                    int dy = currY - mLastY;

                    // 拖拽
                    int left = getLeft() + dx;
                    int top = getTop() + dy;
                    int right = getRight() + dx;
                    int bottom = getBottom() + dy;
                    layout(left, top, right, bottom);
                } else if (isScaling) {
                    float currentDistance = getDistance(event);
                    float scale = currentDistance / initialDistance;

                    // 拉伸
                    ViewGroup.LayoutParams layoutParams = getLayoutParams();
                    layoutParams.width *= scale;
                    layoutParams.height *= scale;
                    setLayoutParams(layoutParams);
                }

                break;

            case MotionEvent.ACTION_POINTER_UP:
                if (event.getPointerCount() == 2) {
                    isScaling = false;
                }
                break;

            case MotionEvent.ACTION_UP:
                isDragging = false;
                isScaling = false;
                break;
        }

        return true;
    }

    private float getDistance(MotionEvent event) {
        float dx = event.getX(0) - event.getX(1);
        float dy = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(dx * dx + dy * dy);
    }
}
相关推荐
Hello.Reader2 分钟前
全面解析 Golang Gin 框架
开发语言·golang·gin
禁默13 分钟前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood20 分钟前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑22 分钟前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb421528725 分钟前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶26 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
qq_4336184428 分钟前
shell 编程(二)
开发语言·bash·shell
zfoo-framework34 分钟前
【jenkins插件】
java
风_流沙39 分钟前
java 对ElasticSearch数据库操作封装工具类(对你是否适用嘞)
java·数据库·elasticsearch