Android View animation - poor performance on large screens

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

When the user touches the screen, simple animations of the view are performed with this kind of game panel - they can be rotated or turned over.

Everything works fine on small screens, but users report performance issues on the Galaxy Tab - there is a noticeable lag before the animation starts, and the animation itself is not smooth. (The size of the bitmap in the game board on these screens is about 600 * 600 pixels)

Here is 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 the snippet where I am doing the animation is pretty simple:

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

So what causes delays in view animation and how can I fix it? Is my approach right or maybe I should use SurfaceView or frame animation or something else?

+4
source share
4 answers

As I know, all 2D-Graphic commands are very slow and keep slowing down when the screen gets bigger. I would suggest creating a 3D-Surfaceview to place your images as ATV textures and render them. It will be much faster than with 2D-Gfx.

+1
source

Turn on hardware acceleration if you haven't already. The layout rendering system was changed in version 3.0+ to improve performance on these large screens by moving most of the software to hardware. You take care of this in more detail here.

There are certain things that will not work with hardware acceleration, but from the fact that you showed that everything is in order. The easiest way to enable this is to add this to your application manifest and specify 3.0 or later (as for building against, you can still support lower api levels)

 <application android:hardwareAccelerated="true" ... > 
+11
source

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

 this.setAnimationCacheEnabled(true); this.setDrawingCacheEnabled(true); 
+3
source

you read 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(); 
0
source

Source: https://habr.com/ru/post/1387981/


All Articles