自定义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);
}
}
}

原文地址:https://www.cnblogs.com/Nigeria/p/8979305.html

时间: 2024-12-14 15:07:07

自定义view属性动画的相关文章

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

近期项目中需要使用到一种类似手机电池充电进度的动画效果,以前没学属性动画的时候,是用图片+定时器的方式来完成的,最近一直在学习动画这一块,再加上复习一下自定义view的相关知识点,所以打算用属性动画和自定义view的方式来完成这个功能,将它开源出来,供有需要的人了解一下相关的内容. 本次实现的功能类似下面的效果: 接下来便详细解析一下如何完成这个功能,了解其中的原理,这样就能举一反三,实现其他类似的动画效果了. 详细代码请看大屏幕 https://github.com/crazyandcoder

UI--从学习styleable自定义view属性到一点儿更有意思的尝试

<代码里的世界> -UI篇 用文字札记描绘自己 android学习之路 转载请保留出处 by Qiao http://blog.csdn.net/qiaoidea/article/details/45599593 [导航] - 多行文本折叠展开 自定义布局View实现多行文本折叠和展开 1.概述 前面封装view的时候用到了自定义属性,觉得有必要单独讲一下这部分,但是呢,又不想向其他文章一样千篇一律地写这些东西.所以呢,后便会加一些临时的发散思维,引用点有意思的东西.分享东西嘛,随性点儿. 回

android 自定义View属性

在android开发过程中,用到系统的View时候可以通过XML来定义一些View的属性.比如ImageView: android:src  和android:scaleType为ImageView指定了图片源和图片缩放类型. 其实我们也可以自定义图片的这种属性. 下面以自定义标题栏为例,简单说明下自定义View属性. 比如在项目中,经常会用到标题栏,左边是返回,中间是标题,右边是下一步.如下图: 如果,每一次用到标题都在XML里面进行布局,那就太麻烦了.我们可以自定义一个标题栏. 自定义Vie

【android自定义控件】自定义View属性

1.自定义View的属性 2.在View的构造方法中获得我们自定义的属性 3.重写onMesure 4.重写onDraw 3这个步骤不是必须,当然了大部分情况下还是需要重写的. 1.自定义View的属性,首先在res/values/  下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式. <?xml version="1.0" encoding="utf-8"?> <resources> <attr name=&

自定义View淡出动画

//AllGestureView为自定义view AllGestureView *gestureView=[[AllGestureView alloc]init]; gestureView.frame=[UIScreen mainScreen].bounds; //先设置自定义view的透明度为0 gestureView.alpha=0; [[UIApplication sharedApplication].keyWindow addSubview:gestureView]; /****动画部分

Android自定义 view属性

第一种 /MainActivity/res/values/attrs.xml   <?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="MyCircle"> <attr name="radius" format="integer"/> <attr name=&

android自定义view属性

方法一: MyView.class package com.bwie.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class MyView ext

Android 自定义View背景动画 流程简读 &lt;2&gt;

这一篇主要根据上一篇的大致说明,我相信如果看完这一篇,对开发自定义View将会有很大的帮助, 先介绍ColorStateList和StateListDrawable两个类: ColorStateList说明:https://developer.android.com/reference/android/content/res/ColorStateList.html StateListDrawable说明:https://developer.android.com/reference/androi

自定义view 柱状图 动画

1 这个是朋友写的柱状图@曹瑀宏 package com.car300.component; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ValueAnimator; import android.content.Context; import android.content.res.TypedArray; import