android弹球动画,Android动画之自定义Evaluator实现弹球效果
前言
今天給大家?guī)淼氖亲远xEvaluator實(shí)現(xiàn)彈球效果,我們先給大家來個(gè)效果圖。
下面我們介紹具體代碼流程
1. 自定義Point類
public class Point {
private int radius;
public Point(int radius) {
this.radius = radius;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
}
2. 自定義PointEvaluator實(shí)現(xiàn)TypeEvaluator接口
class PointEvaluator implements TypeEvaluator{
/**
*@param fraction 動(dòng)畫變化中的浮點(diǎn)參數(shù),0-1
*@param startValue 動(dòng)畫開始時(shí)的Point對(duì)象
*@param endValue 動(dòng)畫結(jié)束時(shí)的Point對(duì)象
*@return 動(dòng)畫過程中通過計(jì)算獲取半徑并返回一個(gè)新的Point對(duì)象
*/
@Override
public Point evaluate(float fraction, Point startValue, Point endValue) {
int startRadius = startValue.getRadius();
int endRadius = endValue.getRadius();
int newRadius = (int) (startRadius + fraction * (endRadius - startRadius));
return new Point(newRadius);
}
}
3. 自定義PointView
定義彈球?qū)傩詀ttrs
declare-styleable>
獲取自定義屬性,這里我們只定義了一個(gè)彈球顏色屬性。
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PointView);
mColor = typedArray.getColor(R.styleable.PointView_point_color, DEFAULT_COLOR);
typedArray.recycle();
}
初始化畫筆
private void initPaint() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mColor);
}
重寫onMeasure方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
/**
* 測(cè)量view的寬高
*/
if (widthMode == MeasureSpec.AT_MOST) {
widthSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_WIDTH, getResources().getDisplayMetrics()) + getPaddingLeft() + getPaddingRight();
}
if (heightMode == MeasureSpec.AT_MOST) {
heightSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_HEIGHT, getResources().getDisplayMetrics()) + getPaddingTop() + getPaddingBottom();
}
/**
* 使用setMeasureDimension方法確定view的最終寬和高
*/
setMeasuredDimension(widthSize, heightSize);
}
在onSizeChanged中獲取view的最終寬度和高度
這里minValue是取寬度和高度中的較小值
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
/**
* 在onSizeChanged重載方法中獲取view的最終寬度和高度
*/
width = w;
height = h;
/**
* 使用width減去左右的padding
* 使用height減去上下的padding
* 并取兩者中的最小值用來確定最大彈球的半徑
*/
minValue = Math.min(width - getPaddingLeft() - getPaddingRight(), height - getPaddingTop() - getPaddingBottom());
}
接下來是動(dòng)畫的實(shí)現(xiàn),使用ValueAnimator的ofObject()實(shí)現(xiàn)彈球動(dòng)畫
public void startAnimation() {
/**
* 使用ValueAnimator.ofObject()方法并使用自定義的Evaluator實(shí)現(xiàn)動(dòng)畫
*/
ValueAnimator animator = ValueAnimator.ofObject(new PointEvaluator(), new Point(0), new Point(minValue * 2 / 5));
animator.setDuration(2000);
/**
* 設(shè)置插值器為BounceInterpolator,其效果為:動(dòng)畫結(jié)束的時(shí)候彈起
*/
animator.setInterpolator(new BounceInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
/**
* 通過getAnimatedValue獲取我們變化中的Point對(duì)象
*/
mPoint = (Point) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
最后是在onDraw函數(shù)中繪制彈球
@Override
protected void onDraw(Canvas canvas) {
if (mPoint != null) {
canvas.drawCircle(width / 2, height / 2, mPoint.getRadius(), mPaint);
}
}
4. xml中的引用
android:id="@+id/third_point_view"
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_below="@id/second_point_view"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="@color/colorPrimary"
app:point_color="#0ff" />
5. 動(dòng)畫實(shí)現(xiàn)
在代碼中獲取PointView的實(shí)例然后調(diào)用startAnimation()方法實(shí)現(xiàn)彈球動(dòng)畫 MainActivity.java
public class MainActivity extends AppCompatActivity {
private PointView pointView;
private PointView secondPointView;
private PointView thirdPointView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pointView = (PointView) findViewById(R.id.point_view);
secondPointView = (PointView) findViewById(R.id.second_point_view);
thirdPointView = (PointView) findViewById(R.id.third_point_view);
findViewById(R.id.animation_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pointView.startAnimation();
secondPointView.startAnimation();
thirdPointView.startAnimation();
}
});
}
}
總結(jié)
以上就是關(guān)于今天自定義Evaluator的全部?jī)?nèi)容,不懂的童鞋可以留言或者發(fā)郵件,另外有需要源碼的小伙伴可以去github下載,點(diǎn)擊跳轉(zhuǎn)到github源碼地址,希望大家多多star!
總結(jié)
以上是生活随笔為你收集整理的android弹球动画,Android动画之自定义Evaluator实现弹球效果的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 自定义键盘字体大小,an
- 下一篇: android contacts电话查询