android - 调整大小/缩放位图后图像质量不佳

标签 android image-manipulation

我正在编写一款纸牌游戏,并且需要我的卡片在不同情况下具有不同的尺寸。我将图像存储为位图,以便可以快速绘制和重绘(用于动画)。

我的问题是,无论我如何尝试缩放图像(无论是通过 matrix.postScale、matrix.preScale 还是 createScaledBitmap 函数),它们总是会出现像素化和模糊。我知道这是导致问题的缩放比例,因为在不调整大小的情况下绘制图像时看起来很完美。

我已经完成了这两个线程中描述的每个解决方案:
android quality of the images resized in runtime
quality problems when resizing an image at runtime

但仍然没有到达任何地方。

我使用以下代码存储我的位图(到 HashMap 中):

cardImages = new HashMap<Byte, Bitmap>();
cardImages.put(GameUtil.hearts_ace, BitmapFactory.decodeResource(r, R.drawable.hearts_ace));

并使用此方法(在 Card 类中)绘制它们:

public void drawCard(Canvas c)
{
    //retrieve the cards image (if it doesn't already have one)
    if (image == null)
        image = Bitmap.createScaledBitmap(GameUtil.cardImages.get(ID), 
            (int)(GameUtil.standardCardSize.X*scale), (int)(GameUtil.standardCardSize.Y*scale), false);

        //this code (non-scaled) looks perfect
        //image = GameUtil.cardImages.get(ID);

    matrix.reset();
    matrix.setTranslate(position.X, position.Y);

    //These methods make it look worse
    //matrix.preScale(1.3f, 1.3f);
    //matrix.postScale(1.3f, 1.3f);

    //This code makes absolutely no difference
    Paint drawPaint = new Paint();
    drawPaint.setAntiAlias(false);
    drawPaint.setFilterBitmap(false);
    drawPaint.setDither(true);

    c.drawBitmap(image, matrix, drawPaint);
}

任何见解将不胜感激。谢谢

最佳答案

使用 createScaledBitmap 会使您的图像看起来很糟糕。 我遇到了这个问题,我已经解决了。 下面的代码将解决问题:

public Bitmap BITMAP_RESIZER(Bitmap bitmap,int newWidth,int newHeight) {    
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

    }

关于android - 调整大小/缩放位图后图像质量不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821488/

相关文章:

android - 将图像附加到联系人

java - 即使我在 manifest.xml 中正确引入了它,我也收到了类似 No Activity found to handle Intent 的错误

java - hashmap.get() 的拆箱会产生 NullPointerException

android - 将可编辑内容转换为字符串

java - 从更大的图像创建图像

javascript - 我们如何使用 JavaScript 查找图像是否被修改/操作

java - Android numberDecimal 不工作

pdf - 来自 PDFS 的高分辨率图像

Javascript 图像处理 反转颜色

iphone - iPhone 上的图像处理