Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android View animation - poor performance on big screens

I developed a puzzle game. In my game I have several custom views, which are nested inside FrameLayout, like layers. Each view displays a bitmap. The largest bitmap is the game board and it takes about 2/3 of screen size. There is also background image under the FrameLayout.

When user touches the screen, simple view animations are performed with this game board view - it can be rotated or flipped.

It all works fine on small screens, but users reported performance issues on Galaxy Tab - there's a noticeable lag just before animation starts and animation itself is not smooth. (Game board bitmap size on these screens is about 600*600px)

Here's my layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootFrame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/backgroundImageLayer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/background"
        android:scaleType="fitXY"/>

    <FrameLayout android:id="@+id/gameFrame"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

            <com.mygame.BoardView
            android:id="@+id/boardLayer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

            <com.mygame.SomeObjectView
            android:id="@+id/someObjectLayer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

            <com.mygame.SomeOtherObjectView
            android:id="@+id/someOtherObjectLayer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        </FrameLayout>    

</FrameLayout>

And here is my BoardView class.

public class BoardView extends View {

Bitmap b;
int x;
int y;

public BoardView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // calculate size of the bitmap
    b = Bitmap.createBitmap(sizeX, sizeY, Bitmap.Config.ARGB_4444);
    createMyBitmap();
}

private void createMyBitmap() {
    Canvas c;
    c = new Canvas(b);
    // lots of c.drawRect() and c.drawLine() , using opaque and semi-transparent colors
}

@Override
public void onDraw(Canvas canvas) {
    canvas.drawBitmap(b, x, y, null);
}
}

And a snippet where I do the animation, it's pretty simple:

AnimationRotate anim1=new AnimationRotate(startAngle,endAngle,centerX,centerY);
anim1.setDuration(500);
anim1.setInterpolator(newDecelerateInterpolator());
startAnimation(anim1);

So what causes lags with view animation and how can I fix it? Is my approach correct or maybe I should use SurfaceView or frame animation or something else?

like image 501
dop2000 Avatar asked Dec 06 '22 17:12

dop2000


2 Answers

Enable Hardware Acceleration if you haven't already. The layout rendering system was modified in 3.0+ to improve performance on these large screens by moving most of the software rendering into hardware. You care read more about it here

There are certain things that will not work work with the hardware acceleration but from what you have shown you are fine. The simplest way to enable it is to add this to your application's manifest and target 3.0 or higher (as in to build against, you can still support lower api levels)

<application android:hardwareAccelerated="true"
...
>
like image 191
smith324 Avatar answered Dec 24 '22 00:12

smith324


I've read that setting the following properties can help improve performance:

this.setAnimationCacheEnabled(true);
this.setDrawingCacheEnabled(true);
like image 28
Oren Bengigi Avatar answered Dec 24 '22 01:12

Oren Bengigi