aga
aga

Reputation: 29426

Animation lags on large screen sizes

I have a RelativeLayout which stores five ImageView, each of them contains an image of cloud. All images differ in size, the smallest being 582*182 and the largest being 700*400. The size of area which contains clouds is 860*1080.
In the top of the screen is the image which I want to load onto my server and below is the RelativeLayout described above. When the user falls on that screen the clouds start moving. The problem is that the whole animation is lagging badly (I'm trying to run it on the HTC One). My animation described like below:

    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:interpolator="@android:anim/linear_interpolator"
         android:fillEnabled="true"
         android:fillAfter="true">
        <translate xmlns:android="http://schemas.android.com/apk/res/android"
                   android:duration="12000"
                   android:toXDelta="-12%p"
                   android:repeatCount="infinite"
                   android:repeatMode="reverse"/>
    </set>

The ImageViews are described like so:

<ImageView
    android:id="@+id/clouds_image_view_cloud2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="cloud 2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"/>

And I start it like below:

Animation animRToL = AnimationUtils.loadAnimation(mContext, R.anim.right_to_left);
Animation animLToR = AnimationUtils.loadAnimation(mContext, R.anim.left_to_right);

mClouds[0].startAnimation(animRToL);
mClouds[2].startAnimation(animRToL);

mClouds[1].startAnimation(animLToR);
mClouds[3].startAnimation(animLToR);

mClouds[4].startAnimation(animRToL);

I've checked the hardware acceleration condition via mClouds[0].isHardwareAccelerated() - it's enabled. I've tried to decrease the BitMap's size by loading them in RGB_565 format - no luck. Is there anything I can do about this lagging?

P.S. The animation of the same images works without any lagging on iOS version of application which runs on iPhone 4.

Upd.: I've noticed that animations run smoothly if I reduce the number of clouds been animated from 5 to 4.

Upvotes: 2

Views: 1078

Answers (2)

Tony
Tony

Reputation: 1611

Similar to me, my animation can run smoothly on small devices but so laggy, jerkily on larger ones such as Samsung Galaxy Pro with screen size of 2560 x 1600 px.

I have tried many solutions but almost given up. However I realise that I can still drag without lagging some much more complicated objects than the one I want to use with animation. Obviously some Android animations (at least the translate one) are not really optimised.

Firstly I have tried to use some thread / sleep to mimic the dragging behaviour. I have written the code using Handler / Runnable and the result is improved quite impressively.

Then after searching for a while, I found ValueAnimator can do the same job with my Handler / Runnable code but in the better way. My final code looks like below (it moves an object horizontally):

    ValueAnimator anim = ValueAnimator.ofFloat(0, amountToMoveX);
    anim.setDuration(300);
    final float startX = mainView.getX();

    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mainView.setX(startX + (Float) animation.getAnimatedValue());
        }
    });

    anim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
        }

        @Override
        public void onAnimationCancel(Animator animation) {
        }

        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    });

    anim.start();

Upvotes: 0

Behnam
Behnam

Reputation: 6620

This may sound crazy but has saved me in a similar situation:

Use different animation objects for different Views.

In your case: You need three animRTL objects, and two animLTR Java Objects. So: animLTR1, animLTR2, animRTL1 animRTL2, animRTL3. Note that you don't have to create any more XML animation files than the two you already have.

Try and let me know the result.

EDIT :

There are two other solutions worth trying on my mind:

In your manifest, put under application tag

  android:hardwareAccelerated="true"

  android:largeHeap="true"

Upvotes: 1

Related Questions