0

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

我现在使用的代码是:

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;
}

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

我怎样才能做到这一点?这是我正在尝试的第四种方法...

4

1 回答 1

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);

这条线调整了第二层位图的大小。

于 2013-08-01T07:28:24.453 回答