Android: Overlay a picture (jpg) with transparency

匿名 (未验证) 提交于 2019-12-03 02:44:02

问题:

I have a picture (jpg) that I want to display on the screen. Additionally the picture should be covered partially by a transparent effect. The transparent cover should be dynamic. So e.g. each day more of the picture is shown. Here a picture to show what I mean:

I have the picture without the gray cover and want to add this cover but in different steps.

Can someone give me a hint how to do that.

回答1:

You can do this simply with widgets:

FrameLayout is the general mechanism for overlaying a view on top of another:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="wrap_content"     android:layout_height="wrap_content"> <ImageView       android:id="@+id/image"     android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:src="@drawable/my_image"/> <View     android:id="@+id/overlay"     android:layout_width="fill_parent"     android:layout_height="fill_parent"/> </FrameLayout> 

Then in your Java code you can dynamically set the transparency of your overlay:

View overlay = (View) findViewById(R.id.overlay); int opacity = 200; // from 0 to 255 overlay.setBackgroundColor(opacity * 0x1000000); // black with a variable alpha FrameLayout.LayoutParams params =     new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, 100); params.gravity = Gravity.BOTTOM; overlay.setLayoutParams(params); overlay.invalidate(); // update the view 


回答2:

You can do something like this where the canvas is coming from an onDraw() request:

Paint paint = new Paint(); Rect r = new Rect();  paint.setColor(0x80808080); // translucent gray  r.right = iDisplayWidth-1;  r.left = 0;  r.top = iDisplayHeight/2;  r.bottom = iDisplayHeight-1;  canvas.drawRect(r, paint); // fill with gray 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!