Java 删除在其上绘制的视图的内容
我有一个可以“用手指画”的视图。我正试图创建一个按钮,以清除其内容没有运气Java 删除在其上绘制的视图的内容,java,android,android-studio,Java,Android,Android Studio,我有一个可以“用手指画”的视图。我正试图创建一个按钮,以清除其内容没有运气 class PaintView extends View { public ArrayList<Point> listOfColors = new ArrayList<Point>(); public static int BRUSH_SIZE = 60; public static final int DEFAULT_COLOR = Color.RED; publ
class PaintView extends View {
public ArrayList<Point> listOfColors = new ArrayList<Point>();
public static int BRUSH_SIZE = 60;
public static final int DEFAULT_COLOR = Color.RED;
public static final int DEFAULT_BG_COLOR = Color.WHITE;
private static final float TOUCH_TOLERANCE = 4;
private float mX, mY;
private Path mPath;
private Paint mPaint;
private ArrayList<FingerPath> paths = new ArrayList<>();
private int currentColor;
private int backgroundColor = DEFAULT_BG_COLOR;
private int strokeWidth;
private boolean emboss;
private boolean blur;
private boolean doClear = false;
private MaskFilter mEmboss;
private MaskFilter mBlur;
private Bitmap mBitmap;
private Canvas mCanvas;
private Paint mBitmapPaint = new Paint(Paint.DITHER_FLAG);
ArrayList<Point> listOfPoints = new ArrayList<Point>();
public Bitmap passTheCache;
public int xwidth, xheight;
public PaintView(Context context) {
this(context, null);
}
public PaintView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(DEFAULT_COLOR);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setXfermode(null);
mPaint.setAlpha(0xff);
mEmboss = new EmbossMaskFilter(new float[]{1, 1, 1}, 0.4f, 6, 3.5f);
mBlur = new BlurMaskFilter(5, BlurMaskFilter.Blur.NORMAL);
}
public void setColor(int color) {
currentColor = color;
}
public void init(DisplayMetrics metrics) {
int height = metrics.heightPixels;
int width = metrics.widthPixels;
mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
passTheCache = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
xwidth = width;
xheight = height;
backgroundColor = Color.BLACK;
currentColor = DEFAULT_COLOR;
strokeWidth = BRUSH_SIZE;
}
public void normal() {
emboss = false;
blur = false;
}
public void emboss() {
emboss = true;
blur = false;
}
public void blur() {
emboss = false;
blur = true;
}
public void clear() {
backgroundColor = DEFAULT_BG_COLOR;
doClear = true;
paths.clear();
normal();
invalidate();
Log.d("FILEZ", "cleared...");
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
mCanvas.drawColor(0x00AAAAAA);
mCanvas.drawColor(0x00AAAAAA);
for (FingerPath fp : paths) {
mPaint.setColor(fp.color);
mPaint.setStrokeWidth(fp.strokeWidth);
mPaint.setMaskFilter(null);
if (fp.emboss)
mPaint.setMaskFilter(mEmboss);
else if (fp.blur)
mPaint.setMaskFilter(mBlur);
mCanvas.drawPath(fp.path, mPaint);
}
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.restore();
}
private void touchStart(float x, float y) {
mPath = new Path();
FingerPath fp = new FingerPath(currentColor, emboss, blur, strokeWidth, mPath);
paths.add(fp);
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touchMove(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touchUp() {
mPath.lineTo(mX, mY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
Point point = new Point((int) x, (int) y);
listOfColors.add(point);
Log.d("OK", "wtf?!");
Log.d("z", "OK:" + point);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touchStart(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touchMove(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touchUp();
invalidate();
break;
}
return true;
}
}
类PaintView扩展了视图{
public ArrayList listOfColors=new ArrayList();
公共静态int笔刷大小=60;
公共静态最终int默认值_COLOR=COLOR.RED;
公共静态最终整数默认值\u BG\u COLOR=COLOR.WHITE;
专用静态最终浮动接触公差=4;
私人浮动mX,我的;
专用路径mPath;
私人油漆;
私有ArrayList路径=新ArrayList();
私人色彩;
private int backgroundColor=默认背景颜色;
私有内冲程宽度;
私有布尔浮雕;
私有布尔模糊;
私有布尔doClear=false;
私人MaskFilter mEmboss;
私人MaskFilter mBlur;
私有位图mBitmap;
私人帆布mCanvas;
专用油漆mBitmapPaint=新油漆(油漆抖动标志);
ArrayList listOfPoints=新的ArrayList();
公共位图密码;
公共整数xwidth,xheight;
公共PaintView(上下文){
这个(上下文,空);
}
公共画图视图(上下文、属性集属性){
超级(上下文,attrs);
mPaint=新油漆();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(默认颜色);
mPaint.setStyle(油漆、样式、笔划);
mPaint.setStrokeJoin(油漆.连接.圆形);
mPaint.setStrokeCap(油漆盖圆形);
mPaint.setXfermode(null);
mPaint.setAlpha(0xff);
mEmboss=新的浮雕maskfilter(新的float[]{1,1,1},0.4f,6,3.5f);
mBlur=新的模糊过滤器(5,BlurMaskFilter.Blur.NORMAL);
}
公共void setColor(int-color){
当前颜色=颜色;
}
公共void init(DisplayMetrics){
int height=metrics.heightPixels;
int width=metrics.widthPixels;
mBitmap=Bitmap.createBitmap(宽度、高度、Bitmap.Config.ARGB_8888);
passTheCache=Bitmap.createBitmap(宽度、高度、Bitmap.Config.ARGB_8888);
mCanvas=新画布(mBitmap);
xwidth=宽度;
xheight=高度;
backgroundColor=Color.BLACK;
currentColor=默认颜色;
笔划宽度=画笔大小;
}
公共无效正常值(){
浮雕=假;
模糊=错误;
}
公共虚空浮雕(){
浮雕=真;
模糊=错误;
}
公共空间模糊(){
浮雕=假;
模糊=真;
}
公共空间清除(){
backgroundColor=默认的背景颜色;
doClear=true;
清除路径();
正常();
使无效();
Log.d(“FILEZ”,“清除…”);
}
@凌驾
受保护的void onDraw(画布){
canvas.save();
mCanvas.drawColor(0x00aaaa);
mCanvas.drawColor(0x00aaaa);
对于(FingerPath fp:路径){
mPaint.setColor(fp.color);
mPaint.设置行程宽度(fp.行程宽度);
mPaint.setMaskFilter(null);
if(fp.浮雕)
mPaint.setMaskFilter(mEmboss);
else if(fp.blur)
mPaint.setMaskFilter(mBlur);
mCanvas.drawPath(fp.path,mPaint);
}
drawBitmap(mBitmap,0,0,mbitMapPoint);
canvas.restore();
}
专用void touchStart(浮点x、浮点y){
mPath=新路径();
FingerPath fp=新的FingerPath(当前颜色、浮雕、模糊、笔划宽度、mPath);
添加路径(fp);
mPath.reset();
移动到(x,y)的速度;
mX=x;
mY=y;
}
私有无效触摸移动(浮动x、浮动y){
float dx=Math.abs(x-mX);
float dy=Math.abs(y-mY);
如果(dx>=接触公差| | dy>=接触公差){
兆帕四分之一秒(mX,mY,(x+mX)/2,(y+mY)/2);
mX=x;
mY=y;
}
}
私人修补{
mPath.lineTo(mX,mY);
}
@凌驾
公共布尔onTouchEvent(运动事件){
float x=event.getX();
float y=event.getY();
点=新点((int)x,(int)y);
颜色列表。添加(点);
Log.d(“OK”,“wtf?!”);
对数d(“z”,“OK:+点”);
开关(event.getAction()){
case MotionEvent.ACTION\u DOWN:
touchStart(x,y);
使无效();
打破
case MotionEvent.ACTION\u移动:
touchMove(x,y);
使无效();
打破
case MotionEvent.ACTION\u UP:
修补();
使无效();
打破
}
返回true;
}
}
我的想法是创建一个clear()函数,在该函数中,我只需删除所有的“点”,然后使其无效以重新绘制画布,但它不起作用。感谢您的帮助。我尝试过几种方法,比如创建一个与视图大小相同的黑色矩形,但是当我在它上面重新绘制时,旧画就会出现 您甚至必须清除“mBitmap”,因为“canvas.drawBitmap()”始终绘制最新准备的位图