Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
265 views
in Technique[技术] by (71.8m points)

Android Dynamic Transparent Image Overlay

I need to have a transparent image overlaid on top of the real image I want the user to see. The images will be stored on the SD card. I have seen many tutorials on doing this, but they always seem to fall short of just how do I get the two images displayed. Or maybe I'm just missing it. Anyway, here is my layout

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

and here is my code:

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

I am pretty sure my error is in my code.

Edit1 I have done some testing and it does appear that the xml layout is ok. I can show the visible image by itself, but when I show the second image (the transparent image), all I get is an empty, all black display.

To show the second image, do I just call setImageBitmap() a second time? I am beginning to think I need to do something else.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured it out. I needed to remove the android:background="#FF000000" from the layout xml.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...