Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

Android L ripple touch effect on shape?

I have a rounded rectangle with elevation that casts a shadow, just like in the example here: http://developer.android.com/preview/material/views-shadows.html#shadows

Here is my shape:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/white" />
    <corners android:radius="6dp" />
</shape>

I want to get the nice "ripple" touch effect that everything else has, but when that's set as the view's background, no touch feedback is given. The shape stays white.

So, I made it into a layer-list:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <solid android:color="@android:color/white" />
            <corners android:radius="6dp" />
        </shape>
    </item>
    <item android:drawable="?android:selectableItemBackground" />
</layer-list>

Now I get nice touch feedback when I tap it, but there's a problem.

The outline of the rounded rectangle shape was lost. It still draws the white rounded rectangle fine, but the shadow is cast as a non-rounded rectangle (square edges), and the ripple goes all the way out past the corners:

Bad corners

It doesn't look too horrible on here, but on the device it's pretty ugly and off-putting.

Is there a way to fix this? The Outline section of the first link seems to be what I want, but I can't figure out how to implement it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try that one:

ripple.xml

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="@color/COLOR_WHILE_PRESSING" xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:drawable="@drawable/background"></item>
</ripple>

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <solid android:color="@color/BACKGROUND_COLOR" />
 <corners android:radius="6dp" />
</shape>

Or maybe this post will help you: Android L FAB Button shadow

I explained there, how to implement the new FAB button, I also used the outline for that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...