Android在另一个位图上绘制一个调整大小的位图

时间:2013-07-31 20:46:51

标签: android bitmap android-canvas

我需要绘制一个在另一个位图上调整大小的位图(缩小一个位图,然后在另一个位图上绘制)。

我现在使用的代码是:

Resources res = ((Context)(activity)).getResources();
Drawable[] layers = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.marker_report_red);
layers[1] = new BitmapDrawable(getResizedBitmap(((BitmapDrawable)res.getDrawable(R.drawable.report_path_merge_ic)).getBitmap(),10,10));
LayerDrawable layerDrawable = new LayerDrawable(layers);
Bitmap b = Bitmap.createBitmap(layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
layerDrawable.setBounds(0, 0, layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight());
layerDrawable.draw(new Canvas(b));

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    int width = bm.getWidth();
    int height = bm.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 resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    return resizedBitmap;
}

但是生成的图像只显示了一个模糊的位图,它被拉伸到底层的大小。

我该怎么做?这是我尝试的第四种方法......

1 个答案:

答案 0 :(得分:0)

我修复了问题,我完全使用了不同的代码:

Resources res = ((Context)(activity)).getResources();
Drawable[] layers = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.marker_report_red);
layers[1] = res.getDrawable(R.drawable.report_path_merge_ic);
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(1, 15, 10, 15, 15);
Bitmap b = Bitmap.createBitmap(layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
layerDrawable.setBounds(0, 0, layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight());
layerDrawable.draw(new Canvas(b));

强调行:

layerDrawable.setLayerInset(1, 15, 10, 15, 15);

此行调整了第二层上的位图大小。