2015-04-04 14 views
21

Używam następujący kształt w mojej aplikacjiWsady na kształt z przezroczystym tłem

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

To nie w moim rozciągliwej folderu. Teraz aktualizuję swoją aplikację do Lollipop i chcę podać informację zwrotną o okrągłym przycisku, którego użyłem. Więc w drawable-v21 folderu zmieniłem go na tętnienia selektora:

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

Ale niestety efekt tętnienia nie jest generowany przy użyciu wyżej rozciągliwej w Lollipop. Czy to z powodu <solid android:color="@android:color/transparent"/>?

Czy ktoś może mi pokazać, gdzie się zepsułem? Dzięki

+0

ten sam problem z ' 'używane jako maska ​​w marszczyć. Wydaje się nie uwzględniać kształtu przedmiotu wewnętrznego, jeśli jest on przezroczysty. \ n \ twoje rozwiązanie używa '@android: color/white' zamiast' transparent' i to właśnie działa, jak sądzę. – John

Odpowiedz

44

Po kilku próbach i błędach wydaje mi się, że źle zrozumiałem hierarchię selector i item.

Poniższe działa idealnie.

<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

dziękuję za odpowiedź na własne pytanie – defhlt

+2

To nie jest przezroczyste tło. Czy udało Ci się ustawić marszczyć z przezroczystym tłem? –

+2

@anoniim Działa na przezroczystym tle http://stackoverflow.com/questions/30215663/ripple-effect-in-pressed-stransparency-in-normal-state/35906753#35906753 – thuongle

7

Działa z przezroczystym tłem:

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

ten został zmodyfikowany nieco od tej odpowiedzi: Transparent ripple

+0

To jest to, czego szukam. Dziękuję za tę odpowiedź – AndEngine

1

Następujący kod działa na niestandardowym kształcie z odzew na przezroczystym przycisku -

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> 
Powiązane problemy