學(xué)習(xí)資料:Android開發(fā)藝術(shù)探索
和Android應(yīng)用開發(fā)之所有動畫使用詳解
1.Android中的三種動畫
- View動畫
通過場景里的對象不斷做圖像變換(平移借跪,縮放膏燕,旋轉(zhuǎn)秋泳,透明度)從而產(chǎn)生動畫效果晕粪,是一種漸近式動畫颠蕴,并支持自定義。
- 幀動畫
幀動畫其實也屬于
View
動畫辈讶。通過順序播放一系列圖像從而產(chǎn)生動畫效果命浴,可以簡單理解為圖片切換動畫效果,但圖片過多過大會導(dǎo)致OOM
- 屬性動畫
屬相動畫通過動態(tài)地改變對象的屬性從而達到動畫效果贱除。
重點在于屬性動畫的學(xué)習(xí)
2.View動畫
View
動畫的作用對象是View
生闲。支持四種典型動畫效果:
- 平移動畫
TranslateAnimation
- 縮放動畫
ScaleAnimation
- 旋轉(zhuǎn)動畫
RotateAnimation
- 透明度動畫
AlphaAnimation
對于
View
動畫,建議采用xml
來定義動畫月幌,這樣可讀性更好
View
動畫的四種變換
名稱 | 標(biāo)簽 | 子類 | 效果 |
---|---|---|---|
平移動畫 | <translate> |
TranslateAnimation |
移動View
|
縮放動畫 | <scale> |
ScaleAnimation |
放大或縮小View
|
旋轉(zhuǎn)動畫 | <rotate> |
RotateAnimation |
旋轉(zhuǎn)View
|
透明度動畫 | <alpha> |
AlphaAnimation |
改變View 的透明度 |
Animation
屬性:
xml屬性 | jav代碼 | 作用 |
---|---|---|
android:detachWallpaper |
setDetachWallpaper(boolean) |
是否在壁紙上運行 |
android:duration |
setDuration(long) |
動畫的持續(xù)時間 |
android:fillAfter |
setFillAfter(boolean) |
動畫結(jié)束后是否停留在結(jié)束位置 |
android:fillBefore |
setFillBefore(boolean) |
動畫結(jié)束時是否還原開始位置 |
android:fillEnabled |
setFillEnabled(boolean) |
同上碍讯,與fillBefore相同 |
android:interpolator |
setInterpolator(Interpolator) |
設(shè)置插值器 |
android:repeatCount |
setRepeatCount(int) |
重復(fù)次數(shù) |
android:repeatMode |
setRepeatMode(int) |
有兩種重復(fù)類型,reverse 倒序回放扯躺,restart 從頭播放 |
android:startOffset |
setStartOffset(long) |
開啟動畫startAnimation(animation) 之后等待執(zhí)行運行動畫的時間 |
android:zAdjustment |
setZAdjustment(int) |
表示被設(shè)置動畫的內(nèi)容運行時在Z軸上的位置(top/bottom/normal )冲茸,默認(rèn)為normal
|
View
動畫既可以是單個動畫,也可以是一些列動畫組成缅帘。
<set>
標(biāo)簽標(biāo)示動畫集合,對應(yīng)于AnimationSet
類难衰,可以包含若干動畫钦无,也可以嵌套其他的動畫集合。
android:interpolator
動畫集合所采用的的插值器盖袭,插值器影響動畫的速度失暂,比如非勻速動畫就需要通過插值器來控制動畫的播放過程。默認(rèn)為@android:anim/accelerate_decelerate_interpolator
,即加速加速插值器鳄虱。
-
android:shareInterpolator
集合中的動畫是否和集合共享同一個插值器弟塞。如果集合不指定插值器,子動畫就需要單獨指定所需的插值器或者使用默認(rèn)值拙已。
2.1TranslateAnimation
平移動畫
可以簡單實現(xiàn)抖動效果决记,轉(zhuǎn)成
gif
掉幀有點嚴(yán)重,沒有抖起來
在res
下創(chuàng)建anim
文件夾倍踪,文件名translate_animation.xml
xml
文件代碼:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false"
android:duration="100"
android:repeatMode="restart"
>
<translate
android:repeatCount="2"
android:fromXDelta="-10"
android:fromYDelta="-5"
android:toXDelta="10"
android:toYDelta="5" />
</set>
java
代碼:
Button bt = (Button) view.findViewById(R.id.bt_translate_fragment_translate);
ImageView iv = (ImageView)view.findViewById(R.id.iv_translate_fragment_translate);
//初始化動畫
Animation animation = AnimationUtils.loadAnimation(context, R.anim.translate_animation);
//點擊按鈕開始動畫
bt.setOnClickListener((v) -> iv.startAnimation(animation));
-
android:fromXDelta
x的起始坐標(biāo)值,可以為數(shù)值系宫、百分?jǐn)?shù)、百分?jǐn)?shù)p
建车。以View
的左上角為坐標(biāo)系原點扩借。負(fù)為左,正為右缤至。
- 數(shù)值: 10表示以當(dāng)前View左上角坐標(biāo)加10px為初始點
- 百分?jǐn)?shù): 50%表示以當(dāng)前View的左上角加上當(dāng)前View寬高的50%做為初始點
- 百分?jǐn)?shù)
p
: 50%p表示以當(dāng)前View的左上角加上父控件寬高的50%做為初始點
-
android:toXDelta
x的結(jié)束坐標(biāo)值 -
android:fromYDelta
y的起始坐標(biāo)值潮罪。負(fù)為上,正為下 -
android:toYDelta
y的結(jié)束坐標(biāo)值
需要注意的是,TranslateAnimation
動畫并不會改變View
的位置布局屬性嫉到。
例如沃暗,利用TranslateAnimation
把一個Button
改變了,點擊移動后的Button
是無效的屯碴,而點擊Button
移動前的原始空白位置會響應(yīng)Button
的點擊事件描睦。
2.2ScaleAnimation
縮放動畫
xml
代碼:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fillAfter="false"
android:repeatMode="reverse">
<scale
android:fromXScale="0.5"
android:fromYScale="0.5"
android:pivotX="-100"
android:pivotY="-100"
android:repeatCount="2"
android:toXScale="1"
android:toYScale="1" />
</set>
android:fromXScale
水平方向的縮放值,數(shù)字代表比例导而。1.0是不縮放android:fromYScale
垂直方向的縮放值android:toXScale
水平方向的結(jié)束值android:toYScale
垂直方向的結(jié)束值android:pivotX
縮放的軸點的x軸的坐標(biāo)忱叭。軸點為View
的左上角android:pivotY
縮放的軸點的y軸的坐標(biāo)
默認(rèn)情況下軸點為
View
的中心點
感覺書上這里和我實際測試有些出入,我感覺默認(rèn)是View
的左上角今艺。不曉得是我哪里搞錯了,希望可以指出韵丑。感覺坐標(biāo)系就是自己上面畫的那個1.0坐標(biāo)圖
。
2.2RotateAnimation
旋轉(zhuǎn)動畫
xml
代碼:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:repeatMode="reverse">
<rotate
android:fromDegrees="0"
android:pivotX="235"
android:pivotY="150"
android:repeatCount="2"
android:toDegrees="360" />
</set>
-
android:fromDegrees
旋轉(zhuǎn)開始的角度 -
android:toDegrees
旋轉(zhuǎn)結(jié)束的角度 -
android:pivotX
旋轉(zhuǎn)的軸點的x坐標(biāo) -
android:pivotY
旋轉(zhuǎn)的軸點的y坐標(biāo)
2.4AlphaAnimation
透明度動畫
xml
代碼:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:repeatMode="reverse">
<alpha
android:fromAlpha="1.0"
android:repeatCount="2"
android:toAlpha="0.1" />
</set>
-
android:fromAlpha
透明度的起始值虚缎,1.0
代表最不透明撵彻,值越小越透明 -
android:toAlpha
透明度的結(jié)束值
3. 幀動畫
幀動畫是順序播放一組預(yù)先定義好的圖片,類似播放電影实牡。需要用到AnimationDrawable
這個類陌僵。
隨便百度的吾王,一點沒有表現(xiàn)出吾王美如畫
。
幀動畫使用步驟:
- 先在
drawable
文件下创坞,定義一個animation-list
文件碗短,文件名字frames_animation.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="@drawable/a"
android:duration="500" />
<item
android:drawable="@drawable/b"
android:duration="500" />
<item
android:drawable="@drawable/c"
android:duration="500" />
</animation-list>
- 將
Drawable
作為View
的背景播放
private void initView() {
ImageView iv = (ImageView) findViewById(R.id.iv_frames_animation_activity);
Button bt_start= (Button) findViewById(R.id.bt_start_frames_animation_activity);
Button bt_stop= (Button) findViewById(R.id.bt_stop_frames_animation_activity);
iv.setBackgroundResource(R.drawable.frames_animation);
AnimationDrawable animation = (AnimationDrawable) iv.getBackground();
bt_start.setOnClickListener((v)-> animation.start());
bt_stop.setOnClickListener((v)->animation.stop());
}
幀動畫使用很簡單,但很容易出現(xiàn)OOM题涨。盡量避免使用較大較多的圖片偎谁。
4.View
動畫的特殊使用場景
View
動畫除了四種基本使用場景外,還可以在ViewGroup
中控制子元素的出場效果纲堵,在Activity
中可以實現(xiàn)不同Activity
之間的切換效果巡雨。
4.1 LayoutAnimation
簡單介紹
LayoutAnimation
作用于ViewGroup
,為ViewGroup
指定一個動畫席函,這樣當(dāng)它的子元素出場時铐望,都會具有這種動畫效果。這種效果常常用于ListView
上茂附。
挖坑:RecyclerView
子item
的動畫效果如何實現(xiàn)蝌以?
使用步驟:
1.指定子View
的動畫
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:repeatMode="reverse">
<rotate
android:fromDegrees="0"
android:pivotX="235"
android:pivotY="150"
android:repeatCount="2"
android:toDegrees="360" />
</set>
用的是2.2
旋轉(zhuǎn)的動畫
2. 定義LayoutAnimation
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="@anim/rotate_animation"
android:animationOrder="reverse"
android:delay="1" />
android:animation
指定子元素入場顯示的動畫
android:animationOrder
子元素動畫的順序。有:
nomal
,reverse
,random
nomal
順序顯示何之,排在前面的子元素先顯示動畫跟畅;
reverse
逆序顯示,排在后面的子元素先顯示動畫溶推;
random
隨機顯示子元素的動畫
android:delay
子元素開始動畫的時間延遲徊件。比如子元素的入場動畫周期為300ms奸攻,0.5就表示每個子元素都需要延遲150ms。第一個進來延遲150ms虱痕,播放入場動畫睹耐,第二個子元素延遲300ms播放入場動畫。依次類推部翘。
3.ViewGroup
使用LayoutAniimation
采用布局文件的形式硝训,指定android:layoutAnimation
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layoutAnimation="@anim/layout_anim"
android:orientation="vertical"
>
<ImageView style="@style/img" />
<ImageView style="@style/img" />
<ImageView style="@style/img" />
</LinearLayout>
也可以通過代碼來實現(xiàn):
Animation animation= AnimationUtils.loadAnimation(context,R.anim.resId);
LayoutAnimationController controller = new LayoutAnimationController(animation);
controller.setDelay(1);
controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
目標(biāo)ViewGroup.setLayoutAnimation(controller);
4.2Activity
的切換效果
使用overidePendingTransition(int enterAnim, int exitAnim)
可以改變Activity
的的默認(rèn)切換效果。這個方法 必須在startActivity()
或者finish()
之后才有效果新思。
enterAnim
Activity
被打開時所需的動畫資源id
exitAnim
Activity
被暫停時所需的動畫資源id
啟動一個Activity
時:
Intent intent = new Intent(MainActivity.this, activity);
startActivity(intent);
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
退出一個Activity
時:
@Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.enter_anim,R.anim.exit_anim);
}
5.最后
下篇學(xué)習(xí)屬性動畫窖梁。周末填4.1
挖的坑,學(xué)習(xí)RecyclerView
如何為子item
添加動畫效果