dop2000
dop2000

Reputation: 597

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?

Upvotes: 4

Views: 8525

Answers (4)

AnilPatel
AnilPatel

Reputation: 2366

you read from here

View.setLayerType(View.LAYER_TYPE_HARDWARE, null);
ObjectAnimator animator = ObjectAnimator.ofFloat(view, "rotationY", 180);
animator.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        view.setLayerType(View.LAYER_TYPE_NONE, null);
    }
});
animator.start();

Upvotes: 0

Oren Bengigi
Oren Bengigi

Reputation: 1034

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

this.setAnimationCacheEnabled(true);
this.setDrawingCacheEnabled(true);

Upvotes: 3

smith324
smith324

Reputation: 13060

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"
...
>

Upvotes: 11

TheWhiteLlama
TheWhiteLlama

Reputation: 1286

As I know, all 2D-Graphic commands are very slow, and keep on slow down, when the screen is going bigger. I would advise to set up a 3D-Surfaceview to put your images as textures on quads, and render it. This will be much faster than with 2D-Gfx.

Upvotes: 1

Related Questions