相关疑难解决方法(0)

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

我正在写一个纸牌游戏,需要我的卡在不同情况下的大小不同.我将我的图像存储为位图,以便可以快速绘制和重绘(用于动画).

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

我已经完成了这两个线程中描述的每个解决方案:
在运行时调整图像大小时,运行时质量问题中的图像的android质量大小
调整

但还是没有到达任何地方.

我使用以下代码存储我的位图(到一个hashmap):

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

并使用此方法绘制它们(在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); …
Run Code Online (Sandbox Code Playgroud)

android image-manipulation

75
推荐指数
6
解决办法
9万
查看次数

在运行时调整图像大小时的质量问题

我在磁盘上有一个图像文件,我正在调整文件大小并将其作为新的图像文件保存回磁盘.为了这个问题,我没有将它们带入内存以便在屏幕上显示它们,只是为了调整它们并重新保存它们.这一切都很好.但是,缩放后的图像上有如下所示的工件:android:运行时调整大小的图像质量

它们会因为这种失真而被保存,因为我可以将它们从磁盘上取下并在计算机上查看它们,但它们仍然存在同样的问题.

在将图像加载到Bitmap对象以将位图解码到内存时,我使用类似于此奇怪的内存不足问题的代码:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageFilePathString, options);

int srcWidth = options.outWidth;
int srcHeight = options.outHeight;
int scale = 1;

while(srcWidth / 2 > desiredWidth){
   srcWidth /= 2;
   srcHeight /= 2;
   scale *= 2;
}

options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = scale;
Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(imageFilePathString, options);
Run Code Online (Sandbox Code Playgroud)

然后我正在进行实际缩放:

Bitmap scaledBitmap = Bitmap.createScaledBitmap(sampledSrcBitmap, desiredWidth, desiredHeight, false);
Run Code Online (Sandbox Code Playgroud)

最后,新调整大小的图像将保存到磁盘:

FileOutputStream out = new FileOutputStream(newFilePathString);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
Run Code Online (Sandbox Code Playgroud)

然后,正如我所提到的,如果我将该文件从磁盘中拉出来并查看它,它就会将质量问题链接到上面并且看起来很糟糕.如果我跳过createScaledBitmap并将samplesSrcBitmap直接保存回磁盘就没有问题,似乎只有在大小改变时才会发生.

我已经尝试过,正如您在代码中看到的那样,设置inDither为false,如http://groups.google.com/group/android-developers/browse_thread/thread/8b1abdbe881f9f71所述,并在上面的第一个链接帖子中提到.这并没有改变任何事情.另外,在我链接的第一篇文章中,罗曼盖伊说:

不要在绘图时调整大小(这将是非常昂贵的),尝试在屏幕外位图中调整大小并确保位图为32位(ARGB888). …

android resize image bitmap

28
推荐指数
2
解决办法
3万
查看次数

标签 统计

android ×2

bitmap ×1

image ×1

image-manipulation ×1

resize ×1