So I've been having animation issues especially when two animations happen at once or right when an activity loads. I understand it's probably a resource problem and a lot of things are going on in the main thread causing the animations to stutter.
I've found a couple interesting suggestions:
1. Threads (ThreadPoolExecutor)
Here: How do I make my animation smoother Android
2. setDrawingCacheEnabled(true)
Here: How does Android's setDrawingCacheEnabled() work?
3. ViewGroup: animationCache = true
Here: http://www.curious-creature.org/2010/12/02/android-graphics-animations-and-tips-tricks/
However I haven't been able to find any sort of examples to implement these things. Any ideas?
I've reduced the amount of stutter on my animations by following these rules listed in order of importance when reducing stutter:
onCreate
, onStart
or onResume
.onClick
and disable touch events until animation is complete.If you are using animation you should follow the android docs; in fact in some cases, you might need to postpone your fragment transition for a short period of time. For example in my case I need to postpone my animation until my viewmodel return some data:
Use postponeEnterTransition()
in the entering fragment onViewCreated()
method:
public class A extends Fragment {
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
...
postponeEnterTransition();
}
}
Once the data are ready to start the transition, call startPostponedEnterTransition()
public class A extends Fragment {
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
postponeEnterTransition();
final ViewGroup parentView = (ViewGroup) view.getParent();
// Wait for the data to load
viewModel.getData()
.observe(getViewLifecycleOwner(), new Observer<List<String>>() {
@Override
public void onChanged(List<String> list) {
// Set the data on the RecyclerView adapter
adapter.setData(list);
// Start the transition once all views have been
// measured and laid out
parentView.getViewTreeObserver()
.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
parentView.getViewTreeObserver()
.removeOnPreDrawListener(this);
startPostponedEnterTransition();
return true;
});
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With