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;
            }
        }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。