如何用Android画布绘制一个平滑地跟随手指的形状

时间:2014-05-02 19:32:20

标签: android canvas custom-view touch-event

我试图在屏幕上画出一个跟在我手指上的圆圈 它已经完成但是如果我快速移动手指,圆圈移动并不平滑 customview.java

public customview(Context context, AttributeSet attrs) {
    super(context, attrs);
        this.context = context;    
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setARGB(255, 255, 255, 255);
}

Context context;
public static float x=100, y=100;
private Paint paint;
public static boolean drew = false;
RadialGradient radialGradient;

@Override
protected void onDraw(Canvas canvas) {
    if(drew){
        int ringColors[] = { 0xFF15afda, 0x0015afda };
        radialGradient = new RadialGradient(x, y,100, ringColors, null,
                TileMode.CLAMP);

        paint.setShader(radialGradient);
        canvas.drawCircle(x, y, 200, paint);
    }
}

public static void setDrew(boolean drew){
    customview.drew = drew;
}

public static void setXT(float x){
    customview.x = x;
}

public static void setYT(float y){
    customview.y = y;
}

触摸事件(在触摸事件中,我更改显示手指位置的x和y,并设置无效,直到视图在新位置重绘圆圈)

@Override
        public boolean onTouch(View v, MotionEvent event) {
            dispatchGenericMotionEvent(event);
            // TODO Auto-generated method stub
            MotionEvent ev = event;
            switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                customview.setXT(ev.getRawX());
                customview.setYT(ev.getRawY());
                customview.setDrew(true);
                (findViewById(R.id.touch)).invalidate();

                break;
            case MotionEvent.ACTION_UP:
                customview.setDrew(false);
                (findViewById(R.id.customview)).invalidate();
                break;
            default:
                break;
            }
            return true;
        }

0 个答案:

没有答案