事件
事件就是當(dāng)用戶觸摸屏幕所產(chǎn)生的點(diǎn)擊事件(Touch Event),其相關(guān)細(xì)節(jié)被封裝成MotionEvent對象
事件類型
- MotionEvent.ACTION_DOWN 按下View(所有事件的開始)
- MotionEvent.ACTION_MOVE 滑動(dòng)View
- MotionEvent.ACTION_UP 抬起View(與DOWN對應(yīng))
- MotionEvent.ACTION_CANCEL 結(jié)束事件(非人為原因)
事件分發(fā)
把點(diǎn)擊事件傳遞到某個(gè)對應(yīng)的View上,并處理的過程
事件分發(fā)順序
Activity -> ViewGroup -> View

注解:dispatch(分發(fā))
在ViewGroup中可以通過onInterceptTouchEvent方法對事件傳遞進(jìn)行攔截onInterceptTouchEvent方法返回true代表不允許事件繼續(xù)向子View傳遞,返回false代表不對事件進(jìn)行攔截,默認(rèn)返回false。而View中沒有onInterceptTouchEvent方法。
子View中如果將傳遞的事件消費(fèi)掉,ViewGroup中將無法接收到任何事件。
事件傳遞機(jī)制最重要的三個(gè)方法:dispatchTouchEvent()分發(fā)事件、onInterceptTouchEvent()攔截事件、onTouchEvent()消費(fèi)事件。
動(dòng)畫 Animation
分類:屬性動(dòng)畫(property Animation)、視圖動(dòng)畫(逐幀動(dòng)畫(Frame Animation)、補(bǔ)間動(dòng)畫(Tween Animation))

逐幀動(dòng)畫(Frame Animation)
把靜態(tài)圖片放入res/drawable目錄下面,快速播放形成動(dòng)畫,實(shí)現(xiàn)方式有xml和Java,推薦使用xml
XML創(chuàng)建:
通過創(chuàng)建Animation_list.xml定義動(dòng)畫,在xml文件中每個(gè)item對應(yīng)一幀(一張圖片),drawable配置當(dāng)前顯示的界面,duration圖片顯示的時(shí)長。MainActivity中通過imageview.getDrawable實(shí)例化AnimationDrawabel對象,由animationdrawable調(diào)用start和stop來啟動(dòng)或暫停動(dòng)畫。
Java代碼創(chuàng)建:
將圖片資源保存到drawable目錄下面,代碼中將圖片資源保存到整型數(shù)組中;通過new實(shí)例化AnimationDrawable對象,for循環(huán)遍歷圖片資源的整型數(shù)組然后由animationdrawable對象調(diào)用addFrame()方法來添加圖片;最后把a(bǔ)nimationdrawable通過setImageDrawable添加到顯示的控件上面。同樣調(diào)用start和stop來啟動(dòng)或暫停動(dòng)畫。
補(bǔ)間動(dòng)畫(Tween Animation)
只指定圖片開始和圖片結(jié)束的“關(guān)鍵幀”,“中間幀”由系統(tǒng)計(jì)算并補(bǔ)齊
通過AnimationUtils.loadAnimation()來加載動(dòng)畫,startAnimation來啟動(dòng)動(dòng)畫
分類:
位移:Translate Animation
縮放: Scale Animation
透明度:Alpha Animation
旋轉(zhuǎn): Rotate Animation
XML創(chuàng)建四種方式共有屬性:
- android:duration="3000" 動(dòng)畫持續(xù)時(shí)間(ms),必須設(shè)置,動(dòng)畫才有效果
- android:startOffset="1000" 動(dòng)畫延遲開始時(shí)間(ms)
- android:fillBefore 用于確定動(dòng)畫開始時(shí),View的動(dòng)畫屬性值,默認(rèn)為true;fillBefore為true,在startOffset階段時(shí),將動(dòng)畫屬性設(shè)置為給與的初始值
fillBefore為false,將動(dòng)畫屬性設(shè)置為View本身的初始值 - android:fillAfter 動(dòng)畫播放完后,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài),優(yōu)先于fillBefore值,默認(rèn)為false
- android:fillEnabled 是否應(yīng)用fillBefore值,對fillAfter值無影響,默認(rèn)為false
- android:repeatMode 選擇重復(fù)播放動(dòng)畫模式,restart代表正序重放,reverse代表倒序回放,默認(rèn)為restart
- android:repeatCount 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1),為infinite時(shí)無限重復(fù)
- android:interpolator 插值器,即影響動(dòng)畫的播放速度
共有的Java方法:
- public void setDuration(long durationMillis) // 動(dòng)畫持續(xù)時(shí)間(ms),必須設(shè)置,動(dòng)畫才有效果
- public void setStartOffset(long startOffset) // 動(dòng)畫延遲開始時(shí)間(ms)
- public void setFillBefore(boolean fillBefore) // 用于確定動(dòng)畫開始時(shí),View的動(dòng)畫屬性值,默認(rèn)為true
// fillBefore為true,在startOffset階段時(shí),將動(dòng)畫屬性設(shè)置為給與的初始值
// fillBefore為false,將動(dòng)畫屬性設(shè)置為View本身的初始值 - public void setFillAfter(boolean fillAfter) // 動(dòng)畫播放完后,視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài),優(yōu)先于fillBefore值,默認(rèn)為false
- public void setFillEnabled(boolean fillEnabled) // 是否應(yīng)用fillBefore值,對fillAfter值無影響,默認(rèn)為false
- public void setRepeatMode(int repeatMode) // 選擇重復(fù)播放動(dòng)畫模式,Animation.RESTART 代表正序重放,Animation.REVERSE 代表倒序回放,默認(rèn)為Animation.RESTART
- public void setRepeatCount(int repeatCount) // 重放次數(shù)(所以動(dòng)畫的播放次數(shù)=重放次數(shù)+1),為Animation.INFINITE 時(shí)無限重復(fù)
- public void setInterpolator(Interpolator i) // 插值器,即影響動(dòng)畫的播放速度
插值器:

