public void Pattern(Canvas canvas) {
int x = 0;
int y = 0;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setAntiAlias(true);
paint.setColor(Color.MAGENTA);
paint.setTextSize(22);
canvas.drawText("movement pattern", 75, 55, paint);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
canvas.drawCircle(359, 300 , 4, paint);
canvas.drawCircle(425, 300, 4, paint);
// canvas.drawCircle(248, , 4, paint);
canvas.drawCircle(248, 380, 4, paint);
canvas.drawCircle(50, 300, 4, paint);
//canvas.drawCircle(5421, 6499, 4, paint);
// draw a thick dashed line,
paint.setColor(Color.BLUE);
canvas.drawLine(359, 300 , 425, 300, paint);
canvas.drawLine(425, 300, 248, 380, paint);
//canvas.drawLine(400, 500 ,248, 380, paint);
canvas.drawLine(248, 380 , 50, 300, paint);
canvas.drawLine(50, 300, 359, 300, paint);
}
活动主.xml:
<Button
android:id="@+id/BPV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/View"
android:layout_below="@+id/btnShowLocation"
android:layout_marginLeft="15dp"
android:onClick="Pattern"
android:text="Pattern" />
<View
android:id="@+id/PV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/BPV"
android:layout_below="@+id/TextView1" />
基本上,在主布局中,我有 4 个按钮并且一切正常。但除了BPV 按钮。单击BPV 按钮时,我想在PV处显示Pattern(Canvas canvas)。但是单击它时应用程序会强制关闭。