清除画布Canvas.drawColor() [英] Clearing canvas with Canvas.drawColor()
问题描述
我试图更改自定义视图取得了一些成功的背景图像。图像将改变,但问题是,我仍然看到旧形象的痕迹。当我试图清除画布上绘制新的图像之前,它不会出现工作。我创建了一个位图来存储图像。改变图像的时候,我打电话Canvas.drawColor()绘制新的形象出现,但旧的图像仍然存在。我试过drawColor(0),drawColor(Color.BLACK),c.drawColor(0,PorterDuff.Mode.CLEAR),并没有上述作品。因此,我不得不发布此审查从有经验的头脑比我的。
实际code是如下:
私人诠释BGND;
私人布尔切换;
公共无效setBgnd(INT进入){
开关= TRUE;
开关(进入){
案例R.drawable.image1:
BGND =传入;
this.invalidate();
打破;
案例R.drawable.image2:
BGND =传入;
this.invalidate();
打破;
}
}
保护无效的OnDraw(帆布C){
如果(切换==真){
位图B = BitmapFactory.de codeResource(getResources(),BGND);
c.drawColor(0,PorterDuff.Mode.CLEAR);
c.drawBitmap(二,0,0,NULL);
开关= FALSE;
}其他{
位图B = BitmapFactory.de codeResource(getResources(),BGND);
c.drawBitmap(二,0,0,NULL);
}
}
和你一样,我挣扎着如何清除顶层/ surfaceview在我的多层/ surfaceview应用程序。经过2天的搜索和编码,我发现我自己的路,这是我清除了绘图画布,可以具有多层/ surfaceviews时使用它。背景层不会被覆盖着黑色的,那就是绝招。
油漆涂料=新的油漆();
paint.setXfermode(新PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(油漆);
paint.setXfermode(新PorterDuffXfermode(Mode.SRC));
//开始自己的绘图
i'm attempting to change the background image of a custom View with some success. the image will change but the problem is that i still see traces of the old image. when i attempt to clear the canvas before drawing the new image, it doesn't appear to work. i create a bitmap to store the image. when changing the image, i call Canvas.drawColor() before drawing the new image but the old image persists. i've tried drawColor(0), drawColor(Color.BLACK), c.drawColor(0, PorterDuff.Mode.CLEAR), and none of the above works. as such, i had to post this for review from more experienced minds than mine.
the actual code is as follows:
private int bgnd;
private boolean switching;
public void setBgnd(int incoming){
switching = true;
switch (incoming){
case R.drawable.image1:
bgnd = incoming;
this.invalidate();
break;
case R.drawable.image2:
bgnd = incoming;
this.invalidate();
break;
}
}
protected void onDraw(Canvas c){
if(switching == true){
Bitmap b = BitmapFactory.decodeResource(getResources(), bgnd);
c.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawBitmap(b, 0, 0, null);
switching = false;
}else{
Bitmap b = BitmapFactory.decodeResource(getResources(), bgnd);
c.drawBitmap(b, 0, 0, null);
}
}
Just like you, I struggled how to clear a top layer/surfaceview in my multiple layer/surfaceview app. After 2 days searching and coding, I found out my own way and this is how I cleared a canvas before drawing, you can use it when having multiple layers/surfaceviews. The background layer will not be covered with black, that is the trick.
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
canvas.drawPaint(paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC));
// start your own drawing
这篇关于清除画布Canvas.drawColor()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!