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);
}
}
This is because you are calling animation while creation of activity/fragment. Try to start the animation after creation of fragment. From
onActivityCreated()
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().
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 inonStart()
method oronResume()
, its the time when your activity is created and completely ready for user visibility.The code
if(!anim.hasStarted())
is added because it happens that many times activity goes in background and then come to foreground, callingonResume()
. 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.
This will give animation appropriate time to start smoothly. Hope this will help you.
the reason is because you are doing this at the very beginning,
use: