1

I do have some basic understanding on how to achieve ripple effect for normal press.

How to set state_selected in ripple drawable

I was wondering, how can I achieve such long pressed ripple effect.

  1. Entire item is being highlighted when being pressed.
  2. After long pressed a while, the ripple starts to propagate starting from the area of finger pressed.

It is pretty hard to describe clearly from wording. I attach a video to show such effect.

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

May I know how to achieve such long pressed ripple effect?


This is how a normal ripple effect looks like : https://www.youtube.com/watch?v=OJ_WRFy7pWM

which is achieved using common method.

<?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>
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

2 Answers2

0

That is the default behaviour of the ripple effect itself for material design. You can add this effect using xml or just the simple way by adding as background or foreground of a view ?attr/selectableItemBackground. Remember that to allow it to work it is necessary that the view has an onClickListener setup. Otherwise, the effect will not be visible to you.

droidpl
  • 5,872
  • 4
  • 35
  • 47
  • But such ripple effect only happen, when you lift up your finger. Ripple doesn't happen, as long as your finger is pressed on the item. The item is only shown as highlighted. – Cheok Yan Cheng May 28 '17 at 21:50
  • I post another video, to show what is the normal ripple effect achieved by `?attr/selectableItemBackground` – Cheok Yan Cheng May 28 '17 at 22:01
0

You can calculate the width and height of the view and you can start animation like below code.This code will start animation from the middle.

  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;
        }
    });
Rohan Sharma
  • 374
  • 4
  • 11