Android:覆盖透明的图片(jpg)

tob*_*ias 9 transparency android image

我有一张想要在屏幕上显示的图片(jpg).此外,图片应部分由透明效果覆盖.透明的封面应该是动态的.因此,例如每天都会显示更多的图片.这张图片展示了我的意思: 在此输入图像描述

我有没有灰色封面的图片,并希望添加此封面,但步骤不同.

有人可以给我一个提示如何做到这一点.

Mat*_*lis 19

您只需使用小部件即可完成此操作:

FrameLayout是将视图覆盖在另一个视图之上的一般机制:

<?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>
Run Code Online (Sandbox Code Playgroud)

然后在Java代码中,您可以动态设置叠加层的透明度:

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
Run Code Online (Sandbox Code Playgroud)