我使用自定义视图创建了一个画布,它运行正常。我可以用鼠标在上面画画。但是我想用按钮点击清除在画布上绘制的画。我尽我所能但却无法做到这一点。请帮忙
这是我的主要活动
package com.example.paint;
public class MainActivity extends Activity
{
LinearLayout canvasAlphabets;
SingleTouchEventView myView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnClear = (Button)findViewById(R.id.btnSearchNameWise);
myView = new SingleTouchEventView(this, null);
canvasAlphabets = (LinearLayout)findViewById(R.id.First);
canvasAlphabets.addView(myView);
btnClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
myView.clearCanvas();
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
});
}
}
这是我的SingleTouchEventView CLASS
public class SingleTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public boolean cc = false;
public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
protected void onDraw(Canvas canvas)
{
if(cc)
{
path = new Path();
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(0, 0, 0, 0, clearPaint);
cc = false;
}
else
canvas.drawPath(path, paint);
}
public void clearCanvas()
{
Toast.makeText(getContext(), "hello in canvas", Toast.LENGTH_LONG).show();
cc =true;
invalidate();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
}
这是我的布局文件
<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" >
<LinearLayout
android:id="@+id/First"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dip"
android:paddingTop="10dip"
android:orientation="vertical"
android:weightSum="1"
android:background="#aabbcc">
<com.example.paint.SingleTouchEventView
android:id="@+id/cusView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/Second"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:paddingBottom="50dip"
android:weightSum="1"
android:background="#ffaacc" >
<Button
android:id="@+id/btnSearchNameWise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="3dip"
android:paddingRight="3dip"
android:text="Search Name Wise" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
试试这段代码
Paint clear = new Paint();
clear.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(0, 0, 0, 0, clear);