Android上的Alpha渐变

时间:2013-01-15 19:59:34

标签: android imageview gradient android-imageview alpha-transparency

我需要在ImageView的边缘创建一个alpha渐变。最好只使用XML。

Image for example

4 个答案:

答案 0 :(得分:18)

这里的解决方案

图片如下图所示

enter image description here

代码

  • 准备形状可绘制

<强> RES /抽拉/ gradient_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
            android:startColor="#00FFFFFF"
            android:endColor="#FFFFFFFF"
            android:type="linear" />
</shape>

定义布局:activity_main.xml:

<ImageView
    android:id="@+id/photo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/photo"
    android:src="@drawable/gradient_shape" />

这里drawable / photo只是jableg in drawables文件夹

修改  有关更多信息,请参阅 Tutorial

答案 1 :(得分:3)

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <gradient
                android:startColor="#00FFFFFF"
                android:endColor="#FFFFFFFF"
                android:type="linear" />
    </shape>

完美的跨性别。确保下面的视图渐变实际位于下方,而不仅仅是触摸渐变形状视图。

如果你有 RelativeLayout, ListView 查看(背景:渐变)

确保ListView上的渐变视图位于顶部,而不是侧面。如果您的渐变放在一边,它将通过 RelativeLayout 背景颜色而不是 ListView 透明。

我希望你明白我想说的话。

答案 2 :(得分:0)

为了透明:

android:startColor="@null"

或者您可以使用8位数的十六进制颜色:

android:startColor="#FF000000"

前两个FF用于颜色的Alpha,00完全透明而FF不透明

答案 3 :(得分:0)

如果想要无色透明,则需要编写自定义ImageView。

如果您的背景是静态颜色,这是我首选的解决方法:

// when specifying pos
const bool LinkedList::addNode(int val, unsigned int pos) {
    pos = pos == -1 ? getSize() : pos;
    //whatever
}

// when not specifying pos, using getSize() instead
const bool LinkedList::addNode(int val) {
    return addNode(val, getSize());
}