调整位图大小以创建图标

时间:2012-07-09 19:17:27

标签: android bitmap

我正在调整我创建的视图的bimap图像。

Bitmap image = imageCreate( getMeasuredWidth(), getMeasuredHeight() );
image = imageResize( image, 62, 62 );
imageSave(image,"test.png");

调整大小发生在我的自定义视图中。

protected Bitmap imageCreate( int width, int height ) {

    Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(image);
    draw(canvas);
    return image;
}

protected Bitmap imageResize(Bitmap image, int newWidth, int newHeight) {

    int width = image.getWidth();
    int height = image.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);
    // RECREATE THE NEW BITMAP
    Bitmap resizedImage = Bitmap.createBitmap(image, 0, 0, width, height, matrix, false);

    return resizedImage;
}

最后我保存了图片:

protected boolean imageSave( Bitmap image, String filename, Context context ) {

    try {
        FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);
        image.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.close();

        return true;
    } catch (Exception e) {

        e.printStackTrace();
    }

    return false;
}

我的问题是,为什么图像质量太棒了!???

图像有点像素化。原始图像太棒了。

还有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

此代码

//重新开始新的BITMAP 位图resizedImage = Bitmap.createBitmap(图像,0,0,宽度,高度,矩阵,假);

将最后一个参数设置为true以启用过滤。

这有帮助吗?