我想从我的 Android 应用程序的服务器下载一张图片,并在 ImageView
中显示它,顶部有一个黑色图层,具有一定的不透明度。
我能够在 iPhone 上非常轻松地使用 UIView
将 blackColor
作为背景色并将其放在 UIImageView
上方alpha 设置为 .4。在 Android 上有类似的方法吗?我已经阅读了很多这方面的资料,网上的大多数方法都说使用选择器等。
但是当从服务器下载图像然后将黑色 View 放在顶部时,这将不起作用。
最佳答案
您可以从使用 FrameLayout 中获益匪浅.
总而言之,FrameLayout 是一个单一的框架,其中放入的所有内容基本上彼此堆叠(与 LinearLayout 不同,其中内容堆叠在每个框架之上其他)。
设置方式的示例如下:
<!-- This can be whatever your parent view is -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Here is the actual usage of the FrameLayout. Replace the height -->
<!-- and width with the size of the image that you're using -->
<FrameLayout
android:layout_height="100dp"
android:layout_width="100dp">
<!-- You won't use the src tag seeing as you get your image from a -->
<!-- server, but I have it for the sake of demonstration. -->
<ImageView
android:id="@+id/your_image_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"/>
<!-- This is a basic view that has a black background and a .4 alpha -->
<View
android:id="@+id/shadow_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:alpha=".4"
android:background="#000000"/>
</FrameLayout>
</LinearLayout>
这是结果:
出于您自己的目的,您可能需要稍微调整一下高度和宽度属性,否则这应该可以解决问题。
关于Android 下载图片并在 ImageView 中显示黑色阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24637716/