引言
學(xué)習(xí)完上一篇Android 開發(fā)】補(bǔ)間動(dòng)畫(Tween animation)詳解,我們最后來學(xué)習(xí)屬性動(dòng)畫。
內(nèi)容簡(jiǎn)概
一、概念及常用方法
二、實(shí)戰(zhàn)案例
三、運(yùn)行效果
具體內(nèi)容
一、概念及常用方法
你可能會(huì)有這樣的疑問:為什么要引入屬性動(dòng)畫?屬性動(dòng)畫是什么?
(一)ValueAnimation
看完文章相信你肯定對(duì)屬性動(dòng)畫有了初步的了解,一般使用ValueAnimator實(shí)現(xiàn)動(dòng)畫分為以下七個(gè)步驟:
| ??????使用步驟 |
|---|
| 1. 調(diào)用ValueAnimation類中的ofInt、ofFloat等靜態(tài)方法實(shí)例化ValueAnimator對(duì)象,并設(shè)置目標(biāo)屬性的屬性名、初始值或結(jié)束值等值。 |
| 2. 調(diào)用addUpdateListener方法設(shè)置屬性變化的監(jiān)聽器。 |
| 3. 創(chuàng)建自定義的Interpolator,調(diào)用setInterpolator設(shè)置自定義的Interpolator。 |
| 4. 創(chuàng)建自定義的TypeEvaluator,調(diào)用setEvaluator設(shè)置自定義的TypeEvaluator。 |
| 5. 在AnimatorUpdateListener 中的實(shí)現(xiàn)方法為目標(biāo)對(duì)象的屬性設(shè)置計(jì)算好的屬性值。 |
| 6. 設(shè)置動(dòng)畫的持續(xù)時(shí)間、是否重復(fù)及重復(fù)次數(shù)等屬性。 |
| 7. 設(shè)置目標(biāo)對(duì)象并啟動(dòng)動(dòng)畫。 |
(二)ObjectAnimation
在大部分的開發(fā)工作中,都會(huì)使用ObjectAnimator而非ValueAnimator實(shí)現(xiàn)我們所需的動(dòng)畫效果。ObjectAnimator是ValueAnimator的子類,不僅繼承了ValueAnimator的所有方法和特性,并且還封裝很多實(shí)用的方法,方便開發(fā)人員快速實(shí)現(xiàn)動(dòng)畫。同時(shí),由于屬性值會(huì)自動(dòng)更新,使用ObjectAnimator實(shí)現(xiàn)動(dòng)畫不需要像ValueAnimator那樣必須實(shí)現(xiàn)ValueAnimator.AnimatorUpdateListener 。
| ??????使用步驟 |
|---|
| 1. 通過調(diào)用ofFloat()、ofInt()等方法創(chuàng)建ObjectAnimator對(duì)象,并設(shè)置目標(biāo)對(duì)象、需要改變的目標(biāo)屬性名、初始值和結(jié)束值; |
| 2. 設(shè)置動(dòng)畫的持續(xù)時(shí)間、是否重復(fù)及重復(fù)次數(shù)等屬性; |
| 3. 啟動(dòng)動(dòng)畫。 |
(三)屬性
| 系統(tǒng)屬性 |
|---|
| alpha |
| scaleX |
| scaleY |
| translationX |
| translationY |
自定義屬性必須實(shí)現(xiàn)set get方法 |
二、實(shí)戰(zhàn)案例
(一)ValueAnimation
它有兩個(gè)子類,分別是TimeAnimator和ObjectAnimator,在此主要介紹它的子類ObjectAnimator。
ValueAnimation案例
(二)ObjectAnimator——實(shí)現(xiàn)透明度、旋轉(zhuǎn)、平移、縮放動(dòng)畫
1. 準(zhǔn)備一張圖片或者直接在xml中設(shè)置有顏色的形狀。
2. 首先配置布局文件,這次就不寫B(tài)utton控件了(懶)。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/v"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="@drawable/homework"/>
</RelativeLayout>
3. MainActivity
通過改變調(diào)用的方法觀察不同的動(dòng)畫。
public class MainActivity extends AppCompatActivity {
View v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v = findViewById(R.id.v);
}
// 透明度
public void test1(){
ObjectAnimator alphyAni = ObjectAnimator.ofFloat(v,"alpha",1,0);
alphyAni.setDuration(1000);
alphyAni.start();
}
// 旋轉(zhuǎn)
public void test2(){
ObjectAnimator rotateAni = ObjectAnimator.ofFloat(v,"rotation",0,360);
rotateAni.setDuration(1000);
rotateAni.start();
}
public void test3(){
ObjectAnimator scaleAni = ObjectAnimator.ofFloat(v,"scaleX",1,2.1f,1,2.1f,1);
scaleAni.setDuration(1000);
scaleAni.start();
// ObjectAnimator scaleAni2 = ObjectAnimator.ofFloat(v,"scaleY",1,2.3f,1,2.3f,1);
// scaleAni2.setDuration(1000);
// scaleAni2.start();
// 同時(shí)動(dòng)畫
// AnimatorSet aSet = new AnimatorSet();
// aSet.playTogether(scaleAni,scaleAni2);
// aSet.play(scaleAni).after(scaleAni2);
// aSet.start();
}
// 平移
public void test4(){
ObjectAnimator transAnim = ObjectAnimator.ofFloat(v,"translationX",v.getTranslationX()+100);
transAnim.setDuration(1000);
transAnim.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
test3();
}
return true;
}
}
4. 我寫的實(shí)戰(zhàn)比較簡(jiǎn)單了,可以看下面的幾篇拓展文章:
三、運(yùn)行效果
-
旋轉(zhuǎn)
-
平移
-
透明度
-
縮放
-
組合(先后)
-
組合(同時(shí))





