绘制的三角形路径留下痕迹

时间:2015-10-01 23:56:06

标签: android

class Triangle

public class Triangle extends View {
    private int x, y;
    private int _xDelta, _yDelta;
    private Point triangleA = new Point();
    private Point triangleB = new Point();
    private Point triangleC = new Point();
    public static Path trianglePath = new Path();
    private Paint paintTriangle = new Paint();

    public Triangle(Context context) {
        super(context);
        init(null, 0);
    }

    public Triangle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs, 0);
    }

    public Triangle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttyr) {
        paintTriangle.setColor(Color.RED);
        paintTriangle.setStyle(Style.FILL);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            _xDelta = X - x;
            _yDelta = Y - y;
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            x = X - _xDelta;
            y = Y - _yDelta;
            break;
        }
        invalidate();
        return true;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        triangleA.set(x - 100 - 10, y + 100 + 10);
        triangleB.set(x, y - 100 - 10);
        triangleC.set(x + 100 + 10, y + 100 + 10);
        trianglePath.moveTo(triangleA.x, triangleA.y);
        trianglePath.lineTo(triangleB.x, triangleB.y);
        trianglePath.lineTo(triangleC.x, triangleC.y);
        trianglePath.lineTo(triangleA.x, triangleA.y);
        trianglePath.close();
        canvas.drawPath(trianglePath, paintTriangle);

        invalidate();
    }

}

ActivityMain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.squaremovement.MainActivity" >

<com.example.squaremovement.Triangle
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

三角形在移动时留下痕迹,因此如何移除它并仅离开新位置。还是有其他解决方案吗? 这只是我主要应用程序的一个测试,我必须看看这条路径是否包含另一条路径。

2 个答案:

答案 0 :(得分:0)

在绘制三角形之前尝试清除画布:

canvas.drawColor(0, Mode.CLEAR);

答案 1 :(得分:0)

我明白这一点,但我不能像那样使用它,因为我需要在另一个视图中使用路径来查看它是否包含使用边界或类似的其他路径

我刚刚像这样创建了ondraw

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    triangleA.set(x - 100 - 10, y + 100 + 10);
    triangleB.set(x, y - 100 - 10);
    triangleC.set(x + 100 + 10, y + 100 + 10);
    Path trianglePath = new Path();

    trianglePath.moveTo(triangleA.x, triangleA.y);
    trianglePath.lineTo(triangleB.x, triangleB.y);
    trianglePath.lineTo(triangleC.x, triangleC.y);
    trianglePath.lineTo(triangleA.x, triangleA.y);
    trianglePath.close();

    canvas.drawPath(trianglePath, paintTriangle);

}