自定义view属性动画

Posted 芳草玫瑰下

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义view属性动画相关的知识,希望对你有一定的参考价值。


import android.animation.AnimatorSet;
import android.animation.TimeInterpolator;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.Timer;
import java.util.TimerTask;

/**
* Created by Administrator on 2018/5/2.
*/

public class MyView extends View {

private Paint mPaint;
private Point mPoint;
private int mColor;
private ValueAnimator animator1;
private ValueAnimator animator;
private Context context;
private Timer timer;
private int i = 1;

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}

public MyView(Context context) {
super(context);
initPaint();
}

private void initPaint() {
mPaint = new Paint();
mPaint.setColor(0xFFF00000);
mPaint.setAntiAlias(true); // 鎶楅敮榻?
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(mPoint.x, mPoint.y, 60, mPaint);
Log.d("jljs-----x",""+mPoint.x);
Log.d("jljs",""+mPoint.y);
}

public void start(final Context context) {
this.context = context;
animator = ValueAnimator.ofObject(new PointEvaluator(),
new Point(60, 60), new Point(990, 1680));
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mPoint = (Point) animation.getAnimatedValue();
invalidate();
}
});
if (Build.VERSION.SDK_INT < 21){
animator1 = ValueAnimator.ofObject(new TextArgbEvaluator(), 0xFFF000ff, 0xFF00FF00);
}else {
animator1 = ValueAnimator.ofArgb(0xFFFff000, 0xFF000000);
}
animator1.setRepeatCount(ValueAnimator.INFINITE);
animator1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mColor = (int) animation.getAnimatedValue();
mPaint.setColor(mColor);
}
});

AnimatorSet animationSet = new AnimatorSet();
animationSet.setDuration(10000);
animationSet.setInterpolator(new LgDecelerateInterpolator());
animationSet.play(animator).with(animator1);
animationSet.start();

@SuppressLint("HandlerLeak") final Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int obj = (int) msg.obj;
if (obj == 5){
Intent intent = new Intent(context, Main2Activity.class);
context.startActivity(intent);
timer.cancel();
MainActivity mainActivity = (MainActivity) context;
mainActivity.finish();
}
}
};
timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
i++;
Message message = new Message();
message.obj = i;
mHandler.sendMessage(message);

}
};
timer.schedule(task,0,1000);

}

class PointEvaluator implements TypeEvaluator {

@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
Point startPoint = (Point) startValue;
Point endPoint = (Point) endValue;

int x = (int) (startPoint.x + fraction * (endPoint.x - startPoint.x));
int y = (int) (startPoint.y + fraction * (endPoint.y - startPoint.y));

return new Point(x, y);
}
}

class LgDecelerateInterpolator implements TimeInterpolator {

private float background;

public LgDecelerateInterpolator() {
background = 10;
}

@Override
public float getInterpolation(float input) {
return (1 - (float) Math.pow(background, -input));
}
}
private class TextArgbEvaluator implements TypeEvaluator {
//杩欐浠g爜鏄粠婧愮爜涓姞鍑烘潵鐨?
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;

int endInt = (Integer) endValue;
int endA = (endInt >> 24) & 0xff;
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;

return (int) ((startA + (int) (fraction * (endA - startA))) << 24) |
(int) ((startR + (int) (fraction * (endR - startR))) << 16) |
(int) ((startG + (int) (fraction * (endG - startG))) << 8) |
(int) ((startB + (int) (fraction * (endB - startB))));
}
}
public class FloatEvaluator implements TypeEvaluator<Number> {
public Float evaluate(float fraction, Number startValue, Number endValue) {
float startFloat = startValue.floatValue();
return startFloat + fraction * (endValue.floatValue() - startFloat);
}
}
}

以上是关于自定义view属性动画的主要内容,如果未能解决你的问题,请参考以下文章

自定义UI 属性动画

自定义UI 属性动画

自定义view属性动画

android 自定义view+属性动画实现充电进度条

Android自定义View和属性动画完美结合,创造出酷炫圆环动画,带标尺和进度

再谈属性动画——介绍以及自定义Interpolator插值器