渐变笔触与透明背景

问题描述:

我试着去实现这样的事情,我在XML中使用图层列表中这样说:渐变笔触与透明背景

<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> 

这里是布局代码,边界的形状需要重叠图像。

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_marginTop="12dp" 
     android:background="@android:color/transparent"> 


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


     <FrameLayout 
      android:id="@+id/goals_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 

      android:layout_gravity="center" 
      android:clickable="true" 

      android:layout_marginLeft="20dp" 
      android:layout_marginRight="32dp" 
      android:background="#00000000" 
      > 


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

       /> 


     </FrameLayout> 

    </RelativeLayout> 

但如果我将背景设置为透明的形状背景变成了整个渐变颜色。我怎样才能实现具有透明背景的渐变描边?提前致谢。

enter image description here

+0

分享您的布局 – Maddy

+0

@Maddy我编辑的布局代码 – Ahmad

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

入住这

How to make transparent gradient?

入住这也 http://www.coderzheaven.com/2013/02/04/create-transparency-image-android/

+0

我想这是问题以及它仍然无法正常工作。 – Ahmad

编辑检查这个

<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> 
+0

也不起作用 – Ahmad

后试试这个:

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> 
+0

这不起作用 – Ahmad

+0

你在找什么? – Maddy