博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自定义view属性动画
阅读量:5085 次
发布时间:2019-06-13

本文共 4717 字,大约阅读时间需要 15 分钟。

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);
}
}
}

转载于:https://www.cnblogs.com/Nigeria/p/8979305.html

你可能感兴趣的文章
nio通道
查看>>
ORA-12154: TNS: 无法解析指定的连接标识符
查看>>
Java IO模型
查看>>
【2018.11.23】2018WCTest(7)
查看>>
Tomcat中catalina.bat详解
查看>>
Python的hasattr() getattr() setattr() 函数使用方法详解
查看>>
Java注解简单学习
查看>>
ZooKeeper系列3:ZooKeeper命令、命令行工具及简单操作
查看>>
VMware exsi 虚拟化嵌套
查看>>
java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
查看>>
div模态层示例
查看>>
转:ASP.NET发布WebService操作流程
查看>>
.NET短信接口 实例
查看>>
Visual Studio 常用快捷键
查看>>
【代码笔记】Web-ionic-创建APP的架构
查看>>
TimePicker 时间选择器
查看>>
开始使用MarkDown写博客
查看>>
ui-router 1.0 002 未登录跳转到login
查看>>
HTML加固3——超文本
查看>>
js-ES6学习笔记-函数的扩展
查看>>