觸摸反饋:
在Android L5.0中加入了觸摸反饋動(dòng)畫(huà)产雹。
其中最明顯,最具代表性的就是波紋動(dòng)畫(huà)翁锡,比如當(dāng)點(diǎn)擊按鈕或條目時(shí)會(huì)從點(diǎn)擊的位置產(chǎn)生類(lèi)似于波紋的擴(kuò)散效果蔓挖。
波紋效果(Ripple):
當(dāng)你使用了Material主題后,波紋動(dòng)畫(huà)會(huì)自動(dòng)應(yīng)用在所有的控件上馆衔,我們當(dāng)然可以來(lái)設(shè)置其屬性來(lái)調(diào)整到我們需要的效果瘟判。
可以通過(guò)如下代碼設(shè)置波紋的背景:
- android:background="?android:attr/selectableItemBackground" 波紋有邊界
- android:background="android:attr/selectableItemBackgroundBorderless" 波紋超出邊界
使用效果如下:
- B1是不設(shè)任何背景的按鈕
- B2設(shè)置了?android:attr/selectableItemBackground
- B3設(shè)置了?android:attr/selectableItemBackgroundBorderless
設(shè)置顏色
我們也可以通過(guò)設(shè)置xml屬性來(lái)調(diào)節(jié)動(dòng)畫(huà)顏色,從而可以適應(yīng)不同的主題:
android:colorControlHighlight:設(shè)置波紋顏色
android:colorAccent:設(shè)置checkbox等控件的選中顏色
比如下面這個(gè)比較粉嫩的主題角溃,就需要修改動(dòng)畫(huà)顏色來(lái)匹配:
Circular Reveal:
Circular Reveal是一個(gè)Android L新增的動(dòng)畫(huà)效果拷获,但我始終不知道如何翻譯這個(gè)名字,圓形揭示减细?
使用方法:
應(yīng)用ViewAnimationUtils.createCircularReveal()方法可以去創(chuàng)建一個(gè)RevealAnimator動(dòng)畫(huà)
ViewAnimationUtils.createCircularReveal源碼如下:
public static Animator createCircularReveal(View view,
int centerX, int centerY, float startRadius, float endRadius) {
return new RevealAnimator(view, centerX, centerY, startRadius, endRadius);
}
源碼非常簡(jiǎn)單匆瓜,就是通過(guò)createCircularReveal方法根據(jù)5個(gè)參數(shù)來(lái)創(chuàng)建一個(gè)RevealAnimator動(dòng)畫(huà)對(duì)象。
這五個(gè)參數(shù)分別是:
- view 操作的視圖
- centerX 動(dòng)畫(huà)開(kāi)始的中心點(diǎn)X
- centerY 動(dòng)畫(huà)開(kāi)始的中心點(diǎn)Y
- startRadius 動(dòng)畫(huà)開(kāi)始半徑
- startRadius 動(dòng)畫(huà)結(jié)束半徑
根據(jù)下面的效果圖和代碼可以很容易的了解這幾個(gè)參數(shù)的作用:
final View oval = this.findViewById(R.id.oval);
oval.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animator animator = ViewAnimationUtils.createCircularReveal(
oval,
oval.getWidth()/2,
oval.getHeight()/2,
oval.getWidth(),
0);
animator.setInterpolator(new AccelerateDecelerateInterpolator());
animator.setDuration(2000);
animator.start();
}
});
final View rect = this.findViewById(R.id.rect);
rect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animator animator = ViewAnimationUtils.createCircularReveal(
rect,
0,
0,
0,
(float) Math.hypot(rect.getWidth(), rect.getHeight()));
animator.setInterpolator(new AccelerateInterpolator());
animator.setDuration(2000);
animator.start();
}
});