Skip to content
Frame Animation with Drawable without OutOfMemory
Branch: master
Clone or download
Pull request Compare This branch is 8 commits ahead of tigerjj:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
gradle/wrapper
optanimation
sample
.gitignore
README.md
build.gradle
gradlew
gradlew.bat
import-summary.txt
local.properties
settings.gradle

README.md

Mock Frame Animation

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

How to use

compile 'com.github.avenwu:animation:0.2.0'

Use custom ImageView

<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();
        }
    }
});

Use standard ImageView

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();
        }
    }
});

Animation

Download Video

Optimization

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:

  1. fix warning when reuse bitmap with option.in;
  2. fix animation frozen issue after Home press
  3. FasterAnimationsContainer is no longer singleton, so each ImageView may control it's animation;
  4. global bitmap cache supported;

License

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.
You can’t perform that action at this time.