動畫這塊可謂是經(jīng)常用,但是沒怎么下功夫研究過库北,今天就自己總結(jié)下爬舰,當(dāng)做備忘錄。
是不是看完圖寒瓦,發(fā)現(xiàn)我艸情屹,怎么知識點還那么多,我們就挨個慢慢縷一縷杂腰。
補間動畫
感謝分享
http://www.reibang.com/p/733532041f46
根據(jù)不同的動畫效果垃你,補間動畫分為4種動畫:
平移動畫(Translate)
縮放動畫(scale)
旋轉(zhuǎn)動畫(rotate)
透明度動畫(alpha)
可以從xml讀取,也可以直接代碼上手喂很。
XML寫法
<?xml version="1.0" encoding="utf-8"?>
// 采用<translate /> 標(biāo)簽表示平移動畫
<translate xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動畫持續(xù)時間(ms)惜颇,必須設(shè)置,動畫才有效果
android:startOffset ="1000" // 動畫延遲開始時間(ms)
android:fillBefore = “true” // 動畫播放完后恤筛,視圖是否會停留在動畫開始的狀態(tài)官还,默認(rèn)為true
android:fillAfter = “false” // 動畫播放完后,視圖是否會停留在動畫結(jié)束的狀態(tài)毒坛,優(yōu)先于fillBefore值望伦,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值林说,對fillAfter值無影響,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動畫模式屯伞,restart代表正序重放腿箩,reverse代表倒序回放,默認(rèn)為restart|
android:repeatCount = ''2'' // 重放次數(shù)(所以動畫的播放次數(shù)=重放次數(shù)+1)劣摇,為infinite時無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器珠移,
// 以下參數(shù)是平移動畫特有的屬性
android:fromXDelta="0" // 視圖在水平方向x 移動的起始值
android:toXDelta="500" // 視圖在水平方向x 移動的結(jié)束值
android:fromYDelta="0" // 視圖在豎直方向y 移動的起始值
android:toYDelta="500" // 視圖在豎直方向y 移動的結(jié)束值
/>
布局應(yīng)用
ImageView img = (ImageView)findViewById(R.id.img);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_translate);
img.startAnimation(animation);
如果用代碼去寫
上面那些公用屬性 在TranslateAnimation都能找到set這里就不寫全了
ImageView img = (ImageView)findViewById(R.id.img);
TranslateAnimation translateAnimation = new TranslateAnimation(100, 600, 100, 600);
translateAnimation.setDuration(3000);
translateAnimation.setRepeatCount(2);
img.startAnimation(translateAnimation);
縮放
<?xml version="1.0" encoding="utf-8"?>
// 采用<scale/> 標(biāo)簽表示是縮放動畫
<scale xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動畫持續(xù)時間(ms),必須設(shè)置末融,動畫才有效果
android:startOffset ="1000" // 動畫延遲開始時間(ms)
android:fillBefore = “true” // 動畫播放完后钧惧,視圖是否會停留在動畫開始的狀態(tài),默認(rèn)為true
android:fillAfter = “false” // 動畫播放完后勾习,視圖是否會停留在動畫結(jié)束的狀態(tài)浓瞪,優(yōu)先于fillBefore值,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值巧婶,對fillAfter值無影響乾颁,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動畫模式,restart代表正序重放艺栈,reverse代表倒序回放英岭,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動畫的播放次數(shù)=重放次數(shù)+1),為infinite時無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器湿右,即影響動畫的播放速度,下面會詳細(xì)講
// 以下參數(shù)是縮放動畫特有的屬性
android:fromXScale="0.0"
// 動畫在水平方向X的起始縮放倍數(shù)
// 0.0表示收縮到?jīng)]有诅妹;1.0表示正常無伸縮
// 值小于1.0表示收縮;值大于1.0表示放大
android:toXScale="2" //動畫在水平方向X的結(jié)束縮放倍數(shù)
android:fromYScale="0.0" //動畫開始前在豎直方向Y的起始縮放倍數(shù)
android:toYScale="2" //動畫在豎直方向Y的結(jié)束縮放倍數(shù)
android:pivotX="50%" // 縮放軸點的x坐標(biāo)
android:pivotY="50%" // 縮放軸點的y坐標(biāo)
// 軸點 = 視圖縮放的中心點
// pivotX pivotY,可取值為數(shù)字诅需,百分比漾唉,或者百分比p
// 設(shè)置為數(shù)字時(如50)荧库,軸點為View的左上角的原點在x方向和y方向加上50px的點堰塌。在Java代碼里面設(shè)置這個參數(shù)的對應(yīng)參數(shù)是Animation.ABSOLUTE。
// 設(shè)置為百分比時(如50%)分衫,軸點為View的左上角的原點在x方向加上自身寬度50%和y方向自身高度50%的點场刑。在Java代碼里面設(shè)置這個參數(shù)的對應(yīng)參數(shù)是Animation.RELATIVE_TO_SELF。
// 設(shè)置為百分比p時(如50%p)蚪战,軸點為View的左上角的原點在x方向加上父控件寬度50%和y方向父控件高度50%的點牵现。在Java代碼里面設(shè)置這個參數(shù)的對應(yīng)參數(shù)是Animation.RELATIVE_TO_PARENT
// 兩個50%表示動畫從自身中間開始,具體如下圖
/>
ImageView img = (ImageView)findViewById(R.id.img);
ScaleAnimation scaleAnimation = new ScaleAnimation(0, 2, 0, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
img.startAnimation(scaleAnimation);
//構(gòu)造函數(shù)
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
參數(shù)介紹
// 1. fromX :動畫在水平方向X的結(jié)束縮放倍數(shù)
// 2. toX :動畫在水平方向X的結(jié)束縮放倍數(shù)
// 3. fromY :動畫開始前在豎直方向Y的起始縮放倍數(shù)
// 4. toY:動畫在豎直方向Y的結(jié)束縮放倍數(shù)
// 5. pivotXType:縮放軸點的x坐標(biāo)的模式
// 6. pivotXValue:縮放軸點x坐標(biāo)的相對值
// 7. pivotYType:縮放軸點的y坐標(biāo)的模式
// 8. pivotYValue:縮放軸點y坐標(biāo)的相對值
// pivotXType = Animation.ABSOLUTE:縮放軸點的x坐標(biāo) = View左上角的原點 在x方向 加上
//pivotXValue數(shù)值的點(y方向同理)
// pivotXType = Animation.RELATIVE_TO_SELF:縮放軸點的x坐標(biāo) = View左上角的原點 在x方向 加上
// 自身寬度乘上pivotXValue數(shù)值的值(y方向同理)
// pivotXType = Animation.RELATIVE_TO_PARENT:縮放軸點的x坐標(biāo) = View左上角的原點 在x方向 加
//上 父控件寬度乘上pivotXValue數(shù)值的值 (y方向同理)
旋轉(zhuǎn)
<?xml version="1.0" encoding="utf-8"?>
// 采用<rotate/> 標(biāo)簽表示是旋轉(zhuǎn)動畫
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動畫持續(xù)時間(ms)邀桑,必須設(shè)置瞎疼,動畫才有效果
android:startOffset ="1000" // 動畫延遲開始時間(ms)
android:fillBefore = “true” // 動畫播放完后,視圖是否會停留在動畫開始的狀態(tài)壁畸,默認(rèn)為true
android:fillAfter = “false” // 動畫播放完后贼急,視圖是否會停留在動畫結(jié)束的狀態(tài)茅茂,優(yōu)先于fillBefore值,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值太抓,對fillAfter值無影響空闲,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動畫模式,restart代表正序重放走敌,reverse代表倒序回放碴倾,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動畫的播放次數(shù)=重放次數(shù)+1),為infinite時無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器掉丽,即影響動畫的播放速度,下面會詳細(xì)講
// 以下參數(shù)是旋轉(zhuǎn)動畫特有的屬性
android:duration="1000"
android:fromDegrees="0" // 動畫開始時 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時針跌榔,負(fù)數(shù) = 逆時針)
android:toDegrees="270" // 動畫結(jié)束時 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時針,負(fù)數(shù) = 逆時針)
android:pivotX="50%" // 旋轉(zhuǎn)軸點的x坐標(biāo)
android:pivotY="0" // 旋轉(zhuǎn)軸點的y坐標(biāo)
// 軸點 = 視圖縮放的中心點
// pivotX pivotY,可取值為數(shù)字捶障,百分比矫户,或者百分比p
// 設(shè)置為數(shù)字時(如50),軸點為View的左上角的原點在x方向和y方向加上50px的點残邀。在Java代碼里面設(shè)置這個參數(shù)的對應(yīng)參數(shù)是Animation.ABSOLUTE皆辽。
// 設(shè)置為百分比時(如50%),軸點為View的左上角的原點在x方向加上自身寬度50%和y方向自身高度50%的點芥挣。在Java代碼里面設(shè)置這個參數(shù)的對應(yīng)參數(shù)是Animation.RELATIVE_TO_SELF驱闷。
// 設(shè)置為百分比p時(如50%p),軸點為View的左上角的原點在x方向加上父控件寬度50%和y方向父控件高度50%的點空免。在Java代碼里面設(shè)置這個參數(shù)的對應(yīng)參數(shù)是Animation.RELATIVE_TO_PARENT
// 兩個50%表示動畫從自身中間開始空另,具體如下圖
/>
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動畫的 視圖View
Animation rotateAnimation = new RotateAnimation(0,270,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
// 步驟2:創(chuàng)建旋轉(zhuǎn)動畫的對象 & 設(shè)置動畫效果:旋轉(zhuǎn)動畫對應(yīng)的Animation子類為RotateAnimation
// 參數(shù)說明:
// 1. fromDegrees :動畫開始時 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時針,負(fù)數(shù) = 逆時針)
// 2. toDegrees :動畫結(jié)束時 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時針蹋砚,負(fù)數(shù) = 逆時針)
// 3. pivotXType:旋轉(zhuǎn)軸點的x坐標(biāo)的模式
// 4. pivotXValue:旋轉(zhuǎn)軸點x坐標(biāo)的相對值
// 5. pivotYType:旋轉(zhuǎn)軸點的y坐標(biāo)的模式
// 6. pivotYValue:旋轉(zhuǎn)軸點y坐標(biāo)的相對值
// pivotXType = Animation.ABSOLUTE:旋轉(zhuǎn)軸點的x坐標(biāo) = View左上角的原點 在x方向 加上 pivotXValue數(shù)值的點(y方向同理)
// pivotXType = Animation.RELATIVE_TO_SELF:旋轉(zhuǎn)軸點的x坐標(biāo) = View左上角的原點 在x方向 加上 自身寬度乘上pivotXValue數(shù)值的值(y方向同理)
// pivotXType = Animation.RELATIVE_TO_PARENT:旋轉(zhuǎn)軸點的x坐標(biāo) = View左上角的原點 在x方向 加上 父控件寬度乘上pivotXValue數(shù)值的值 (y方向同理)
rotateAnimation.setDuration(3000);
// 固定屬性的設(shè)置都是在其屬性前加“set”扼菠,如setDuration()
mButton.startAnimation(rotateAnimation);
// 步驟3:播放動畫
漸隱動畫
<?xml version="1.0" encoding="utf-8"?>
// 采用<alpha/> 標(biāo)簽表示是透明度動畫
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
// 以下參數(shù)是4種動畫效果的公共屬性,即都有的屬性
android:duration="3000" // 動畫持續(xù)時間(ms),必須設(shè)置坝咐,動畫才有效果
android:startOffset ="1000" // 動畫延遲開始時間(ms)
android:fillBefore = “true” // 動畫播放完后循榆,視圖是否會停留在動畫開始的狀態(tài),默認(rèn)為true
android:fillAfter = “false” // 動畫播放完后墨坚,視圖是否會停留在動畫結(jié)束的狀態(tài)秧饮,優(yōu)先于fillBefore值,默認(rèn)為false
android:fillEnabled= “true” // 是否應(yīng)用fillBefore值泽篮,對fillAfter值無影響盗尸,默認(rèn)為true
android:repeatMode= “restart” // 選擇重復(fù)播放動畫模式,restart代表正序重放帽撑,reverse代表倒序回放泼各,默認(rèn)為restart|
android:repeatCount = “0” // 重放次數(shù)(所以動畫的播放次數(shù)=重放次數(shù)+1),為infinite時無限重復(fù)
android:interpolator = @[package:]anim/interpolator_resource // 插值器亏拉,即影響動畫的播放速度,下面會詳細(xì)講
// 以下參數(shù)是透明度動畫特有的屬性
android:fromAlpha="1.0" // 動畫開始時視圖的透明度(取值范圍: -1 ~ 1)
android:toAlpha="0.0"http:// 動畫結(jié)束時視圖的透明度(取值范圍: -1 ~ 1)
/>
漸隱代碼
Button mButton = (Button) findViewById(R.id.Button);
// 步驟1:創(chuàng)建 需要設(shè)置動畫的 視圖View
Animation alphaAnimation = new AlphaAnimation(1,0);
// 步驟2:創(chuàng)建透明度動畫的對象 & 設(shè)置動畫效果:透明度動畫對應(yīng)的Animation子類為AlphaAnimation
// 參數(shù)說明:
// 1. fromAlpha:動畫開始時視圖的透明度(取值范圍: -1 ~ 1)
// 2. toAlpha:動畫結(jié)束時視圖的透明度(取值范圍: -1 ~ 1)
alphaAnimation.setDuration(3000);
// 固定屬性的設(shè)置都是在其屬性前加“set”扣蜻,如setDuration()
mButton.startAnimation(alphaAnimation);
// 步驟3:播放動畫
我們發(fā)現(xiàn)其實用補間動畫還是挺方便的寸癌,但是為什么后面又出了屬性動畫呢?是因為補間動畫有一個致命缺點弱贼,比如我們利用位移動畫來測試一下
ImageView img = (ImageView) findViewById(R.id.img);
TranslateAnimation translateAnimation = new TranslateAnimation(100, 600, 100, 600);
translateAnimation.setDuration(3000);
translateAnimation.setFillAfter(true);
img.startAnimation(translateAnimation);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "我被點到了", Toast.LENGTH_SHORT).show();
}
});
我們在點擊圖片的時候打出一句吐司蒸苇,結(jié)果發(fā)現(xiàn)在圖片位移到下方的時候,我們點圖片木有吐司吮旅,在點擊圖片原來位置溪烤,也就是紅圈位置,居然彈出來了吐司庇勃。檬嘀,補間動畫只是表面上實現(xiàn)了平移,旋轉(zhuǎn)责嚷,漸變鸳兽,縮放,實際上屬性值不變罕拂,看起來位移走了揍异,實際還在原來地方。
動畫的監(jiān)聽
translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//動畫開始
}
@Override
public void onAnimationEnd(Animation animation) {
//動畫結(jié)束
}
@Override
public void onAnimationRepeat(Animation animation) {
//重復(fù)的時候
}
});
差值器Interpolator和估值器TypeEvaluator
感謝分享
https://blog.csdn.net/carson_ho/article/details/72863901
插值器(Interpolator)
- 定義:一個接口
- 作用:設(shè)置 屬性值 從初始值過渡到結(jié)束值 的變化規(guī)律
- 如勻速爆班、加速 & 減速 等等
- 即確定了 動畫效果變化的模式衷掷,如勻速變化、加速變化 等等
主要實現(xiàn)實現(xiàn)非線性運動的動畫效果
非線性運動:動畫改變的速率不是一成不變的柿菩,如加速 & 減速運動都屬于非線性運動
差值器如何在動畫中設(shè)置呢戚嗅?
- 代碼方式
Interpolator overshootInterpolator = new OvershootInterpolator();
alphaAnimation.setInterpolator(overshootInterpolator);
- XML模式
android:interpolator="@android:anim/overshoot_interpolator"
android 為我們提供了很多種內(nèi)置的差值器,系統(tǒng)默認(rèn)的插值器是AccelerateDecelerateInterpolator枢舶,即先加速后減速
自定義差值器
本質(zhì):根據(jù)動畫的進(jìn)度(0%-100%)計算出當(dāng)前屬性值改變的百分比
具體使用:自定義插值器需要實現(xiàn) Interpolator / TimeInterpolator接口 & 復(fù)寫getInterpolation()
補間動畫 實現(xiàn) Interpolator接口懦胞;屬性動畫實現(xiàn)TimeInterpolator接口
TimeInterpolator接口是屬性動畫中新增的,用于兼容Interpolator接口凉泄,這使得所有過去的Interpolator實現(xiàn)類都可以直接在屬性動畫使用躏尉,
public class MyInterpolator implements Interpolator {
@Override
public float getInterpolation(float v) {
//業(yè)務(wù)邏輯,返回值就是要改變的屬性的百分比
return xxx
}
}
估值器(TypeEvaluator)
定義:一個接口
作用:設(shè)置 屬性值 從初始值過渡到結(jié)束值 的變化具體數(shù)值
插值器(Interpolator)決定 值 的變化規(guī)律(勻速旧困、加速等)醇份,即決定的是變化趨勢;而接下來的具體變化數(shù)值則交給 而估值器 是屬性動畫特有的屬性吼具。用來協(xié)助插值器 實現(xiàn)非線性運動的動畫效果。
內(nèi)置估值器有
IntEvaluator:以整型的形式從初始值 - 結(jié)束值 進(jìn)行過渡
FloatEvaluator:以浮點型的形式從初始值 - 結(jié)束值 進(jìn)行過渡
ArgbEvaluator:以Argb類型的形式從初始值 - 結(jié)束值 進(jìn)行過渡
設(shè)置方式為
ObjectAnimator anim = ObjectAnimator.ofObject(myView2, "height", new Evaluator()矩距,1拗盒,3);
其實我也尋思很久 這個估值器能干嘛,因為也一直沒用過锥债,個人覺得估值器最大作用就是根據(jù)一個動畫每時每刻獲取當(dāng)前動畫的進(jìn)度值陡蝇。如果說這玩意能干嘛痊臭,我們就寫個小例子來實驗下。
@Override
public Object evaluate(float v, Object o, Object t1) {
return null;
}