Reputation: 467
I'm trying to achieve animation on a surfaceview. But, my surfaceview's background is transparent, and I can't clear away the pixels of last frame. Can I achieve some animations on transparent surfaceview? Maybe I only use 'Animation' class on a view. Thanks!
Maybe I didn't describe the question clearly. I want to achieve a animation, but surfaceview can't refresh last graph.It looks like the picature.
The background is transparent. I known draw canvas with color can clean the last drawn graph. But its background is transparent, the method doesn't achieve refresh. Please help me. Thanks!
Upvotes: 5
Views: 5118
Reputation: 126
Watch the following code, it worked for me:
Paint clearPaint = new Paint();
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
this.setZOrderOnTop(true);
this.getHolder().setFormat(PixelFormat.TRANSPARENT);
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
public void drawMethod(){
Canvas canvas = this.getHolder().lockCanvas();
canvas.drawPaint(clearPaint);
canvas.drawBitmap(yourBitmap, null, yourBitmapRect, null);
this.getHolder().unlockCanvasAndPost(canvas);
}
You have have to put your SurfaceView on top in Z order and set PixelFormat to transparent. That will make your SurfaceView transparent (this part you've done, as you wrote). Then you have to fill your surface with a transparent paint before each new drawing. This "special" paint needs to be set like so:
clearPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
Upvotes: 9
Reputation: 9300
you can try to canvas.drawColor(Color.BLACK)
at the start of every draw()
Upvotes: 3