MockFrameAnimation can help to avoid OutOfMemoryError when playing frame animation. It loads an image on background thread with global bitmap cache.
As we known, Android loads all the drawables at once for any frame animations, so animation with many frames causes OutOfMemoryError easily.
For more information http://hub.hacktons.cn/animation
compile 'com.github.avenwu:animation:0.2.0'
<cn.hacktons.animation.MockFrameImageView
android:id="@+id/imageview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
app:cache_percent="0.4"
app:src="@drawable/loading_animation"/>
ImageView imageView = (ImageView) findViewById(R.id.imageview);
animateDrawable = (Animatable) imageView.getDrawable();
((Switch) findViewById(R.id.button)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
animateDrawable.start();
} else {
animateDrawable.stop();
}
}
});
int[] FRAMES = {
R.drawable.num_0,
R.drawable.num_1,
R.drawable.num_2,
R.drawable.num_3,
R.drawable.num_4,
R.drawable.num_5,
R.drawable.num_6,
R.drawable.num_7,
R.drawable.num_8,
R.drawable.num_9,
R.drawable.num_a,
R.drawable.num_b,
R.drawable.num_c,
R.drawable.num_d,
R.drawable.num_e,
R.drawable.num_f,
};
animateDrawable = new AnimationBuilder()
.frames(FRAMES, 120/*duration*/)
.cachePercent(0.4f)
.oneShot(false)
.into(findViewById(R.id.imageview));
((Switch) findViewById(R.id.button)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
animateDrawable.start();
} else {
animateDrawable.stop();
}
}
});
The standard android frame animation is more suit for small animations with less images, so it won't lead to OutOfMemoryError while keep the animation fluent; As to MockFrameAnimation, we decode image dynamically and exert as little pressure as possible on system memory. These can increase the pressure on CPU, so we allow developer to set the cache size for the new balance between memory and CPU. The more we cache, the less we need to decode.
This project is fork from FasterAnimationsContainer; Since the original project seems no longer maintained actively and there are some issues need to be fixed before it can be used. We've send Pull Request and fixed these issues in MockFrameAnimation.
The mainly changes we've done:
- fix warning when reuse bitmap with option.in;
- fix animation frozen issue after Home press
- FasterAnimationsContainer is no longer singleton, so each ImageView may control it's animation;
- global bitmap cache supported;
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.