Android 向imageview添加渐变
我想在我的图像底部添加一个渐变。大概是这样的: 我尝试过类似的方法,但我只得到了渐变效果,没有图像Android 向imageview添加渐变,android,android-imageview,android-drawable,Android,Android Imageview,Android Drawable,我想在我的图像底部添加一个渐变。大概是这样的: 我尝试过类似的方法,但我只得到了渐变效果,没有图像 <ImageView android:id="@+id/trendingImageView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/trend_donald_sterling"
<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"
/>
趋势梯度形状:
<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>
您需要两层:一层是
图像视图
,另一层是视图
,渐变为android:background
。将这两个视图
放在框架布局中
:
<FrameLayout
... >
<ImageView
...
android:src="@drawable/trend_donald_sterling" />
<View
...
android:background="@drawable/trending_gradient_shape"/>
</FrameLayout>
只需在gardient.xml中设置alpha值: 您的图像视图:
android:background="@drawable/trend_donald_sterling"
android:src="@drawable/trending_gradient_shape"
您的渐变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>
在颜色值中,#后的前两位对应于alpha值,而其余的是R G B格式的实际颜色值,每一位两个。这就是我要做的,
我使用相对布局作为父布局,使用以下代码
<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>
希望您能理解,我在下面附上了我的渐变代码。请在可绘制文件夹中使用它。
<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>
尝试在imageview中使用“前台”属性
<ImageView
...
android:src="@drawable/trend_donald_sterling"
android:foreground="@drawable/trending_gradient_shape" />
这对我很有用。使用
android:front=“…”
而不是android:background=“…”
现在,您不需要将ImageView和View放在FrameLayout中
那么您的最终代码将是:
图像视图
<ImageView
...
android:foreground="@drawable/trend_donald_sterling"/>
可牵引
<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>
**1>创建文件black\u shadow.xml**
布拉
**2> 只需在Imageview的下面添加一行**
android:foreground=“@drawable/black_shadow”
不要忘记为颜色添加alpha值。尝试从android:endColor=“#00000000”android:startColor=“ff000000”开始,并根据您的需要使用它。用户界面需要做的工作太多了。就个人而言,我更喜欢使用单一ImageView的解决方案。我没有做数学运算,但在性能方面似乎更有效这是出于我的目的,但如果你只想让渐变上升到某个高度,在ImageView的顶部有一个视图会更有意义。注意:属性android:前台
对低于23的API级别没有影响(当前最小值为16)。这是IDE向我展示的。注意:属性android:前台对低于23的API级别没有影响(当前最小值为16)。这是IDE向我展示的内容。正如您在我的回答中所看到的,我使用的是android:background
而不是android:foreground
。关于API级别警告的问题,请参考另一个SO问题。但您已经写了:出于某种原因使用android:前台=“…”而不是android:background=“…”
。哦,这是我的错。在这种情况下,您可以将minSdkVersion
更改为22以上,或者按照需要FrameLayout
**1> Create file black_shadow.xml**
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="270"
android:startColor="#00000000"
android:centerColor="#9c000000"
android:endColor="#000000"
android:type="linear" />
</shape>
</item>bla
</selector>
**2> Just add below line in Imageview.**
android:foreground="@drawable/black_shadow"