使用笔触样式时,移动视图会留下痕迹

时间:2015-02-07 19:23:32

标签: android

我有一个EditView类,允许用户在编辑模式下在屏幕上移动视图。另外,我在视图后面绘制一个矩形,向用户显示视图区域。一切都按预期工作,但是将绘制样式设置为STROKE会在移动视图时导致轨迹。如果我将样式留给FILL,我就没有这个问题。对此有解释吗?

CustomView类:

public class CustomView extends EditView {

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // Draw other stuff here
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch(e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                if(!super.onTouchEvent(e))
                    break;
                // Code here
            case MotionEvent.ACTION_UP:
                if(!super.onTouchEvent(e))
                    break;
                // Code here
            case MotionEvent.ACTION_DOWN:
                if(!super.onTouchEvent(e))
                    break;
                // Code here
        }
        return true;
    }
}

EditView类:

public class EditView extends View {

    private Paint p = new Paint();

    public EditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        p.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(MainActivity.EDIT_MODE)
            canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), p);
    }

    public void setPaintColor(int color) {
        p.setColor(color);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        if(!MainActivity.EDIT_MODE)
            return true;
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
        switch(e.getAction()) {
            case MotionEvent.ACTION_MOVE:
                // Change layout params here
                return false;
            case MotionEvent.ACTION_UP:
                setPaintColor(Color.RED);
                return false;
            case MotionEvent.ACTION_DOWN:
                setPaintColor(Color.GREEN);
                return false;
            default: return true;
    }
}

1 个答案:

答案 0 :(得分:0)

我设法解决了问题:似乎你必须设置笔画宽度。

相关问题