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

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

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

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

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

我用这段代码存储我的位图(到一个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);}

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


喵喵时光机
浏览 456回答 3
3回答

不负相思意

我在低屏幕分辨率下有blury图像,直到我禁用了来自资源的位图加载缩放:Options&nbsp;options&nbsp;=&nbsp;new&nbsp;BitmapFactory.Options(); &nbsp;&nbsp;&nbsp;&nbsp;options.inScaled&nbsp;=&nbsp;false; &nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;source&nbsp;=&nbsp;BitmapFactory.decodeResource(a.getResources(),&nbsp;path,&nbsp;options);

千巷猫影

使用createScaledBitmap会使您的图像看起来非常糟糕。我遇到了这个问题,我已经解决了。下面的代码将解决问题:public&nbsp;Bitmap&nbsp;BITMAP_RESIZER(Bitmap&nbsp;bitmap,int&nbsp;newWidth,int&nbsp;newHeight)&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;Bitmap&nbsp;scaledBitmap&nbsp;=&nbsp;Bitmap.createBitmap(newWidth,&nbsp;newHeight,&nbsp;Config.ARGB_8888); &nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;ratioX&nbsp;=&nbsp;newWidth&nbsp;/&nbsp;(float)&nbsp;bitmap.getWidth(); &nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;ratioY&nbsp;=&nbsp;newHeight&nbsp;/&nbsp;(float)&nbsp;bitmap.getHeight(); &nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;middleX&nbsp;=&nbsp;newWidth&nbsp;/&nbsp;2.0f; &nbsp;&nbsp;&nbsp;&nbsp;float&nbsp;middleY&nbsp;=&nbsp;newHeight&nbsp;/&nbsp;2.0f; &nbsp;&nbsp;&nbsp;&nbsp;Matrix&nbsp;scaleMatrix&nbsp;=&nbsp;new&nbsp;Matrix(); &nbsp;&nbsp;&nbsp;&nbsp;scaleMatrix.setScale(ratioX,&nbsp;ratioY,&nbsp;middleX,&nbsp;middleY); &nbsp;&nbsp;&nbsp;&nbsp;Canvas&nbsp;canvas&nbsp;=&nbsp;new&nbsp;Canvas(scaledBitmap); &nbsp;&nbsp;&nbsp;&nbsp;canvas.setMatrix(scaleMatrix); &nbsp;&nbsp;&nbsp;&nbsp;canvas.drawBitmap(bitmap,&nbsp;middleX&nbsp;-&nbsp;bitmap.getWidth()&nbsp;/&nbsp;2,&nbsp;middleY&nbsp;-&nbsp;bitmap.getHeight()&nbsp;/&nbsp;2,&nbsp;new&nbsp;Paint(Paint.FILTER_BITMAP_FLAG)); &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;scaledBitmap; &nbsp;&nbsp;&nbsp;&nbsp;}

慕妹3146593

createScaledBitmap有一个标志,您可以设置是否应过滤缩放的图像。那个标志改善了位图的质量......
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android