码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-03-16 12:30:47      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

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

 

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

标签:

原文地址:http://www.cnblogs.com/Anita9002/p/4341307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!