2015-04-04 122 views
21

我使用以下形狀的形狀,我的應用程序漣漪上具有透明背景

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <shape android:shape="oval" > 
      <solid android:color="@color/primary_light"/> 
      <size android:height="80dp" android:width="80dp"/> 
     </shape> 
    </item> 

    <item android:state_focused="true"> 
     <shape android:shape="oval"> 
      <stroke android:color="@color/primary_light" android:width="5dp"/> 
      <size android:height="80dp" android:width="80dp"/> 
     </shape> 
    </item> 

    <item> 
     <shape android:shape="oval"> 
      <solid android:color="@android:color/transparent"/> 
      <size android:height="80dp" android:width="80dp"/> 
     </shape> 
    </item> 
</selector> 

它的存在在我的繪製文件夾。現在我正在將我的應用更新爲棒棒糖,並且希望對我使用的圓形按鈕提供反饋。 所以在drawable-v21文件夾我把它改成一個波紋選擇:

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="@color/primary_light"> 
    <item> 
     <selector> 
      <item android:state_focused="true"> 
       <shape android:shape="oval"> 
        <stroke android:color="@color/primary_light" android:width="5dp"/> 
        <size android:height="80dp" android:width="80dp"/> 
       </shape> 
      </item> 

      <item android:id="@android:id/mask"> 
       <shape android:shape="oval"> 
        <solid android:color="@android:color/transparent"/> 
        <size android:height="80dp" android:width="80dp"/> 
       </shape> 
      </item> 
     </selector> 
    </item> 
</ripple> 

但遺憾的是沒有被使用棒棒糖上面繪製所產生的連鎖反應。是因爲<solid android:color="@android:color/transparent"/>

任何人都可以告訴我我哪裏出錯了嗎?謝謝

+0

曾與'同樣的問題<固體機器人:顏色= 「@機器人:彩色/透明」/> '用作漣漪的面具。如果它是透明的,它似乎沒有考慮內部物品的形狀。 \ n 您的解決方案使用'@android:color/white'而不是'transparent',而這正是它實際上的作用,我想。 – John 2015-09-25 18:47:49

回答

44

經過一些試驗和錯誤,似乎我誤解了selectoritem的層次結構。

以下工作完美。

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
     android:color="@color/primary_light"> 
     <item android:id="@android:id/mask"> 
       <shape android:shape="oval"> 
         <solid android:color="@android:color/white"/> 
         <size android:height="80dp" android:width="80dp"/> 
       </shape> 
     </item> 
</ripple> 
+3

感謝您回答自己的問題 – defhlt 2015-11-17 14:07:59

+2

這不是透明背景..您是否設置了帶有透明背景的波紋? – 2016-02-29 09:03:07

+2

@anoniim它適用於透明背景 http://stackoverflow.com/questions/30215663/ripple-effect-in-pressed-state-transparency-in-normal-state/35906753#35906753 – thuongle 2016-03-10 03:00:00

7

這個工程具有透明背景:

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    android:color="?android:colorControlHighlight" 
    android:exitFadeDuration="@android:integer/config_shortAnimTime"> 
<!-- Use this to define the shape of the ripple effect (rectangle, oval, ring or line). The color specified here isn't used anyway --> 
<item android:id="@android:id/mask"> 
    <shape android:shape="rectangle"> 
     <corners android:radius="3dp"/> 
     <!-- This color is needed to be set - doesn't affect anything --> 
     <solid android:color="@android:color/white"/> 
    </shape> 
</item> 
<!-- This is the background for your button --> 
<item> 
    <!-- Use the shape you want here --> 
    <shape android:shape="rectangle"> 
     <!-- Use the solid tag to define the background color you want --> 
     <solid android:color="@android:color/transparent"/> 
    </shape> 
</item> 
</ripple> 

這是從這個答案修改了一下:Transparent ripple

+0

這就是我正在尋找的。感謝這個答案 – AndEngine 2018-02-15 09:56:16

1

下面的代碼工作的自定義形狀的透明按鈕連鎖反應 -

main_activity_buttons_ripple_with_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:color="#888888" 
    tools:targetApi="lollipop"> 

    <!-- Use this to define the shape of the ripple effect (rectangle, oval, ring or line). The color specified here isn't used anyway --> 
    <item android:id="@android:id/mask"> 
     <shape android:shape="rectangle"> 
      <corners android:radius="25dp" /> 
      <!-- This color is needed to be set - doesn't affect anything --> 
      <solid android:color="#000" /> 
     </shape> 
    </item> 

    <!-- This is the background for your button --> 
    <item> 
     <!-- Use the shape you want here --> 
     <shape android:shape="rectangle"> 
      <!-- Use the solid tag to define the background color you want --> 
      <solid android:color="@android:color/transparent" /> 
      <stroke 
       android:width="1dp" 
       android:color="#FFF" /> 
      <corners android:radius="25dp" /> 
     </shape> 
    </item> 

</ripple> 

styles.xml

<style name="MainActivityButtonsStyle"> 
<item name="android:background">@drawable/main_activity_buttons_ripple_with_background</item> 
<item name="android:textAllCaps">false</item> 
<item name="android:layout_margin">15dp</item> 
<item name="android:paddingLeft">20dp</item> 
<item name="android:paddingRight">20dp</item> 
<item name="android:layout_centerHorizontal">true</item> 
<item name="android:layout_width">wrap_content</item> 
<item name="android:layout_height">50dp</item> 
<item name="android:textColor">#FFF</item> 
<item name="android:textSize">18sp</item> 

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"> 

     <Button 
      android:layout_alignParentTop="true" 
      style="@style/MainActivityButtonsStyle" 
      android:text="@string/button_search_by_id" /> 

    </RelativeLayout> 

</LinearLayout>