【问题标题】:Android: Combine two overlay imageviews to a bmp with correct positionAndroid:将两个叠加图像视图组合到具有正确位置的 bmp
【发布时间】:2012-09-18 15:57:39
【问题描述】:

我有两个 ImageView。 ImageView1 是背景图像,ImageView2 是较小的图像。 ImageView2 的位置在应用程序的中间。

我想将这两个 ImageView 组合成一个位图,以便 ImageView2 在 ImageView1 之上。

合并过程正常,但 ImageView2 始终位于 bmp 文件的左上角。

以下是我用来生成 bmp 的代码:

    ImageView iv = (ImageView)findViewById(R.id.imageView1);
    ImageView iv2 = (ImageView)findViewById(R.id.imageView2);

    File rootPath = new File(Environment.getExternalStorageDirectory(), "testmerge");

    if (!rootPath.exists()) {
        rootPath.mkdirs();
    }

    Toast.makeText(this, rootPath.getPath(), Toast.LENGTH_LONG).show();
    File dataFile = new File(rootPath, "picture.png");

    iv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    iv.layout(0, 0, iv.getMeasuredWidth(), iv.getMeasuredHeight());

    iv.setDrawingCacheEnabled(true);
    Bitmap b1 = Bitmap.createBitmap(iv.getDrawingCache());
    iv.setDrawingCacheEnabled(false);

    iv2.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    iv2.layout(0, 0, iv2.getMeasuredWidth(), iv2.getMeasuredHeight());

    iv2.setDrawingCacheEnabled(true);
    Bitmap b2 = Bitmap.createBitmap(iv2.getDrawingCache());
    iv2.setDrawingCacheEnabled(false);        

    Bitmap bmOverlay = Bitmap.createBitmap(b1.getWidth(), b1.getHeight(), b1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

    canvas.drawBitmap(b1, 0, 0, null);
    canvas.drawBitmap(b2, 0, 0, null);

    try {
        FileOutputStream out = new FileOutputStream(dataFile, false);
        bmOverlay.compress(CompressFormat.PNG, 95, out);
    } catch (IOException e) {
        e.printStackTrace();
    }

您能告诉我如何调整最终位图文件的位置,以使 ImageView 与它在应用程序上显示的位置相同吗?

谢谢。

【问题讨论】:

  • @Kintaro 你能帮我解答这个问题吗?
  • 我知道这个问题很久以前就被问过了,但也许你仍然可以帮助我。我正在尝试使用您的“合并”方法,但我的问题是 .Compress 方法要求 System.IO.Stream 而不是 Java.IO.FileOutputStream

标签: android canvas imageview overlay


【解决方案1】:

只需创建一个FrameLayout,并在其中包含两个ImageViews。它会自然地将第一张图片与第二张重叠。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/main_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/main" />

    <ImageView
        android:id="@+id/overlay_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/overlay" />

</FrameLayout>

您可以将重力应用到中心或以其他方式对齐图像。

【讨论】:

  • 感谢您的回复。我的问题是最终的位图(bmOverlay)包含两个 imageViews 但顶部的 imageViews 始终位于位图的左上角。你的意思是 framelayout 会自动为我做这件事(当我从 framelayout 创建位图时)?
  • 如果你把两个孩子放在框架布局中,他们会完全重叠,除非你添加其他布局属性导致这种情况不会发生(比如让容器 match_parent,并在每个上设置不同的重力孩子)。
猜你喜欢
  • 1970-01-01
  • 2014-02-28
  • 1970-01-01
  • 2012-11-03
  • 2019-10-14
  • 1970-01-01
  • 2021-02-01
  • 2011-03-22
  • 2018-08-12
相关资源
最近更新 更多