简体   繁体   中英

How to add the Material design effect to android views?

I am using AppCompat theme to provide material theme to versions prior to Lollipop. When I set a image resource as a Navigation icon or as a Menu button in Toolbar , I get the ripple effect (motion) on clicking them. But when I try to give the same icons inside the toolbar, they behave like ordinary views, there is no motion on click.

How are the ripples created? Is android able to create ripples on all the drawables that I provide? How can I make the ripples appear when I use the image resources in a ImageView ?

您只需在XML布局中设置图像的背景即可创建纹波。
android:background="?android:attr/selectableItemBackground"

Ripple is basically an instance of the RippleDrawable class. You can create ripples using xml and < ripple > tag. The most simple ripple drawable looks like this:

<?xml version="1.0" encoding="utf-8"?>
<ripple 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight" />

You can find more info here: http://blog.stylingandroid.com/ripples-part-1/

You can achive that using ripple effect.

Implementation of Ripple effect from Material Design for Android API 9+.

The lib is available on Maven Central, you can find it with Gradle, please

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

Usage

RippleView

Declare a RippleView inside your XML layout file with a content like an ImageView or whatever.

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

If you want to know when the Ripple effect is finished, you can set a listener on your view

 rippleView.setOnRippleCompleteListener(new  RippleView.OnRippleCompleteListener() {

   @Override
   public void onComplete(RippleView rippleView) {
       Log.d("Sample", "Ripple completed");
   }

 });

For Code and More Information See this link :

https://github.com/traex/RippleEffect

For some more design :

https://github.com/wasabeef/awesome-android-ui

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM