xml 레이아웃을 기반으로하는 사용자 정의보기를 만들었습니다. 보기를 지울 수있는 단추를 추가했습니다. 이제 클릭 할 때 캔버스 영역을 비우고 싶습니다. xml 레이아웃 file.Now에서 onClick 이벤트를 추가하여 전체보기/캔버스를 지우는 코드를 어디에 추가합니까? 몇 가지 코드를 추가했습니다. (이것은 아무것도 지우지 않습니다). 내 활동,보기 및 레이아웃 파일을 아래 순서대로 추가했습니다.캔버스 영역 지우기
public class CustomViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void clearLine(View v) {
new CustomView(CustomViewActivity.this, null).clearCanvas();
}
}
public class CustomView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public Boolean clearCanvas = false;
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomView(Context context,AttributeSet attrs) {
super(context,attrs);
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setTextSize(20);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeWidth(5f);
}
protected void onDraw(Canvas canvas) {
if(clearCanvas)
{ // Choose the colour you want to clear with.
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
//canvas.drawColor(0, Mode.CLEAR);
clearCanvas = false;
}
super.onDraw(canvas);
canvas.drawText("Hello World", 5, 30, paint);
canvas.drawPath(path, paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//int action = event.getAction() & MotionEvent.ACTION_MASK;
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;
}
public void clearCanvas(){
clearCanvas = true;
postInvalidate();
//canvas.drawColor(0, Mode.CLEAR);
}
}
<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" >
<com.example.CustomViewEvent.CustomView
android:id="@+id/customView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:onClick="clearLine"
android:text="CLEAR" />
</RelativeLayout>
drawColor가 나에게 좋은 출발 인 것 같습니다. – njzk2