gpt4 book ai didi

android - 如何在单击按钮时清除android中的 Canvas

转载 作者:太空宇宙 更新时间:2023-11-03 12:20:38 24 4
gpt4 key购买 nike

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

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(18f);
paint.setColor(Color.LTGRAY);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
if(cc)
{
Bitmap back = BitmapFactory.decodeResource(getResources(), R.drawable.black_square);
Bitmap cb = Bitmap.createScaledBitmap(back, 0, 0, false);
canvas.drawBitmap(cb,0,0,null);
cc = false;

}
canvas.drawPath(path, paint);
}

public void clearCanvas()
{
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;
}
}

上面的文件是我的SingleTouchEventView.Java这是我的 MainActivity.java

public class MainActivity extends Activity {

Button reset;;
LinearLayout canvasAlphabets;
SingleTouchEventView myView;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);


reset = (Button)findViewById(R.id.reset_canvas);

myView = new SingleTouchEventView(this, null);
canvasAlphabets = (LinearLayout)findViewById(R.id.canvas_Alphabets);
canvasAlphabets.addView(myView);


reset.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
// TODO Auto-generated method stub


}
});

}

}

我的问题是我应该在重置按钮中使用什么代码来删除 Canvas 的所有内容。请帮我我已经尝试实现 myView.clearCanvas() 但这没有帮助。如果我添加此代码以重置单击时的按钮,它会导致 FC

谢谢

最佳答案

 path = new Path(); 
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawRect(0, 0, 0, 0, clearPaint);
cc = false;

我用上面的代码修复了它

关于android - 如何在单击按钮时清除android中的 Canvas ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17084570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com