2013-11-25 54 views
1

我得到了一些小故障是didnt动画期间与变色动画视图

我的继承人动画类

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

setProgress方法从RimProgress

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 = (RimProgress) view.findViewById(R.id.rimProgress); 
RimProgressAnimation anim = new RimProgressAnimation(rimProgress, 0, userPoints); 
anim.setDuration(1000); 
rimProgress.startAnimation(anim); 

RimProgress定制View工作方式类似于进度条,当生命的它并没有改变颜色,但是当我setProgress没有asnimation它设置合适的颜色

UPDATE

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

这个动画对我来说很好,分享你的自定义View RimProgress的代码,问题可以在这里。此外,代码中“userPoints”(最小 - 最大值)的值范围是多少? –

+0

已更新; userPoints min是0,max没有定义'convert'方法计算它应该绘制多少 – Mariusz

回答

0

setProgress固定它通过呼叫progressPaint.setColor(progressColor);invalidate();