Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android surfaceview animation

Tags:

android

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.

enter image description here

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!

like image 331
Sea turtle Avatar asked Sep 13 '11 08:09

Sea turtle


2 Answers

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));
like image 140
exdeny Avatar answered Nov 15 '22 15:11

exdeny


you can try to canvas.drawColor(Color.BLACK) at the start of every draw()

like image 21
weakwire Avatar answered Nov 15 '22 14:11

weakwire