自定義插值器:
本質(zhì)根據(jù)動(dòng)畫的進(jìn)度(0%-100%)改變當(dāng)前屬性的值
實(shí)現(xiàn)方式:實(shí)現(xiàn)Interpolator/TimeInterpolator接口,重寫getInterpolator()方法
補(bǔ)間動(dòng)畫實(shí)現(xiàn)Interpolator接口,屬性動(dòng)畫實(shí)現(xiàn)TimeInterpolator接口(用于兼容Interpolator接口)
// Interpolator接口
public interface Interpolator {
// 內(nèi)部只有一個(gè)方法
float getInterpolation(float input) {
// 參數(shù)說明
// input值值變化范圍是0-1,且隨著動(dòng)畫進(jìn)度(0% - 100% )均勻變化
// 即動(dòng)畫開始時(shí),input值 = 0;動(dòng)畫結(jié)束時(shí)input = 1
// 而中間的值則是隨著動(dòng)畫的進(jìn)度(0% - 100%)在0到1之間均勻增加
...
// 插值器的計(jì)算邏輯
return xxx;
// 返回的值就是用于估值器繼續(xù)計(jì)算的fraction值,下面會(huì)詳細(xì)說明
}
// TimeInterpolator接口
// 同上
public interface TimeInterpolator {
float getInterpolation(float input);
}
平移獨(dú)有特性:
- android:fromXDelta 視圖在水平方向x移動(dòng)的起始值
- android:toXDelta 視圖在水平方向x移動(dòng)結(jié)束始值
- android:fromYDelta 視圖在垂直方向y移動(dòng)的起始值
- android:toYDelta 視圖在垂直方向y移動(dòng)的結(jié)束值
平移Java方法:
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
縮放獨(dú)有特性:
android:fromXScale //動(dòng)畫在水平方向x的起始縮放倍數(shù)
android:toXScale //動(dòng)畫在水平方向x的結(jié)束縮放倍數(shù)
android:fromYScale //動(dòng)畫在豎直方向y的起始縮放倍數(shù)
android:toYScale //動(dòng)畫在豎直方向y的結(jié)束縮放倍數(shù)
//縮放倍數(shù) 0.0 表示收縮到?jīng)]有,1.0 表示正常無收縮
//縮放倍數(shù) < 1.0,收縮
//縮放倍數(shù) > 1.0 放大android:pivotX //縮放軸點(diǎn)的x坐標(biāo)
android:pivotY //縮放軸點(diǎn)的y坐標(biāo)
縮放Java方法:
public ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
//縮放軸點(diǎn)取值類型:數(shù)值,百分比,百分比p
//數(shù)值(如50):軸點(diǎn)為View的左上角的原點(diǎn)在x方向或y方向加上50px的點(diǎn)。
//百分比(如50%):軸點(diǎn)為View的左上角的原點(diǎn)在x方向加上自身寬度50%或y方向加上自身高度50%的點(diǎn)
//百分比p(如50%p):軸點(diǎn)為View的左上角的原點(diǎn)在x方向加上父控件寬度50%或y方向加上父控件高度50%的點(diǎn)。與旋轉(zhuǎn)共享
旋轉(zhuǎn)獨(dú)有特性:
android:fromDegrees // 動(dòng)畫開始時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針,負(fù)數(shù) = 逆時(shí)針)
android:toDegrees // 動(dòng)畫結(jié)束時(shí) 視圖的旋轉(zhuǎn)角度(正數(shù) = 順時(shí)針,負(fù)數(shù) = 逆時(shí)針)
android:pivotX //縮放軸點(diǎn)的x坐標(biāo)
android:pivotY //縮放軸點(diǎn)的y坐標(biāo)
旋轉(zhuǎn)Java方法:
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,int pivotYType, float pivotYValue)
透明度獨(dú)有特性:
- android:fromAlpha // 動(dòng)畫開始時(shí)視圖的透明度(取值范圍: 0.0 ~ 1.0)
- android:toAlpha // 動(dòng)畫結(jié)束時(shí)視圖的透明度(取值范圍: 0.0 ~ 1.0)
1表示不透明,0表示完全透明
透明度Java方法:
public AlphaAnimation(float fromAlpha, float toAlpha)
組合動(dòng)畫
同時(shí)使用旋轉(zhuǎn),縮放,透明度,平移的動(dòng)畫稱為組合動(dòng)畫
組合動(dòng)畫創(chuàng)建XML,需要使用<set></set>標(biāo)簽
組合動(dòng)畫Java方法:
- public AnimationSet(boolean shareInterpolator) //AnimationSet的構(gòu)造方法,shareInterpolator控制組合動(dòng)畫中的動(dòng)畫是否和集合共享同一個(gè)差值器
- public void addAnimation(Animation a) //將子動(dòng)畫加入到組合動(dòng)畫中
動(dòng)畫高級(jí)使用
1、監(jiān)聽動(dòng)畫
通過給Animation設(shè)置Listener,監(jiān)聽動(dòng)畫的開始、結(jié)束、重復(fù)來進(jìn)行一系列操作,例如:界面跳轉(zhuǎn)等。
采用動(dòng)畫適配器,解決實(shí)現(xiàn)接口繁瑣的問題:
anim.addListener(new AnimatorListenerAdapter() {
// 向addListener()方法中傳入適配器對象AnimatorListenerAdapter()
// 由于AnimatorListenerAdapter中已經(jīng)實(shí)現(xiàn)好每個(gè)接口
// 所以這里不實(shí)現(xiàn)全部方法也不會(huì)報(bào)錯(cuò)
@Override
public void onAnimationStart(Animator animation) {
// 如想只想監(jiān)聽動(dòng)畫開始時(shí)刻,就只需要單獨(dú)重寫該方法就可以
}
});

屬性動(dòng)畫
補(bǔ)間動(dòng)畫只能作用在View上,無法對非View的對象進(jìn)行動(dòng)畫操作;且補(bǔ)間動(dòng)畫沒有改變View屬性,只改變了視覺效果,以及動(dòng)畫效果單一。
屬性動(dòng)畫是通過在一定時(shí)間間隔內(nèi)不斷對值進(jìn)行改變和將該值賦值給對象,實(shí)現(xiàn)從對象屬性上產(chǎn)生動(dòng)畫效果。

參考文獻(xiàn):
http://182.254.228.71/