Android 具有更改颜色的动画视图

Android 具有更改颜色的动画视图,android,animation,android-animation,Android,Animation,Android Animation,我遇到了一些小问题,在动画中并没有改变视图的颜色 这是我的动画课 public class RimProgressAnimation extends Animation { private RimProgress view; private int from; private int to; public RimProgressAnimation(RimProgress view, int from, int to) { super(); this.view = view;

我遇到了一些小问题,在动画中并没有改变视图的颜色

这是我的动画课

public class RimProgressAnimation extends Animation {

private RimProgress view;
private int from;
private int  to;

public RimProgressAnimation(RimProgress view, int from, int to) {
    super();
    this.view = view;
    this.from = from;
    this.to = to;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    super.applyTransformation(interpolatedTime, t);
    float value = from + (to - from) * interpolatedTime;
    view.setProgress((int) Math.ceil(value));
}
}
来自RimProgress的setProgress方法

public void setProgress(int progress) {
    this.progress = progress;
    double proc = ((double)progress/max)*100.0; // ile procent max stanowi progress

    if(proc <= 25.0) progressColor = res.getColor(R.color.scaleRed);
    if(25.0 < proc && proc <= 50.0) progressColor = res.getColor(R.color.scaleOrange);
    if(50.0 < proc && proc <= 75.0) progressColor = res.getColor(R.color.scaleYellow);
    if(75.0 < proc && proc < 100) progressColor = res.getColor(R.color.scaleLightGreen);
    if(100 <= proc) progressColor = res.getColor(R.color.scaleGreen);

    invalidate();
}
RimProgress rimProgress = (RimProgress) view.findViewById(R.id.rimProgress);
RimProgressAnimation anim = new RimProgressAnimation(rimProgress, 0, userPoints);
anim.setDuration(1000);
rimProgress.startAnimation(anim);
RimProgress
在自定义
视图中,它的工作原理类似于progressBar,当设置动画时,它不会更改颜色,但当i
setProgress
未设置动画时,它会设置正确的颜色

更新

public class RimProgress extends View {

int progress = 0;
private int color = Color.GRAY;
private int width = 5;
private int max = 100;
private Paint progressPaint = new Paint();

private int progressColor;
private int progressWidth;
private int rimColor;
private int rimWidth;
private RectF rimBounds;
private Paint rimPaint = new Paint();
private Resources res;

public RimProgress(Context context, AttributeSet attrs) {
    super(context, attrs);

    parseAttributes(context.obtainStyledAttributes(attrs, R.styleable.RimProgress));
    res = context.getResources();
}

private void parseAttributes(TypedArray att) {
    progressColor = (int) att.getColor(R.styleable.RimProgress_color, color);
    progressWidth = rimWidth = (int) att.getDimension(R.styleable.RimProgress_width, width);
    max = (int) att.getInt(R.styleable.RimProgress_max, max);
    rimColor = (int) att.getColor(R.styleable.RimProgress_background, color);
}

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    int paddingTop = this.getPaddingTop();
    int paddingBottom = this.getPaddingBottom();
    int paddingLeft = this.getPaddingLeft();
    int paddingRight = this.getPaddingRight();

    rimBounds = new RectF(paddingLeft + progressWidth,
            paddingTop + progressWidth,
            this.getLayoutParams().width - paddingRight - progressWidth,
            this.getLayoutParams().height - paddingBottom - progressWidth);

    progressPaint.setColor(progressColor);
    progressPaint.setAntiAlias(true);
    progressPaint.setStyle(Style.STROKE);
    progressPaint.setStrokeWidth(progressWidth);

    rimPaint.setColor(rimColor);
    rimPaint.setAntiAlias(true);
    rimPaint.setStyle(Style.STROKE);
    rimPaint.setStrokeWidth(rimWidth);
    invalidate();
}

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawArc(rimBounds, 360, 360, false, rimPaint);
    canvas.drawArc(rimBounds, -90, convert(), false, progressPaint);
}

public void setProgress(int progress) {
    this.progress = progress;
    double proc = ((double)progress/max)*100.0;

    if(proc <= 25.0) progressColor = res.getColor(R.color.scaleRed);
    if(25.0 < proc && proc <= 50.0) progressColor = res.getColor(R.color.scaleOrange);
    if(50.0 < proc && proc <= 75.0) progressColor = res.getColor(R.color.scaleYellow);
    if(75.0 < proc && proc < 100) progressColor = res.getColor(R.color.scaleLightGreen);
    if(100 <= proc) progressColor = res.getColor(R.color.scaleGreen);

    invalidate();
}

public void setMax(int max) {
    this.max = max;
}

public int getMax() {
    return this.max;
}

public void setProgressColor(int color) {
    progressColor = color;
    invalidate();
}

private int convert() {
    double a = (progress*100)/((double) max);
    return (int) Math.ceil(360.0*(a/100.0));
}
}
public类进程扩展视图{
int progress=0;
私有int color=color.GRAY;
私有整数宽度=5;
私人int最大值=100;
private Paint progressPaint=新油漆();
私人色彩;
私有宽度;
私人色彩;
私有宽度;
私有RectF边界;
private Paint rimPaint=新油漆();
私人资源;
公共进程(上下文、属性集属性){
超级(上下文,attrs);
parseAttributes(context.getainstyledattributes(attrs,R.styleable.RimProgress));
res=context.getResources();
}
私有属性(类型Darray att){
progressColor=(int)att.getColor(R.styleable.RimProgress\u color,color);
progressWidth=rimWidth=(int)att.getDimension(R.styleable.RimProgress\u width,width);
max=(int)att.getInt(R.styleable.RimProgress_max,max);
rimColor=(int)att.getColor(R.styleable.rimColor\u background,color);
}
@凌驾
公共空间和附加的Towindow(){
super.onAttachedToWindow();
int paddingTop=this.getPaddingTop();
int paddingBottom=this.getPaddingBottom();
int paddingLeft=this.getPaddingLeft();
int paddingRight=this.getPaddingRight();
rimBounds=新的RectF(paddingLeft+progressWidth,
paddingTop+progressWidth,
this.getLayoutParams().width-paddingRight-progressWidth,
this.getLayoutParams().height-paddingBottom-progressWidth);
progressPaint.setColor(progressColor);
progressPaint.setAntiAlias(真);
progressPaint.setStyle(Style.STROKE);
progressPaint.设置行程宽度(progressWidth);
rimPaint.setColor(rimColor);
rimPaint.setAntiAlias(真);
rimPaint.setStyle(Style.STROKE);
边缘油漆。设置行程宽度(边缘宽度);
使无效();
}
受保护的void onDraw(画布){
super.onDraw(帆布);
画布.drawArc(边框,360,360,假,边框绘制);
drawArc(rimBounds,-90,convert(),false,progressPaint);
}
公共void setProgress(int progress){
这个。进步=进步;
双进程=((双)进程/最大值)*100.0;

如果(proc我通过调用
progressPaint.setColor(progressColor);
invalidate()之前;
setProgress

中修复了它,那么这个动画对我来说运行良好,请共享自定义视图RimProgress的代码,问题可能在于此。另外,“用户点”(最小最大值)的值范围是多少在您的代码中?已更新;userPoints min为0,max不是define
convert
method count它应该绘制多少