【问题标题】:overlay canvas image with another image android用另一个图像 android 覆盖画布图像
【发布时间】:2017-05-27 02:18:57
【问题描述】:

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

查看另一个帖子中的图片,我问了如何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);

}

【问题讨论】:

  • 第一:onDraw 不是BitmapFactory.decodeResource() 的最佳位置,第二:代替调用Bitmap.createScaledBitmap() 修改canvas(缩放、平移、旋转)
  • @pskink 好的,什么只在 Draw 中显示?
  • 在 onDraw 只调用画布相关的东西,我认为你的 IDE 会警告你调用 decodeResource 和类似的东西
  • @pskink 好的,但它不允许我拍摄 image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);出去?
  • 你不应该使用缩放位图:而是缩放画布

标签: android canvas


【解决方案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);
}

【讨论】:

  • 如果要叠加 500 多张图片,速度会非常慢
  • 抱歉,我正在处理 900 张图片 :) 我确实做得很快 :)
  • 好吧,出于 OP 的目的(覆盖 2 张图像),这已经足够快了。无论如何,我都会很想知道您为什么要叠加这么多图像。也很高兴看到您的光速代码。
  • 实际上我需要在视频上叠加一些图像。为此,我使用 ffmpeg lib 将 vdo 转换为帧,因此对于 60 秒的视频,它将以 15 帧/秒的速度生成 900 帧...所以在处理完文件夹中的所有图像后,我将从文件夹 n 中选择图像覆盖在该文件夹中:P :) 并且为了使其快速处理,我在覆盖之前将图像 32 位转换为 8 位。
  • 我明白了。但是对于几个静态图像(比如背景图像和有条件变化的前景图标),我可以对我的代码感到满意。 ;)
【解决方案2】:

这是我的方法,将两个图像叠加到一个 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;
    }

获取参考和位图进行叠加!

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

结果如下:

Download the complete sample.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多