Android 使波纹填充整个视图

Android 使波纹填充整个视图,android,android-drawable,android-resources,ripple,rippledrawable,Android,Android Drawable,Android Resources,Ripple,Rippledrawable,在我的应用程序中,我想创建一个填充整个视图的涟漪。因为它不能正常工作,我创建了一个最小的示例应用程序,并在那里尝试,但没有成功 我的布局如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="ma

在我的应用程序中,我想创建一个填充整个视图的涟漪。因为它不能正常工作,我创建了一个最小的示例应用程序,并在那里尝试,但没有成功

我的布局如下所示:

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

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ripple"
        android:onClick="onViewClicked" />

</LinearLayout>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#1E88E5"
    android:radius="0dp">

    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white" />

</ripple>

我的drawable定义如下:

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

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ripple"
        android:onClick="onViewClicked" />

</LinearLayout>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#1E88E5"
    android:radius="0dp">

    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white" />

</ripple>

这是一个视频,它目前看起来如何。我希望圆圈(我认为它正式地被称为热点)从正中开始增长,直到填满整个
视图。我不是说波纹的半径,这里有意识地设置为0dp。你有什么想法我可以做到这一点吗

编辑:最后,我想实现以下目标


对于每个不想阅读所有答案的人:我设法实现了它,并为此创建了一个新的解决方案。您可以在项目中随意使用它并向我发送请求。

您可以通过创建自定义RippleView来实现此效果。使用
onDraw
方法在全视图上绘制圆,并将动画设置为该圆

if (animationRunning) {
            canvas.save();
            if (rippleDuration <= timer * frameRate) {
                animationRunning = false;
                timer = 0;
                durationEmpty = -1;
                timerEmpty = 0;
                // There is problem on Android M where canvas.restore() seems to be called automatically
                // For now, don't call canvas.restore() manually on Android M (API 23)
                if(Build.VERSION.SDK_INT != 23) {
                    canvas.restore();
                }
                invalidate();
                if (onCompletionListener != null) onCompletionListener.onComplete(this);
                return;
            } else
                canvasHandler.postDelayed(runnable, frameRate);

            if (timer == 0)
                canvas.save();


            canvas.drawCircle(x, y, (radiusMax * (((float) timer * frameRate) / rippleDuration)), paint);

            paint.setColor(Color.parseColor("#ffff4444"));

            if (rippleType == 1 && originBitmap != null && (((float) timer * frameRate) / rippleDuration) > 0.4f) {
                if (durationEmpty == -1)
                    durationEmpty = rippleDuration - timer * frameRate;

                timerEmpty++;
                final Bitmap tmpBitmap = getCircleBitmap((int) ((radiusMax) * (((float) timerEmpty * frameRate) / (durationEmpty))));
                canvas.drawBitmap(tmpBitmap, 0, 0, paint);
                tmpBitmap.recycle();
            }

            paint.setColor(rippleColor);

            if (rippleType == 1) {
                if ((((float) timer * frameRate) / rippleDuration) > 0.6f)
                    paint.setAlpha((int) (rippleAlpha - ((rippleAlpha) * (((float) timerEmpty * frameRate) / (durationEmpty)))));
                else
                    paint.setAlpha(rippleAlpha);
            }
            else
                paint.setAlpha((int) (rippleAlpha - ((rippleAlpha) * (((float) timer * frameRate) / rippleDuration))));

            timer++;
        }
输出:

您可以得到一个从中间一直延伸到整个视图的圆

可点击视图的连锁反应 如果希望涟漪延伸到视图的边界之外,则可以使用
?attr/selectableitemsbackgroundless
。这适用于作为较大视图一部分的图像按钮和较小按钮:

试试这个我希望它对你有用

使用android:前台=“@drawable/ripple”

而不是
android:background=“@drawable/ripple”


同时将android:radius=“0dp”更改为50dp或100dp,并检查实际测试设备上有哪些API?不幸的是,我认为它(RippleDrawable)只适用于API 21+(Android 5.0,棒棒糖)。支持库21仍然无法在旧设备上运行。是的,你是对的。我在API 26奥利奥上测试了它。如果真正的解决方案可以在API 14+上运行,那就太好了。我想我已经看到了一个可以实现这一点的库,如果我找到它,请与您联系。@Cilenco尝试使用这个android:background=“?android:attr/selectableItemBackground”。如果您对库满意,请检查这个库是我测试过的最好的库。易于实现和API 14+。感谢您的回答。我自己设法实现了这一点,并为此创建了一个新的应用程序。我使用了API 11中的动画类,并不像在库中那样完全由我自己计算,所以自己决定哪个版本最适合您。 Most buttons are made with several drawables. Usually you’ll have a pressed and
normal version of assets like this: /drawable/button.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/>
    <item android:drawable="@drawable/button_normal"/>
</selector>
If you have a custom button with selected state, your text color changes
depending on the state, etc. So the default button background is not going
to work for you here. You can add this feedback for your own drawables and
for custom buttons by simply wrapping them in a ripple element: /drawable-v21/button.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item android:drawable="@drawable/button_normal" />
</ripple>
<?xml version="1.0" encoding="utf-8"?>
<resources>

  <style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <item name="android:colorControlHighlight">@color/your_custom_color</item>
  </style>

</resources>