如何在不模糊图像的情况下缩放图片?

8
我正在尝试将一个非常小的图像(大约20x40像素)缩放到任意大小的图像。我通过设置ImageView.scaleType属性来进行缩放,但是它会从一个原始像素颜色渐变到下一个,从而创建一个模糊的大版本图像。
这是问题的一些背景信息。我正在尝试为Android创建一个旧式16位风格的游戏。我希望将精灵/等存储为小文件,然后根据用户的屏幕大小进行缩放。这样,我就不必为每个精灵创建许多不同大小的图像。现在我只是在做一个概念验证,所以我想将一个小的20x40像素的图像文件缩放到整个屏幕的大小。
*编辑:我找到了使用Reflog推荐的方法的方式。然而,它仍然会有一些渐变效果,只是比默认算法少得多。
*编辑2:搞定了!我必须关闭抗锯齿功能
*编辑3:这对我来说神奇地停止工作了,我发现另一个需要禁用抖动/自动缩放的地方。添加了选项行。
@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setDither(false);
    paint.setAntiAlias(false);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inScaled = false;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tinyimg, options);
    canvas.drawBitmap(bitmap, null, new RectF(getLeft(), getTop(), getRight()/2, getBottom()/2), paint);
}

3
听起来你想要最近邻插值缩放。我不熟悉Android开发,但你可以试着在文档中搜索这个功能。 - Bemmu
2个回答

6

用途:

paint.setDither(false);
g.drawBitmap(img, src, dst, paint);

使用NN缩放可以减少模糊,但也会降低缩放的质量。


1
好的,谢谢。我完全是新手Android开发者,所以我正在努力弄清楚如何使用你告诉我的内容。我之前一直在使用Xml将ImageView放入Layout中,但现在我正在尝试通过代码来实现,这样我就可以使用paint对象了。我明天会继续进行。那个“g”变量是Canvas吗? - mnemy
1
没错,这是一个画布(Canvas)。在重写的protected void onDraw(Canvas g)方法中使用此代码。 - reflog
1
嗯,显然评论不是在这个网站上继续讨论的地方,所以我把它添加到了原始问题中。 - mnemy

1
也许这段代码可以帮助你:
/**
 * @param bitmap Bitmap original
 * @param finWid width of the new Bitmap (width of the cut)
 * @param finHei height of the new Bitmap (height of the cut)
 * @param angulo angle of rotation in degrees
 * @param sx scale X
 * @param sy scale Y
 * @param tx Translate X
 * @param ty Translate Y
 * @param config {@link Config} of the new Bitmap
 * @return {@link Bitmap} transformed
 */
public Bitmap createTransPixBitmap(Bitmap bitmap, int finWid, int finHei, float  angulo, float sx, float sy, float tx, float ty, Config config){

    return Bitmap.createBitmap(createTransPixArray(bitmap, bitmap.getWidth(), bitmap.getHeight(), finWid, finHei, angulo, sx, sy, tx, ty), finWid, finHei, config);

}

public int[] createTransPixArray(Bitmap bitmap, int width, int height, int finWid, int finHei, float angulo, float sx, float sy, float tx, float ty){

    float scaWid = width*sx;
    float scaHei = height*sy;

    int[] ori = new int[width*height];
    bitmap.getPixels(ori, 0, width, 0, 0, width, height);

    bitmap.recycle();
    bitmap = null;
    System.gc();

    return transformPix(ori, width, height, scaWid, scaHei, finWid, finHei, angulo, tx, ty);
}

/**
 * Core function for apply scale, translate and rotation (cut the image if you want too)
 * @param ori original color array
 * @param wid original width
 * @param hei original height
 * @param scaWid scaled original width
 * @param scaHei scaled original height
 * @param finWid width of the new Bitmap
 * @param finHei height of the new Bitmap
 * @param angulo rotation in degrees
 * @param tx Translate X
 * @param ty Translate Y
 * @return int[] of colors
 */
private int[] transformPix(int[] ori, int wid, int hei, float scaWid, float scaHei, int finWid, int finHei, float angulo, float tx, float ty){

    int[] fin = new int[finWid*finHei];

    double sin = Math.sin(Math.toRadians(angulo));
    double cos = Math.cos(Math.toRadians(angulo));

    int dx = (int)((scaWid-finWid)/2);
    int dy = (int)((scaHei-finHei)/2);

    /* No se como explicar esto, solo puedo decir que el proceso de escalado es en sentido inverso
     * escala, rota, translada y corta, todo al mismo tiempo :D
     */
    for(int y = 0; y < finHei; y++){
        for(int x = 0; x < finWid; x++){
            int tempX = (int)Math.floor(((x+dx-((float)scaWid/2))*((float)wid/scaWid))+0.5f-tx);
            int tempY = (int)Math.floor(((y+dy-((float)scaHei/2))*((float)hei/scaHei))+0.5f-ty);

            int tempRX = (int)Math.floor(((cos*(float)tempX)+(sin*(float)tempY))+0.5f+((float)wid/2));
            int tempRY = (int)Math.floor(((cos*(float)tempY)-(sin*(float)tempX))+0.5f+((float)hei/2));

            if((tempRX >= 0 && tempRX < wid) && (tempRY >= 0 && tempRY < hei))
                fin[x+(y*finWid)] = ori[tempRX+(tempRY*wid)];   
        }
    }

    ori = null;
    return fin;
}

这个函数只是对颜色数组进行缩放(如果您不需要额外的功能):
private int[] scale(int[] ori, int wid, int hei, int finWid, int finHei){

    int[] fin = new int[finWid*finHei];

    for(int y = 0; y < finHei; y++){
        for(int x = 0; x < finWid; x++){
            int temp = (int)(x*(wid/finWid))+((int)(y*(hei/finHei))*wid);
            fin[x+(y*finWid)] = ori[temp];
        }
    }

    return fin;
}

希望这对你有所帮助,我非常乐意接受改进此代码的建议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接