Gradient stroke with transparent background

时间:2017-12-18 08:18:56

标签: android android-drawable layer-list

Im trying to achieve something like this, I used layer list in the xml this way :

[0, 1, 0, 0, 0, 1]

Here is the layout code, the border of the shape needs to overlap the image.

<item>
    <shape android:shape="rectangle" >
        <gradient

            android:centerColor="#4DD0E1"
            android:endColor="#76FF03"

            android:startColor="@color/colorPrimary"
            android:type="linear" />


    </shape>
</item>

<item
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp"
    android:top="2dp">


    <shape android:shape="rectangle" >

        <solid android:color="#fff" />
    </shape>
</item>

but if I set the background to transparent the shapes background becomes the whole gradient color. How can i achieve a gradient stroke with a transparent background? Thanks in advance.

enter image description here

3 个答案:

答案 0 :(得分:0)

只需使用8位数的颜色值,例如#FFF8F8F6,其中前两个字符是alpha值。 FF完全不透明,00完全透明。

检查

How to make transparent gradient?

也检查一下 http://www.coderzheaven.com/2013/02/04/create-transparency-image-android/

答案 1 :(得分:0)

编辑后检查此

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
<!-- create gradient you want to use with the angle you want to use -->
    <shape android:shape="rectangle" >
        <gradient
            android:angle="0"
            android:centerColor="@android:color/holo_blue_bright"
            android:endColor="@android:color/holo_red_light"
            android:startColor="@android:color/holo_green_light" />

    </shape>
</item>
<!-- create the stroke for top, left, bottom and right with the dp you want -->
<item
    android:bottom="2dp"
    android:left="2dp"
    android:right="2dp"
    android:top="2dp">
    <shape android:shape="rectangle" >
        <!-- fill the inside in the color you want (could also be a gradient again if you want to, just change solid to gradient and enter angle, start, maybe center, and end color) -->
        <solid android:color="#fff" />
    </shape>
</item>

</layer-list>

答案 2 :(得分:0)

试试这个:

enter image description here

  

gradient_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:centerColor="#4DD0E1"
                android:endColor="#76FF03"
                android:startColor="@color/colorPrimary"
                android:type="linear" />
            <corners android:radius="2dp" />
            <padding
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp"
                android:top="2dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/white" />
            <corners android:radius="2dp" />
            <size
                android:width="50dp"
                android:height="50dp" />
        </shape>
    </item>
</layer-list>
  

your_layout.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="12dp"
    android:background="@drawable/gradient_drawable">

    <ImageView
        android:id="@+id/goals_image"
        android:layout_width="100dp"
        android:layout_height="75dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="12dp"
        android:src="@mipmap/ic_launcher" />


    <FrameLayout
        android:id="@+id/goals_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="32dp"
        android:background="#00000000"
        android:clickable="true">

        <TextView
            android:id="@+id/goals_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:text="Lose Weight"
            android:textColor="@color/black"
            android:textSize="15dp" />
    </FrameLayout>
</RelativeLayout>