Android 为图像视图添加渐变
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23991395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add gradient to imageview
提问by user1163234
I want to add a gradient on the bottom of my image . Something like this :
我想在我的图像底部添加一个渐变。像这样的事情:
I tried something like this but I only get the gradient no image..
我试过这样的东西,但我只得到渐变没有图像..
<ImageView
android:id="@+id/trendingImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/trend_donald_sterling"
android:src="@drawable/trending_gradient_shape"
/>
trending_gradient_shape:
Trending_gradient_shape:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="@android:color/darker_gray"
android:startColor="@android:color/darker_gray" />
<corners android:radius="0dp" />
</shape>
回答by nhaarman
You need two layers: An ImageView
, and a View
on top of that with your gradient as android:background
. Put these two View
s in a FrameLayout
:
您需要两层: AnImageView
和 aView
在您的渐变为 之上android:background
。将这两个View
放在 a 中FrameLayout
:
<FrameLayout
... >
<ImageView
...
android:src="@drawable/trend_donald_sterling" />
<View
...
android:background="@drawable/trending_gradient_shape"/>
</FrameLayout>
回答by Kaustuv
Simply set the alpha value in your gardient.xml:
只需在您的 gardient.xml 中设置 alpha 值:
Your imageView:
您的图像视图:
android:background="@drawable/trend_donald_sterling"
android:src="@drawable/trending_gradient_shape"
Your gradient xml file:
您的渐变 xml 文件:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
In the color value, the first two places after # correspond to the alpha value, while the rest are the actual color value in R G B format, two for each.
在颜色值中,#后面的前两位对应的是alpha值,其余的都是RGB格式的实际颜色值,各两个。
回答by swetabh suman
try using the "foreground" attribute in your imageview
尝试在图像视图中使用“前景”属性
<ImageView
...
android:src="@drawable/trend_donald_sterling"
android:foreground="@drawable/trending_gradient_shape" />
it worked for me.
它对我有用。
回答by Slick Slime
Use android:foreground="..."
instead of android:background="..."
使用android:foreground="..."
代替android:background="..."
Now you won't need to put ImageView and View inside a FrameLayout!
现在您不需要将 ImageView 和 View 放在 FrameLayout 中!
So your final code will be:
所以你的最终代码将是:
ImageView
图像视图
<ImageView
...
android:foreground="@drawable/trend_donald_sterling"/>
Drawable
可绘制
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>
回答by Ashana.Hymanol
this is how im gonna do, i used relative layout as my parent layout, use the following code
这就是我要做的,我使用相对布局作为我的父布局,使用以下代码
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/img_sample"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradiant"/>
<LinearLayout
android:layout_marginLeft="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Events"
android:gravity="bottom"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#ffffff"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.25"
android:text="Some description about the events goes here"
android:textSize="14sp"
android:textColor="#ffffff"/>
</LinearLayout>
</RelativeLayout>
hope you can figure out, here i attach my gradiant code below.use it inside the drawable folder....
希望你能弄清楚,我在下面附上我的渐变代码。在 drawable 文件夹中使用它....
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#00ffffff"
android:startColor="#aa000000"
android:centerColor="#00ffffff" />
<corners android:radius="0dp" />
</shape>