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

查看:60
本文介绍了在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资源文件,并将anti-alias标志设置为false.根本没用

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

方法二

从资源创建位图对象,并将anti-alias标志设置为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 时,将 Paint .setFilterBitmap(false)一起使用.一种方法是使用自定义的 Drawable ,该包装将基础的 .draw()调用与 DrawFilter 一起修改所需的绘制标志.

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天全站免登陆