【问题标题】:How to achieve such long pressed ripple effect如何实现如此长按的涟漪效果
【发布时间】:2017-05-28 21:39:22
【问题描述】:

我对如何实现普通媒体的涟漪效果有一些基本的了解。

How to set state_selected in ripple drawable

我在想,我怎么能达到这样长按的涟漪效果。

  1. 按下时,整个项目都会突出显示。
  2. 长按一段时间后,波纹开始从手指按下的区域开始传播。

很难从措辞上清楚地描述出来。我附上一个视频来展示这种效果。

https://www.youtube.com/watch?v=ebOYnGM0HCc

请问如何实现这种长按的涟漪效果?


这是正常的波纹效果的样子:https://www.youtube.com/watch?v=OJ_WRFy7pWM

这是用普通方法实现的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="demo.org.myapplication.MainActivity">

    <View
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:clickable="true"
        android:background="?attr/selectableItemBackground" />

</LinearLayout>

【问题讨论】:

  • 你试过selectableItemBackgroundBorderless吗?

标签: android


【解决方案1】:

这是材料设计中涟漪效应本身的默认行为。您可以使用 xml 添加此效果,也可以通过添加为视图 ?attr/selectableItemBackground 的背景或前景来添加此效果。请记住,要让它工作,视图必须具有 onClickListener 设置。否则,您将看不到效果。

【讨论】:

  • 但是这种涟漪效应只有在你抬起手指时才会发生。只要您的手指按在项目上,波纹就不会发生。该项目仅显示为突出显示。
  • 我再发一个视频,看看?attr/selectableItemBackground实现的正常涟漪效果是什么
【解决方案2】:

你可以计算视图的宽度和高度,你可以像下面的代码一样开始动画。这段代码将从中间开始动画。

  button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            int finalRadius=(int)Math.hypot(v.getWidth()/2,v.getHeight()/2);
            Animator anim= null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                anim = ViewAnimationUtils.createCircularReveal(v,v.getWidth()/2,v.getHeight()/2,0,finalRadius);
                anim.setDuration(400);

                anim.start();
            }
            return true;
        }
    });

【讨论】:

    猜你喜欢
    • 2021-02-25
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多