用另一个图像android覆盖画布图像

时间:2015-01-01 20:09:20

标签: android canvas

我在屏幕上显示了平面图的图像,我的问题如何在其上覆盖另一个图像。

查看另一个帖子中的图片,其中我询问了如何here

/**
 * floor plan drawing.
 *
 * @param canvas the canvas on which the background will be drawn
 */
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
    image= Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(image, 0, 0, null);

}

2 个答案:

答案 0 :(得分:1)

如何在第一张图片上添加第二张图片?

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
    image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(image, 0, 0, null);

    Bitmap over = BitmapFactory.decodeResource(getResources(), R.drawable.overlay);
    image = Bitmap.createScaledBitmap(over, canvas.getWidth(), canvas.getHeight(), true);
    canvas.drawBitmap(image, 0, 0, null);
}

答案 1 :(得分:0)

这是我的方法,将两个图像叠加到ImageView中:

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

使用此方法:

  public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {

        int bitmap1Width = bitmapBackground.getWidth();
        int bitmap1Height = bitmapBackground.getHeight();
        int bitmap2Width = bitmapImage.getWidth();
        int bitmap2Height = bitmapImage.getHeight();

        float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
        float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);

        Bitmap overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmapBackground.getConfig());
        Canvas canvas = new Canvas(overlayBitmap);
        canvas.drawBitmap(bitmapBackground, new Matrix(), null);
        canvas.drawBitmap(bitmapImage, marginLeft, marginTop, null);

        return overlayBitmap;
    }

获取引用和位图以进行de overlay!

   ImageView imageView = (ImageView) findViewById(R.id.imageView);

    Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.androide);

    Bitmap bmpImages = overlayBitmap(background, image);
    imageView.setImageBitmap(bmpImages);

得到这个结果:

enter image description here

Download the complete sample.