Ⅰ android之animator 和animation 的區別
一、 前言
Animator框架是Android 4.0中新添加的一個動畫框架,和之前的Animation框架相比,Animator可以進行更多和更精細化的動畫控制,而且比之前更簡單和更高效。在4.0源碼中隨處都可以看到Animator的使用。
二、 Animation和Animator比較
如下圖,是Animation和Animator兩個類繼承圖的對比。
C:Object C:Object
C:Animation C:Animator
C:AlphaAnimation C:AnimatorSet
C:AnimationSet C:ValueAnimator
C:DummyAnimation C:ObjectAnimator
C:Rotate3dAnimation C:TimeAnbimator
C:RotateAniamtion
C:ScaleAnimation
C:TranslateAnimation
Animation框架定義了透明度,旋轉,縮放和位移幾種常見的動畫,而且控制的是一個整個View動畫,實現原理是每次繪制視圖時View所在的ViewGroup中的drawChild函數獲取該View的Animation的Transformation值,然後調用canvas.concat(transformToApply.getMatrix()),通過矩陣運算完成動畫幀,如果動畫沒有完成,繼續調用invalidate()函數,啟動下次繪制來驅動動畫,動畫過程中的幀之間間隙時間是繪制函數所消耗的時間,可能會導致動畫消耗比較多的CPU資源。
在Animator框架中使用最多的是AnimatorSet和ObjectAnimator配合,使用ObjectAnimator進行更精細化控制,只控制一個對象的一個屬性值,多個ObjectAnimator組合到AnimatorSet形成一個動畫。而且ObjectAnimator能夠自動驅動,可以調用setFrameDelay(longframeDelay)設置動畫幀之間的間隙時間,調整幀率,減少動畫過程中頻繁繪制界面,而在不影響動畫效果的前提下減少CPU資源消耗。
三、 關鍵介面介紹
1. ObjectAnimator介紹
Animator框架封裝得比較完美,對外提供的介面非常簡單,創建一個ObjectAnimator只需通過如下圖所示的靜態工廠類直接返回一個ObjectAnimator對象。傳的參數包括一個對象和對象的屬性名字,但這個屬性必須有get和set函數,內部會通過java反射機制來調用set函數修改對象屬性值。還包括屬性的初始值,最終值,還可以調用setInterpolator設置曲線函數。
2. AnimatorSet介紹
AnimatorSet主要是組合多個AnimatorSet和ObjectAnimator形成一個動畫,並可以控制動畫的播放順序,其中還有個輔助類通過調用play函數獲得。
3. AnimatorUpdateListner介紹
通過實現AnimatorUpdateListner,來獲得屬性值發生變化時的事件,在這個回調中發起重繪屏幕事件。
四、 使用實例
在Android4.0中的ApiDemo中有個BouncingBalls實例,描述了Animator框架的使用,當點擊屏幕時,繪制一個球從點擊位置掉到屏幕底部,碰到底部時球有壓扁的效果,然後回彈到點擊位置再消失。
代碼如下:
ShapeHolder newBall =addBall(event.getX(), event.getY());
// Bouncing animation with squash and stretch
float startY = newBall.getY();
float endY = getHeight() - 50f;
float h = (float)getHeight();
float eventY = event.getY();
int ration = (int)(500 * ((h - eventY)/h));
ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY);
bounceAnim.setDuration(ration);
bounceAnim.setInterpolator(new AccelerateInterpolator());
ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x", newBall.getX(),
newBall.getX() - 25f);
squashAnim1.setDuration(ration/4);
squashAnim1.setRepeatCount(1);
squashAnim1.setRepeatMode(ValueAnimator.REVERSE);
squashAnim1.setInterpolator(new DecelerateInterpolator());
ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall, "width", newBall.getWidth(),
newBall.getWidth() + 50);
squashAnim2.setDuration(ration/4);
squashAnim2.setRepeatCount(1);
squashAnim2.setRepeatMode(ValueAnimator.REVERSE);
squashAnim2.setInterpolator(new DecelerateInterpolator());
ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "y", endY,
endY + 25f);
stretchAnim1.setDuration(ration/4);
stretchAnim1.setRepeatCount(1);
stretchAnim1.setInterpolator(new DecelerateInterpolator());
stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);
ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall, "height",
newBall.getHeight(),newBall.getHeight() - 25);
stretchAnim2.setDuration(ration/4);
stretchAnim2.setRepeatCount(1);
stretchAnim2.setInterpolator(new DecelerateInterpolator());
stretchAnim2.setRepeatMode(ValueAnimator.REVERSE);
ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall, "y", endY,
startY);
bounceBackAnim.setDuration(ration);
bounceBackAnim.setInterpolator(newDecelerateInterpolator());
// Sequence the down/squash&stretch/upanimations
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(bounceAnim).before(squashAnim1);
bouncer.play(squashAnim1).with(squashAnim2);
bouncer.play(squashAnim1).with(stretchAnim1);
bouncer.play(squashAnim1).with(stretchAnim2);
bouncer.play(bounceBackAnim).after(stretchAnim2);
// Fading animation - remove the ball when theanimation is done
ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
fadeAnim.setDuration(250);
fadeAnim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animatoranimation) {
balls.remove(((ObjectAnimator)animation).getTarget());
}
});
// Sequence the two animations to play oneafter the other
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(bouncer).before(fadeAnim);
// Start the animation
animatorSet.start();
Ⅱ 如何從Android官方文檔查看 類的繼承圖
http://developer.android.com/reference/packages.html
Develop --- Reference
Ⅲ 關於android 繼承了BaseAdapter類後的一些問題,我這個程序就是為了實現一個圖片的拖拽,就是Gallery
因為你的getCount()返回的是mImageIds.length為5;
所以 getView(int position, ... )會先後將自動被調用5次。產生5個view。這5個view就是你在Galley里看到的5個view。
第一次調用,position=0.
每二次調用,position=1,
。。。
最後一次,position=4.
只要在你的getView(int position, ... )方法中寫成imageview.setImageResource(mImageIds[position]);
因為每次position不一樣,剛好就把你的五幅圖順序擺放了。
Ⅳ android 如何繼承Activity類實現雙指縮放、單指拖曳圖片
onTouch方法和onTouchEvent方法。
onTouch方法是View的 OnTouchListener借口中定義的方法。
當一個View綁定了OnTouchLister後,當有touch事件觸發時,就會調用onTouch方法。
(當把手放到View上後,onTouch方法被一遍一遍地被調用)
onTouchEvent方法是override 的Activity的方法。
重新了Activity的onTouchEvent方法後,當屏幕有touch事件時,此方法就會別調用。
(當把手放到Activity上時,onTouchEvent方法就會一遍一遍地被調用)
在一個Activity裡面放一個TextView的實例tv,並且這個tv的屬性設定為 fill_parent
在這種情況下,當手放到屏幕上的時候,首先會是tv響應touch事件,執行onTouch方法。
如果onTouch返回值為true,
表示這個touch事件被onTouch方法處理完畢,不會把touch事件再傳遞給Activity,
也就是說onTouchEvent方法不會被調用。
(當把手放到屏幕上後,onTouch方法被一遍一遍地被調用)
如果onTouch的返回值是false,
表示這個touch事件沒有被tv完全處理,onTouch返回以後,touch事件被傳遞給Activity,
onTouchEvent方法被調用。
(當把手放到屏幕上後,onTouch方法調用一次後,onTouchEvent方法就會一遍一遍地被調用)
Ⅳ android五大布局繼承哪個類
所有的控制項和布局 都繼承View這個類
Ⅵ android 一個繼承於View 的視圖 能做到 在他自己的類中 定製這個視圖的大小嗎
可以的,你在自定義空間中不要給他設置大小,而是在這個類中的onDraw()函數中使用代碼實現。