Android动态透明图像叠加

时间:2012-02-27 02:48:23

标签: android android-layout transparency android-image

我需要在我希望用户看到的真实图像上覆盖透明图像。图像将存储在SD卡上。我已经看过许多关于这样做的教程,但是他们似乎总是不知道如何显示这两个图像。或者也许我只是错过了它。无论如何,这是我的布局

<merge 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/gallerylayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView
    android:id="@+id/visible_image"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
  />
  <ImageView
    android:id="@+id/colormap_overlay"
    android:background="#FF000000"      
    android:scaleType="fitXY"
    android:layout_alignTop="@id/visible_image"
    android:layout_alignBottom="@id/visible_image"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
  />
</merge>

这是我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ctx = getApplicationContext();
    setContentView(R.layout.imagepage);
    String image_overlay = Environment.getExternalStorageDirectory() + "/" + s(R.string.APP_NAME) + "/overlay.jpg";
    String visible_image = Environment.getExternalStorageDirectory() + "/" + s(R.string.APP_NAME) + "/visible.jpg";

    ImageView image = (ImageView)findViewById(R.id.visible_image);
    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap bm = BitmapFactory.decodeFile(visible_image, options);
    image.setImageBitmap(bm);

    ImageView overlayimage = (ImageView)findViewById(R.id.colormap_overlay);
    Bitmap bm2 = BitmapFactory.decodeFile(image_overlay);
    overlayimage.setAlpha(0);
    overlayimage.setImageBitmap(bm2);
    }

我很确定我的错误出现在我的代码中。

EDIT1 我做了一些测试,确实看起来xml布局没问题。我可以自己显示可见图像,但是当我显示第二张图像(透明图像)时,我得到的只是一个空的全黑显示。

要显示第二个图像,我是否只是第二次调用setImageBitmap()?我开始认为我需要做别的事。

1 个答案:

答案 0 :(得分:1)

我明白了。我需要从布局xml中删除android:background =“#FF000000”。