0

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);
kvance
  • 1,479
  • 18
  • 22

2 Answers2

1

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);
    }
}
kvance
  • 1,479
  • 18
  • 22
0

If you know the texture ID of the texture that's being rendered, at any time after it's created and before it's rendered you should be able to do:

glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

If this is the only texture being rendered then you can do that anywhere from your render thread and it will get picked up without having to explicitly glBindTexture().

fluffy
  • 5,212
  • 2
  • 37
  • 67
  • Can you get the texture ID of an AnimationDrawable? I don't think Android exposes that. – kvance May 20 '14 at 10:53
  • @kvance Doesn't matter, all you're doing is overriding the texture environment of the currently-bound texture. But I'm assuming OpenGL acceleration and so on. – fluffy May 28 '14 at 06:51