在形状的波纹有透明背景

时间:2015-04-04 10:30:04

标签: android rippledrawable

我在我的应用中使用以下形状

<?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文件夹中。现在我正在将我的应用更新到Lollipop,我希望对我使用的圆形按钮给出反馈。 因此,在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>

但不幸的是,在Lollipop中使用上述drawable并不会产生连锁反应。是因为<solid android:color="@android:color/transparent"/>

有谁能告诉我哪里出错了?感谢

4 个答案:

答案 0 :(得分:50)

经过一些试验和错误后,似乎我误解了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>

答案 1 :(得分:24)

这适用于透明背景:

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

答案 2 :(得分:3)

以下代码适用于自定义形状,带有涟漪效果的透明按钮 -

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>

答案 3 :(得分:2)

指定波纹蒙版时,可以在透明或半透明背景上产生波纹效果。波纹蒙版是一个Drawable,但是仅使用alpha值来设置每个像素的波纹alpha。这是一个以编程方式创建的示例。

var background = new android.graphics.drawable.GradientDrawable();
background.setShape(android.graphics.drawable.GradientDrawable.RECTANGLE);
background.setColor(0x77FFFFFF); // semi-transparent background
background.setCornerRadius(10);
background.setStroke(3, 0xFF1144FF); // border color

var mask = new android.graphics.drawable.GradientDrawable();
mask.setShape(android.graphics.drawable.GradientDrawable.RECTANGLE);
mask.setColor(0xFF000000); // the color is irrelevant here, only the alpha
mask.setCornerRadius(5); // you probably want the same corner as the background

var rippleColorLst = android.content.res.ColorStateList.valueOf(
    android.graphics.Color.argb(255,50,150,255)
);

// aaaand
var ripple = new android.graphics.drawable.RippleDrawable(rippleColorLst,background,mask);
yourButton.setBackground(ripple);

(对不起,JavaScript / NativeScript代码,也许有人可以理解)