聊一聊Android中的StateListAnimator
簡(jiǎn)評(píng):使用 StateListAnimator 輕松實(shí)現(xiàn) Material Design 效果。
Material Design 中最基礎(chǔ)的一條原則就是 'motion provides meaning',也就是當(dāng)用戶和你的 app 交互時(shí)應(yīng)當(dāng)提供合理的視覺反饋。標(biāo)準(zhǔn)做法是使用官方提供的 StateListDrawable 來為控件實(shí)現(xiàn)交互效果。
StateListAnimator 是和 Material Design 一同在 API 21 引入的,可以用來方便的實(shí)現(xiàn)交互反饋的視覺效果,今天這篇文章就講解了 StateListAnimator 的用法。
在以前,我們處理 Button,TextView 等控件的點(diǎn)擊效果時(shí),通常是定義一個(gè) selector,為按下和普通情況分別設(shè)置顏色。但這樣的效果一方面不夠動(dòng)人,另一方面也不符合 Material Design 的規(guī)范。
Material Design 規(guī)范推薦 Button 等控件應(yīng)當(dāng)以材質(zhì)的方式表現(xiàn),當(dāng)接觸到手指時(shí)上升。因此 Material Design 對(duì)組件有了 z 軸這個(gè)概念,也就是高度。z 值越大,組件離界面底層(水平面)越遠(yuǎn),投影越重。
那我們?cè)趺磥韺?shí)現(xiàn)組件在 z 軸(高度)上的變化效果呢?這就需要用到今天講到的 StateListAnimator 了。
首先,讓我們創(chuàng)建一個(gè) animator 資源文件夾,在其中創(chuàng)建一個(gè) selector_animator.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <set> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleX" android:valueTo="1.025" android:valueType="floatType" /> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleY" android:valueTo="1.025" android:valueType="floatType" /> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="translationZ" android:valueTo="4dp" android:valueType="floatType" /> </set> </item> <item> <set> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleX" android:valueTo="1.0" android:valueType="floatType" /> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="scaleY" android:valueTo="1.0" android:valueType="floatType" /> <objectAnimator android:duration="@android:integer/config_shortAnimTime" android:propertyName="translationZ" android:valueTo="0dp" android:valueType="floatType" /> </set> </item> </selector>
代碼很簡(jiǎn)單,當(dāng)處于按下情況時(shí),組件沿 x, y 軸擴(kuò)大 1.025 倍并升高 4dp(會(huì)在組件四周產(chǎn)生投影效果)。
需要注意其中的 propertyName 屬性目前支持:
- translationX, translationY: 控制組件沿 x 和 y 軸移動(dòng)多少距離。
- rotation, rotationX, rotationY: 繞中心點(diǎn)旋轉(zhuǎn),設(shè)置 rotation 是 2D 平面旋轉(zhuǎn),rotationX 和 rotationY 分別是從屏幕內(nèi)向屏幕外旋轉(zhuǎn)和從左到右旋轉(zhuǎn),均為 3D 旋轉(zhuǎn)。
- scaleX, scaleY: 沿 x, y 軸的縮放比例,設(shè)置為 1.5 即 1.5 倍。
- pivotX, pivotY: 設(shè)置組件的中心點(diǎn)在哪里,scale 和 rotation 都會(huì)根據(jù)設(shè)置的中心點(diǎn)來變化,默認(rèn)為幾何中心。
- x, y: 組件最終要出現(xiàn)在相對(duì)其父容器的位置。
- alpha: 組件的透明度,值的范圍從 0 到 1,為 0 時(shí)完全透明。
然后在 layout 文件中設(shè)置組件的 stateListAnimator 值就可以啦:
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" ... android:stateListAnimator="@animator/selector_animator" />
因?yàn)橹醪恢С?gif,感興趣的同學(xué)可以點(diǎn)擊原文鏈接查看實(shí)現(xiàn)效果。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼
這篇文章主要介紹了Android 自定義view實(shí)現(xiàn)進(jìn)度條加載效果實(shí)例代碼,需要的朋友可以參考下2017-08-08Android 將文件下載到指定目錄的實(shí)現(xiàn)代碼
本文通過實(shí)例代碼給大家介紹了android將文件下載到指定目錄的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-06-06Android實(shí)現(xiàn)Service重啟的方法
這篇文章主要介紹了Android實(shí)現(xiàn)Service重啟的方法,涉及Android操作Service組件實(shí)現(xiàn)服務(wù)重啟的功能,需要的朋友可以參考下2015-05-05Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽功能
這篇文章主要介紹了Android控件Spinner實(shí)現(xiàn)下拉列表及監(jiān)聽功能,這是在Web開發(fā)中一個(gè)必不可少的交互性組件,而在Android中的對(duì)應(yīng)實(shí)現(xiàn)就是Spinner。需要的朋友可以參考下2018-07-07Android 給圖片加上水印的示例代碼(支持logo+文字)
本篇文章主要介紹了Android 給圖片加上水印的示例代碼(支持logo+文字),具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解
RecyclerView實(shí)現(xiàn)滾動(dòng)相信對(duì)大家來說都不陌生,但是本文主要給大家介紹了利用Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的思路和方法,可以實(shí)現(xiàn)翻頁(yè)功能,一次翻一頁(yè),也可以實(shí)現(xiàn)翻至某一頁(yè)功能。文中給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒,下面來一起看看吧。2017-04-04Android快速開發(fā)系列 10個(gè)常用工具類實(shí)例代碼詳解
今天特此整理出10個(gè)基本每個(gè)項(xiàng)目中都會(huì)使用的工具類,用于快速開發(fā),對(duì)android開發(fā)常用工具類感興趣的朋友跟隨小編一起看看吧2018-09-09Android自定義View展開菜單功能的實(shí)現(xiàn)
這篇文章主要介紹了Android自定義View展開菜單功能的實(shí)現(xiàn),需要的朋友可以參考下2017-06-06