android - 使波纹填充整个 View

标签 android android-drawable android-resources ripple rippledrawable

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

我的布局是这样的:

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

我的drawable是这样定义的:

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

Here是一个视频目前的样子。我希望圆圈(我认为它正式称为热点)从中部开始增长,直到填满整个 View。我不是在谈论这里有意设置为 0dp 的波纹半径。您有什么想法可以实现吗?

编辑: 最后我想实现类似 this 的东西.

对于不想阅读所有答案的每个人:我设法实现了它并创建了一个 repo为了这。随意在您的项目中使用它并向我发送拉取请求。

最佳答案

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

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++;
        }

实现是从 RippleEffect Library 窃取的项目。在库项目中找到完整的实现。

要仅从中心点创建圆,请使用以下方法

 /**
     * Launch Ripple animation for the current view centered at x and y position
     *
     * @param x Horizontal position of the ripple center
     * @param y Vertical position of the ripple center
     */
    public void animateRipple(final float x, final float y) {
        createAnimation(x, y);
    }



/**
 * Create Ripple animation centered at x, y
 *
 * @param x Horizontal position of the ripple center
 * @param y Vertical position of the ripple center
 */
private void createAnimation(final float x, final float y) {
    if (this.isEnabled() && !animationRunning) {
        if (hasToZoom)
            this.startAnimation(scaleAnimation);

        radiusMax = Math.max(WIDTH, HEIGHT);

        if (rippleType != 2)
            radiusMax /= 2;

        radiusMax -= ripplePadding;

        if (isCentered || rippleType == 1) {
            this.x = getMeasuredWidth() / 2;
            this.y = getMeasuredHeight() / 2;
        } else {
            this.x = x;
            this.y = y;
        }

        animationRunning = true;

        if (rippleType == 1 && originBitmap == null)
            originBitmap = getDrawingCache(true);

        invalidate();
    }
}

输出:

你可以得到从中间增长的圆圈,直到它填满整个 View enter image description here

关于android - 使波纹填充整个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48034152/

相关文章:

android - 来自可绘制资源 ID #0x7f0601d8 的异常 android.content.res.Resources$NotFoundException : File res/color/selector_tab_blue_white. xml

java - Android 中两个重叠的 ImageView

Android - imageView.getDrawable() 返回 null

android - 在Android中通过FFMpeg实现视频过滤器

android - 使用 AccessibilityService 中的 AccessibilityNodeInfo 执行滚动

java - Android - 确定 Compass 中的内存泄漏 (SensorManager)

android - 圆形微光Drawable(facebook android-shimmer)

java - 你如何获得按钮的背景颜色?

android - 在构建时下载 Android 字符串资源

android - 换出 ListView 项目 View