• Android----paint触摸轨迹监听


    paint触摸轨迹监听,主要是三种而已,ACTION_DOWN,ACTION_MOVE,ACTION_UP

    public boolean onTouchEvent(MotionEvent event){
       int action = event.getAction();
       float x = event.getX();
       float y = event.getY();
       switch(action){
       case MotionEvent.ACTION_DOWN:
        mPath.moveTo(x, y);
        break;
       case MotionEvent.ACTION_MOVE:
        mPath.quadTo(mPosX, mPosY, x, y);
        break;
       case MotionEvent.ACTION_UP:
        //mPath.reset();
        break;
       }
       //记录当前触摸点得当前得坐标
       mPosX = x;
       mPosY = y;
    return true;
    }

    但是触摸绘制轨迹有两种效果,一种是手指移动完毕,轨迹出现,那么设置在ACTION_UP中:在ACTION_UP中绘制cacheCanvas.drawPath(path, paint);

    public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            float x = event.getX();
            float y = event.getY();
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN : {
                    cur_x = x;
                    cur_y = y;
                    path.moveTo(cur_x, cur_y);
                    isMoving = true;
                    break;
                }
    
                case MotionEvent.ACTION_MOVE : {
                    if (!isMoving)
                        break;
    
                    // 二次曲线方式绘制
                    path.quadTo(cur_x, cur_y, x, y);
            //        cacheCanvas.drawPath(path, paint);
                    // 下面这个方法貌似跟上面一样
                    // path.lineTo(x, y);
                    cur_x = x;
                    cur_y = y;
                    break;
                }
    
                case MotionEvent.ACTION_UP : {
                    // 鼠标弹起保存最后状态
                    cacheCanvas.drawPath(path, paint);
                    path.reset();
                    isMoving = false;
                    break;
                }
            }

    第二种是触摸的时候显示绘制轨迹,那么就在ACTION_MOVE中设置:cacheCanvas.drawPath(path, paint);

    public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            float x = event.getX();
            float y = event.getY();
    
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN : {
                    cur_x = x;
                    cur_y = y;
                    path.moveTo(cur_x, cur_y);
                    isMoving = true;
                    break;
                }
    
                case MotionEvent.ACTION_MOVE : {
                    if (!isMoving)
                        break;
    
                    // 二次曲线方式绘制
                    path.quadTo(cur_x, cur_y, x, y);
                    cacheCanvas.drawPath(path, paint);
                    // 下面这个方法貌似跟上面一样
                    // path.lineTo(x, y);
                    cur_x = x;
                    cur_y = y;
                    break;
                }
    
                case MotionEvent.ACTION_UP : {
                    // 鼠标弹起保存最后状态
            //        cacheCanvas.drawPath(path, paint);
                    path.reset();
                    isMoving = false;
                    break;
                }
            }
  • 相关阅读:
    学习笔记2-查看目录文件
    学习笔记1-基本信息及相关目录
    【图论】二分图最大匹配
    【图论】Dinic算法
    【图论】最小割
    【数据结构】左偏树
    【数学】欧拉定理
    【数据结构】ST表
    【数学】博弈模型
    【字符串】后缀数组
  • 原文地址:https://www.cnblogs.com/Anita9002/p/4341307.html
Copyright © 2020-2023  润新知