Android:在AnimationDrawable上禁用纹理过滤/插值



我有一个逐帧的AnimationDrawable,我想在没有Android默认的模糊插值的情况下缩放。(这是像素艺术,与近邻结合效果更好。)我可以插一面旗吗?覆盖onDraw ?告诉GPU不要使用纹理过滤吗?

我需要在动画中按比例放大每个单独的位图吗?这似乎是浪费CPU和纹理内存。

代码示例:

// 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);

这似乎可以工作,只要你可以假设AnimationDrawable中的所有帧都是BitmapDrawable s:

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

如果你知道正在渲染的纹理的纹理ID,在它创建之后和渲染之前的任何时候,你应该能够做:

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

如果这是唯一被渲染的纹理,那么你可以在渲染线程的任何地方做,它将被拾取,而不必显式地glBindTexture()

最新更新