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!
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));
you can try to canvas.drawColor(Color.BLACK)
at the start of every draw()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With