Search code examples
androidanimationdrawable

Android: Disable texture filtering/interpolation on AnimationDrawable


I have a frame-by-frame AnimationDrawable that I'd like to scale without Android's default, blurry interpolation. (It's pixel art and looks better with nearest-neighbor.) Can I set a flag? Override onDraw? Anything to tell the GPU not to use texture filtering?

Do I need to scale up each individual bitmap in the animation instead? This seems like a waste of CPU and texture memory.

Code example:

// Use a view background to display the animation.
View animatedView = new View(this);
animatedView.setBackgroundResource(R.anim.pixel_animation);
AnimationDrawable animation = (AnimationDrawable)animatedView.getBackground();
animation.start();

// Use LayoutParams to scale the animation 4x.
final int SCALE_FACTOR = 4;
int width = animation.getIntrinsicWidth() * SCALE_FACTOR;
int height = animation.getIntrinsicHeight() * SCALE_FACTOR;
container.addView(animatedView, width, height);

Solution

  • This seems to work, as long as you can assume that all the frames in an AnimationDrawable are BitmapDrawables:

    for(int i = 0; i < animation.getNumberOfFrames(); i++) {
        Drawable frame = animation.getFrame(i);
        if(frame instanceof BitmapDrawable) {
            BitmapDrawable frameBitmap = (BitmapDrawable)frame;
            frameBitmap.getPaint().setFilterBitmap(false);
        }
    }