0

我拍照并获得一个位图(bitmap),当我拍照时有一个图像我想留在顶部(bitmao2)。因此,为了保存带有图像的图片,我使用了画布。我想将 bitmap2 准确地定位在位图上,当我拍照时它是如何定位的。这是我所做的

    Bitmap bitmap = BitmapFactory.decodeByteArray(data,0,data.length);
    int width=bitmap.getWidth();
    int height=bitmap.getHeight();
    Bitmap bitmap2 = GameActivity.decodeSampledBitmapFromResource(gameactivity.getResources(), R.drawable.fire16,width1 );
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);  

    ImageView image = (ImageView) gameactivity.findViewById(R.id.imageView4);
    LayoutParams layout = (LayoutParams) image.getLayoutParams();
    int margin = layout.topMargin;
    Bitmap cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); 
    Canvas canvas = new Canvas(cs);
    canvas.drawBitmap(bitmap, 0f, 0f, null);
    canvas.drawBitmap(bitmap2,0,margin, null);

(我旋转图像是因为它处于横向模式,即使我以纵向模式拍摄它,所以我更正了)

问题是即使尺寸 Canvas = 位图尺寸,在我的代码执行后我看不到整个图像,它有点被裁剪,当然 bitmap2 不是它应该在的位置。也许我应该取而代之的是屏幕的尺寸?

4

1 回答 1

1

您可以使用 drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint) 来绘制位图:

Rect src = new Rect(0, 0, bitmap2.getWidth(), bitmap2.getHeight());
Rect dst = new Rect(0, 0, width, height);
canvas.drawBitmap(bitmap2, src, dst, null);

此外,如果您旋转图像,则宽度和高度将在此处交换:

bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true); 
于 2013-02-25T10:08:05.393 回答