Android 水波紋點擊效果(Ripple Effect)

來源:互聯網
上載者:User

標籤:

 

上周Android發布了Android M的Preview版本.但想必Android5.0很多炫酷效果,多數開發人員還沒有使用過,那更不要說廣大使用者了.

本文介紹的是Android5.0中其中一個炫酷的效果,點擊水波紋擴散效果(Ripple Effect).

以下介紹的實現方式都是調用Android5.0的新API,並非自訂實現,所以支援在Android5.0的裝置.

而大家想相容低系統版本的話,就需要建立v21(即Android5.0)的Resource Directory.

 

圓角背景的水波紋效果(如)

1. 定義一個普通圓角背景的xml;

rounded_corners.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="#FFFFFF" />    <corners android:radius="4dp" /></shape>

 

2. 這裡是重點,<ripple>是API21才有的新Tag,正是實現水波紋效果的;

    其中<ripple android:color="#FF21272B" .... />這個是指定水波紋的顏色.

    而<item />裡面的東西,我們都很熟悉,就是普通的定義一個帶圓角的背景.

ripple_bg.xml:

<?xml version="1.0" encoding="utf-8"?><ripple xmlns:android="http://schemas.android.com/apk/res/android"    android:color="#FF21272B">    <item>        <shape android:shape="rectangle">            <solid android:color="#FFFFFF" />            <corners android:radius="4dp" />        </shape>    </item>    <item android:drawable="@drawable/rounded_corners" /></ripple>

 

3. 這是Activity的布局xml;

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/ripple_bg"        android:text="@string/hello_world" /></LinearLayout>

 

Android 水波紋點擊效果(Ripple Effect)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.