package com.example.sdj.testfn.attributeAnimation;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
/**
* 项目名称:code
* 类描述:
* 创建人:sdj
* 创建时间:2019/6/25 15:46
* 修改人:sdj
* 修改时间:2019/6/25 15:46
* 修改备注:
*/
public class PercentageCircle extends View {
private float sweepAngle;
public float getSweepAngle() {
return sweepAngle;
}
public void setSweepAngle(float sweepAngle) {
this.sweepAngle = sweepAngle;
invalidate();
}
public PercentageCircle(Context context) {
super(context);
}
public PercentageCircle(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public PercentageCircle(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// canvas.drawColor(Color.parseColor("#f200ff00"));
Paint p = new Paint();
p.setAntiAlias(true);
p.setStyle(Paint.Style.STROKE);
// p.setStrokeWidth(4);
p.setColor(Color.parseColor("#ff0000"));
Paint textP = new Paint();
textP.setAntiAlias(true);
textP.setStyle(Paint.Style.STROKE);
textP.setTextSize(16);
textP.setColor(Color.parseColor("#ff0000"));
Paint bg= new Paint();
bg.setAntiAlias(true);
bg.setStyle(Paint.Style.FILL);
bg.setColor(Color.parseColor("#F100ff00"));
canvas.drawArc(4,4,getWidth()-4,getHeight()-4,0,sweepAngle,false,p);
int v = (int) (sweepAngle / 360 *100);
String num = v+"%";
float x = getWidth()/2-TextSizeUtils.getFontWeight(num,p)/2;
int y = getHeight()/2+TextSizeUtils.getFontHeight(num,p)/2;
canvas.drawText(num,x,y,textP);
// canvas.drawRect(0,0,getWidth(),getHeight(),bg);
}
}
以百分比进度条为例
属性动画在自定义View中的应用
1,添加自定义属性
sweepAngle --代表加载的角度(0-360)
2,添加get,set方法
3,根据sweepAngle 绘制,完成属性动画自定义View部分的内容
4,开启动画:
ObjectAnimator.ofFloat(percentageCircle,"sweepAngle",0,360).setDuration(4000).start();
属性动画的操作流程大概就这样,接下来分析一下自定义Evaluator
默认情况下:
ofInt()---对应---IntEvaluator
ofFloat()---对应---FloatEvaluator
通过setEvaluator 设置自定义的Evaluator,继承TypeEvaluator
package com.example.sdj.testfn.attributeAnimation;
import android.animation.TypeEvaluator;
import android.graphics.Color;
/**
* 项目名称:code
* 类描述:
* 创建人:sdj
* 创建时间:2019/6/25 15:23
* 修改人:sdj
* 修改时间:2019/6/25 15:23
* 修改备注:
*/
public class MyArgbEvaluator implements TypeEvaluator<Integer> {
@Override
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
float [] startHsv = new float[3];
float [] endHsv = new float[3];
float [] outHsv = new float[3];
Color.colorToHSV(startValue,startHsv);
Color.colorToHSV(endValue,endHsv);
outHsv[0] = startHsv[0]+(endHsv[0]-startHsv[0])*fraction;
outHsv[1] = startHsv[1]+(endHsv[1]-startHsv[1])*fraction;
outHsv[2] = startHsv[2]+(endHsv[2]-startHsv[2])*fraction;
int alpha = 0x99;
return Color.HSVToColor(alpha,outHsv);
}
}
重写evaluate方法,编写估值算法,泛型可以为Point,或者其他自定义类型