在imageview上添加半透明叠加层

时间:2017-11-07 06:23:53

标签: android imageview transparency

如何在imageview上添加半透明叠加?

enter image description here

将其更改为

enter image description here

7 个答案:

答案 0 :(得分:24)

像这样设置你的imageview ::

<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_parent"
android:layout_height="wrap_parent"
android:foreground="@drawable/image_overlay"
android:src="@drawable/your_image" />

image_overlay.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#401C87C0"
        android:centerColor="#401C87C0"
        android:endColor="#401C87C0"
        />
</shape>

我使用color-code =#1C87C0并将其设置为25%不透明度,因此最终的颜色代码类似于#401C87C0。您可以根据您的要求::

更改颜色不透明度,从而更改颜色定义中的前2个字符
100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8

90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF

80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5

70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C

60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82

50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69

40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F

30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36

20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C

10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00 

答案 1 :(得分:8)

您可以在FrameLayout中包围ImageView。

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="@color/semi_transparent_color">

        <ImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/your_image" />
    </FrameLayout>

你可以在这里使用半透明颜色,可能是#22000000。

答案 2 :(得分:2)

只需在android:alpha="0.6"中添加类似此ImageView的Alpha,并在FrameLayout blue=#2196F3中指定一种基色。

<FrameLayout
     android:background="#2196F3"
     android:layout_width="..."
     android:layout_height="...">

     <ImageView
         android:alpha="0.6"
         android:layout_width="..."
         android:layout_height="..."
         android:src="@drawable/image" />

</FrameLayout>

Alpha值范围从1.00.0。举个例子:

  • 1.0不透明
  • 0.5是半透明的
  • 0.0完全透明

答案 3 :(得分:1)

对我来说,所选答案并没有奏效。但是我使用layer-list.得到了它。你所要做的就是创建一个过滤文件(有你最顶层的颜色),一个可绘图层的文件(加入滤镜颜色和图像)并在图像视图中使用此图层绘制 filter.xml

<shape
    android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

    <gradient
        android:startColor="#cf64b5f6"
        android:endColor="#cf1E88E5"
        android:angle="270"
        />

</shape>
  • layerlist_drawable.xml

    <layer-list
        xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/my_img" />
        <item android:drawable="@drawable/filter" />
    </layer-list>
    
  • main_activity.xml:

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:scaleType="centerCrop"
        android:src="@drawable/layerlist_drawable"
        />
    

答案 4 :(得分:0)

添加一个简单的View作为FrameLayout中的最后一个子项,并将其背景设置为半透明颜色。适当地设置叠加层的可见性(View.VISIBLEView.GONE)。

答案 5 :(得分:0)

一个更简单的解决方案是在带有PorterDuff.Mode.SRC_OVER的ImageView上使用ColorFilter

ImageView.setColorFilter(ContextCompat.getColor(
        context,
        R.color.black_opacity_200),
        PorterDuff.Mode.SRC_OVER
);

如果要去除覆盖层,只需清除它

ImageView.clearColorFilter()

答案 6 :(得分:0)

如果要在Imageview上方添加透明叠加层,可以使用以下方法:

imageView.setColorFilter(getResources().getColor(R.color.color_black_view), PorterDuff.Mode.SRC_OVER);

无需在视图上方添加任何其他视图,也无需添加任何Alpha。

相关问题