How to set a ripple effect on textview or imagevie

2019-01-10 03:09发布

I want to set a ripple effect on textview and imageview in Android Studio. How can I do it?

11条回答
ら.Afraid
2楼-- · 2019-01-10 03:37

Please refer below answer for ripple effect.

ripple on Textview or view :

android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackgroundBorderless"

ripple on Button or Imageview :

android:foreground="?android:attr/selectableItemBackgroundBorderless"
查看更多
成全新的幸福
3楼-- · 2019-01-10 03:37

In addition to @Bikesh M Annur's answer, be sure to update your support libraries. Previously I was using 23.1.1 and nothing happened. Updating it to 23.3.0 did the trick.

查看更多
老娘就宠你
4楼-- · 2019-01-10 03:38

Best way is using libraries. This is one of them. Just add its dependency and put below code in xml before each elements that needs ripple effect:

<com.balysv.materialripple.MaterialRippleLayout
android:id="@+id/ripple"
android:layout_width="match_parent"
android:layout_height="wrap_content">
查看更多
冷血范
5楼-- · 2019-01-10 03:41

Or you can try to use this library (android 9+): RippleEffect

Integration

dependencies {
    compile 'com.github.traex.rippleeffect:library:1.3'
}

Usage:

<com.andexert.library.RippleView
  android:id="@+id/more"
  android:layout_width="?android:actionBarSize"
  android:layout_height="?android:actionBarSize"
  android:layout_toLeftOf="@+id/more2"
  android:layout_margin="5dp"
  rv_centered="true">

  <ImageView
    android:layout_width="?android:actionBarSize"
    android:layout_height="?android:actionBarSize"
    android:src="@android:drawable/ic_menu_edit"
    android:layout_centerInParent="true"
    android:padding="10dp"
    android:background="@android:color/holo_blue_dark"/>

</com.andexert.library.RippleView>
查看更多
【Aperson】
6楼-- · 2019-01-10 03:46

addition to above answers is adding focusable to avoid UI editor's warning

android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
查看更多
虎瘦雄心在
7楼-- · 2019-01-10 03:52

You can use android-ripple-background

Start Effect

final RippleBackground rippleBackground=(RippleBackground)findViewById(R.id.content);
ImageView imageView=(ImageView)findViewById(R.id.centerImage);
imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        rippleBackground.startRippleAnimation();
    }
});

Stop animation:

rippleBackground.stopRippleAnimation();
查看更多
登录 后发表回答