Android first time animation is not smooth

2020-07-23 09:02发布

I am trying one animation to slide up imageView from bottom of screen to center of screen but when I do this animation for very first time it's not smooth but when do animation for second time it's normal and smooth. I have tried almost everything but I couldn't solve my issue.

This is my anim file

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="700"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5"/>
    <translate
        android:fromXDelta="0%"
        android:toXDelta="0%"
        android:fromYDelta="300%"
        android:toYDelta="0%"
        android:duration="2000"
        android:zAdjustment="top" />
</set>

and this how I am displaying on my fragment

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private Animation anim;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startAnimations();
    }

    private void startAnimations() {
        anim = AnimationUtils.loadAnimation(this, R.anim.slide_up_animation);
        ImageView imageView = (ImageView) findViewById(R.id.splash);
        imageView.startAnimation(anim);
    }
}

4条回答
smile是对你的礼貌
2楼-- · 2020-07-23 09:28

This is because you are calling animation while creation of activity/fragment. Try to start the animation after creation of fragment. From onActivityCreated()

查看更多
干净又极端
3楼-- · 2020-07-23 09:30

The first thing to make sure is that you are not doing any heavy work on your main thread.If something is required, you can move it to the background thread.

Since you have mentioned displaying in a fragment, you should put your animation code inside the following method.It will make sure that the animation happens only if the fragment is active. Also, you should always stop your animation in onDestroy().

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
  super.setUserVisibleHint(isVisibleToUser);
}
查看更多
对你真心纯属浪费
4楼-- · 2020-07-23 09:31

Remember Activity lifecycle, onCreate() creates the view, and you are showing animation in this method which introduces some lag. The animation wont be smooth for first time. To rescue this you should do your animation in onStart() method or onResume(), its the time when your activity is created and completely ready for user visibility.

This is the best time to show animation.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        anim = AnimationUtils.loadAnimation(this, R.anim.slide_up_animation);
        ImageView imageView = (ImageView) findViewById(R.id.splash);

    }

    private void startAnimations() {
        if(!anim.hasStarted()){
             imageView.startAnimaition(anim);
        }
    }

    @Override
    public void onResume(){
         startAnimatons();
    }

The code if(!anim.hasStarted()) is added because it happens that many times activity goes in background and then come to foreground, calling onResume(). So you have to check if animation has started or not. If its started earlier then no need to start it again.(This is for infinte animation)

For finite animation you have to make use of onPause() method too. I will left this part for you to do.(Think what @NamNH mentioned in your other question.)

Edits

On first time, the appearance of activity and animation are performed together which creates the lag. So you should put your animation in some handler and perform it after few millisecond.

@Override
    public void onResume(){
        super.onResume();

        if(!anim.hasStarted()){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    imageView.startAnimation(anim);
                },200);
        }
    }

This will give animation appropriate time to start smoothly. Hope this will help you.

查看更多
仙女界的扛把子
5楼-- · 2020-07-23 09:37

the reason is because you are doing this at the very beginning,

use:

try {
    Thread.sleep(400);
} catch (InterruptedException e) {
    e.printStackTrace();
}
startAnimations();
查看更多
登录 后发表回答