在 Android Imageview 上禁用抗锯齿 [英] Disable anti-aliasing on Android Imageview

查看:21
本文介绍了在 Android Imageview 上禁用抗锯齿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在图像视图中显示 96x96 像素艺术精灵,如果不应用抗锯齿(或至少某种形式的插值,如果它不是抗锯齿)就无法显示它,这会破坏锐利的边缘的精灵.

I'm showing 96x96 pixel art sprites in imageviews and can't get it to display without applying anti-aliasing (or at least some form of interpolation, if it isn't anti-aliasing), which ruins the sharp edge of the sprites.

如何做到这一点?我尝试了以下从互联网上收集的方法,但都没有奏效.

How can this be done? I've tried the following methods I've picked up from around the internet, but none work.

方法一

使用位图创建 xml 资源文件,并将抗锯齿标志设置为 false.完全没用

creating an xml resource file with a bitmap, and setting the anti-alias flag to false. Didn't work at all

方法二

从资源创建位图对象,并将抗锯齿标志设置为 false.根本没用.

creating a bitmap object from the resource, and setting the anti-alias flag to false. Didn't work at all.

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dryad_back_large);
    BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
    drawable.setAntiAlias(false);
    playerMonsterImageView.setImageDrawable(drawable);

方法三使用足够大的精灵,这样它们就不需要按比例缩小,只需放大(512x512 对我有用)

METHOD THREE Using sprites which are large enough so that they will never need to be scaled down, only up (512x512 worked for me)

所以这第三种方法可行,但它似乎真的很笨拙,我希望有更好的方法来解决这个问题.

So this third method WORKS, but it seems really ham-fisted, and I was hoping there was a better way of going about this.

也许我错过了除抗锯齿以外的其他东西?

Maybe there's something other than anti-aliasing going on here that I've missed?

推荐答案

有多种因素可能会使用抗锯齿来缩放您的图像.

There are multiple things that may be scaling your image with anti-aliasing.

一个是BitmapFactory.例如,如果您有一个 drawable-mdpi 资源并且当前资源配置是 xhdpi,它将在解码时缩放.您可以提供选项来避免这种情况,但最简单的解决方法是将这些资源放在 drawable-nodpi 中.

One is BitmapFactory. For example, if you have a drawable-mdpi resource and the current resource configuration is xhdpi, it will scale at decode time. You can provide options to avoid this, but the easiest way around it is to put these resources in drawable-nodpi.

默认情况下,画布还使用双线性采样缩放图像.为避免这种情况,请在绘制到 Canvas 时使用带有 .setFilterBitmap(false)Paint.一种方法是使用自定义 Drawable,它使用修改所需绘制标志的 DrawFilter 包装底层 .draw() 调用.

The canvas also scales the image with bilinear sampling by default. To avoid that, use a Paint with .setFilterBitmap(false) when drawing to the Canvas. One way to do that is by using a custom Drawable which wraps the underlying .draw() call with a DrawFilter modifying the desired paint flags.

public class AliasingDrawableWrapper extends DrawableWrapper {
    private static final DrawFilter DRAW_FILTER =
        new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);

    public AliasingDrawableWrapper(Drawable wrapped) {
        super(wrapped);
    }

    @Override
    public void draw(Canvas canvas) {
        DrawFilter oldDrawFilter = canvas.getDrawFilter();
        canvas.setDrawFilter(DRAW_FILTER);
        super.draw(canvas);
        canvas.setDrawFilter(oldDrawFilter);
    }
}

imageView.setDrawable(new AliasingDrawableWrapper(getDrawable(R.drawable.bitmap)));

这篇关于在 Android Imageview 上禁用抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