android動畫三·ValueAnimator和ObjectAnimator的高階用法
內容
動畫
ValueAnimator的高階用法
假如:我們有一個自定義的View,在這個View當中有一個Point物件用於管理座標,然後在onDraw()方法當中就是根據這個Point物件的座標值來進行繪製的。如果可以對Point物件進行動畫操作,那麼整個自定義View的動畫效果就有了。
- TypeEvaluator是告訴動畫系統如何從初始值過度到結束值。
public class FloatEvaluator implements TypeEvaluator {
public Object evaluate (float fraction, Object startValue, Object endValue) {
float startFloat = ((Number) startValue).floatValue();
return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);
}
}
fraction表示動畫的完成度.第二第三個引數分別表示動畫的初始值和結束值。
- 下面來先定義一個Point類,如下所示:
public class Point {
private float x;
private float y;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}
- 接下來定義PointEvaluator,如下所示:
public class PointEvaluator implements TypeEvaluator{
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
Point startPoint = (Point) startValue;
Point endPoint = (Point) endValue;
float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
Point point = new Point(x, y);
return point;
}
}
- 比如說我們有兩個Point物件,現在需要將Point1通過動畫平滑過度到Point2,就可以這樣寫:
Point point1 = new Point(0, 0);
Point point2 = new Point(300, 300);
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
anim.setDuration(5000);
anim.start();
好的,這就是自定義TypeEvaluator的全部用法,掌握了這些知識之後,我們就可以來嘗試一下如何通過對Point物件進行動畫操作,從而實現整個自定義View的動畫效果。
- 新建一個MyAnimView繼承自View,程式碼如下所示:
public class MyAnimView extends View {
public static final float RADIUS = 50f;
private Point currentPoint;
private Paint mPaint;
public MyAnimView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.BLUE);
}
@Override
protected void onDraw(Canvas canvas) {
if (currentPoint == null) {
currentPoint = new Point(RADIUS, RADIUS);
drawCircle(canvas);
startAnimation();
} else {
drawCircle(canvas);
}
}
private void drawCircle(Canvas canvas) {
float x = currentPoint.getX();
float y = currentPoint.getY();
canvas.drawCircle(x, y, RADIUS, mPaint);
}
private void startAnimation() {
Point startPoint = new Point(RADIUS, RADIUS);
Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
currentPoint = (Point) animation.getAnimatedValue();
invalidate();
}
});
anim.setDuration(5000);
anim.start();
}
}
這裡我們定義了一個startPoint和一個endPoint,座標分別是View的左上角和右下角,並將動畫的時長設為5秒。然後有一點需要注意的,就是我們通過監聽器對動畫的過程進行了監聽,每當Point值有改變的時候都會回撥onAnimationUpdate()方法。在這個方法當中,我們對currentPoint物件進行了重新賦值,並呼叫了invalidate()方法,這樣的話onDraw()方法就會重新呼叫,並且由於currentPoint物件的座標已經改變了,那麼繪製的位置也會改變,於是一個平移的動畫效果也就實現了。
- 下面我們只需要在佈局檔案當中引入這個自定義控制元件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.example.tony.myapplication.MyAnimView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ObjectAnimator的高階用法
補間動畫是隻能實現移動、縮放、旋轉和淡入淡出這四種動畫操作的,功能限定死就是這些,基本上沒有任何擴充套件性可言。比如我們想要實現對View的顏色進行動態改變,補間動畫是沒有辦法做到的。
- ObjectAnimator內部的工作機制是通過尋找特定屬性的get和set方法,然後通過方法不斷地對值進行改變,從而實現動畫效果的。因此我們就需要在MyAnimView中定義一個color屬性,並提供它的get和set方法。這裡我們可以將color屬性設定為字串型別,使用#RRGGBB這種格式來表示顏色值,程式碼如下所示:
public class MyAnimView extends View {
...
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
mPaint.setColor(Color.parseColor(color));
invalidate();
}
...
}
- 先編寫一個用於告知系統如何進行顏色過度的TypeEvaluator。建立ColorEvaluator並實現TypeEvaluator介面,程式碼如下所示:
public class ColorEvaluator implements TypeEvaluator {
private int mCurrentRed = -1;
private int mCurrentGreen = -1;
private int mCurrentBlue = -1;
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
String startColor = (String) startValue;
String endColor = (String) endValue;
int startRed = Integer.parseInt(startColor.substring(1, 3), 16);
int startGreen = Integer.parseInt(startColor.substring(3, 5), 16);
int startBlue = Integer.parseInt(startColor.substring(5, 7), 16);
int endRed = Integer.parseInt(endColor.substring(1, 3), 16);
int endGreen = Integer.parseInt(endColor.substring(3, 5), 16);
int endBlue = Integer.parseInt(endColor.substring(5, 7), 16);
// 初始化顏色的值
if (mCurrentRed == -1) {
mCurrentRed = startRed;
}
if (mCurrentGreen == -1) {
mCurrentGreen = startGreen;
}
if (mCurrentBlue == -1) {
mCurrentBlue = startBlue;
}
// 計算初始顏色和結束顏色之間的差值
int redDiff = Math.abs(startRed - endRed);
int greenDiff = Math.abs(startGreen - endGreen);
int blueDiff = Math.abs(startBlue - endBlue);
int colorDiff = redDiff + greenDiff + blueDiff;
if (mCurrentRed != endRed) {
mCurrentRed = getCurrentColor(startRed, endRed, colorDiff, 0,
fraction);
} else if (mCurrentGreen != endGreen) {
mCurrentGreen = getCurrentColor(startGreen, endGreen, colorDiff,
redDiff, fraction);
} else if (mCurrentBlue != endBlue) {
mCurrentBlue = getCurrentColor(startBlue, endBlue, colorDiff,
redDiff + greenDiff, fraction);
}
// 將計算出的當前顏色的值組裝返回
String currentColor = "#" + getHexString(mCurrentRed)
+ getHexString(mCurrentGreen) + getHexString(mCurrentBlue);
return currentColor;
}
/**
* 根據fraction值來計算當前的顏色。
*/
private int getCurrentColor(int startColor, int endColor, int colorDiff,
int offset, float fraction) {
int currentColor;
if (startColor > endColor) {
currentColor = (int) (startColor - (fraction * colorDiff - offset));
if (currentColor < endColor) {
currentColor = endColor;
}
} else {
currentColor = (int) (startColor + (fraction * colorDiff - offset));
if (currentColor > endColor) {
currentColor = endColor;
}
}
return currentColor;
}
/**
* 將10進位制顏色值轉換成16進位制。
*/
private String getHexString(int value) {
String hexString = Integer.toHexString(value);
if (hexString.length() == 1) {
hexString = "0" + hexString;
}
return hexString;
}
}
首先在evaluate()方法當中獲取到顏色的初始值和結束值,並通過字串擷取的方式將顏色分為RGB三個部分,並將RGB的值轉換成十進位制數字,那麼每個顏色的取值範圍就是0-255。接下來計算一下初始顏色值到結束顏色值之間的差值,這個差值很重要,決定著顏色變化的快慢,如果初始顏色值和結束顏色值很相近,那麼顏色變化就會比較緩慢,而如果顏色值相差很大,比如說從黑到白,那麼就要經歷255*3這個幅度的顏色過度,變化就會非常快。
那麼控制顏色變化的速度是通過getCurrentColor()這個方法來實現的,這個方法會根據當前的fraction值來計算目前應該過度到什麼顏色,並且這裡會根據初始和結束的顏色差值來控制變化速度,最終將計算出的顏色進行返回。
最後,由於我們計算出的顏色是十進位制數字,這裡還需要呼叫一下getHexString()方法把它們轉換成十六進位制字串,再將RGB顏色拼裝起來之後作為最終的結果返回。
- 比如說我們想要實現從藍色到紅色的動畫過度,歷時5秒,就可以這樣寫:
ObjectAnimator anim = ObjectAnimator.ofObject(myAnimView, "color", new ColorEvaluator(),
"#0000FF", "#FF0000");
anim.setDuration(5000);
anim.start();
*綜合valueAnimator和ObjectAnimator,接下來我們需要將上面一段程式碼移到MyAnimView類當中,讓它和剛才的Point移動動畫可以結合到一起播放,這就要藉助我們在上篇文章當中學到的組合動畫的技術了。修改MyAnimView中的程式碼,如下所示:
public class MyAnimView extends View {
...
private void startAnimation() {
Point startPoint = new Point(RADIUS, RADIUS);
Point endPoint = new Point(getWidth() - RADIUS, getHeight() - RADIUS);
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
currentPoint = (Point) animation.getAnimatedValue();
invalidate();
}
});
ObjectAnimator anim2 = ObjectAnimator.ofObject(this, "color", new ColorEvaluator(),
"#0000FF", "#FF0000");
AnimatorSet animSet = new AnimatorSet();
animSet.play(anim).with(anim2);
animSet.setDuration(5000);
animSet.start();
}
}
參考:[http://blog.csdn.net/guolin_blog/article/details/43536355]