动画视图与更改颜色

时间:2013-11-25 12:46:48

标签: 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;
    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 = (RimProgress) view.findViewById(R.id.rimProgress);
RimProgressAnimation anim = new RimProgressAnimation(rimProgress, 0, userPoints);
anim.setDuration(1000);
rimProgress.startAnimation(anim);
自定义RimProgress中的

View与progressBar类似,当动画时它不会改变颜色但是当setProgress没有asnimation时它会设置正确的颜色

更新

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

1 个答案:

答案 0 :(得分:0)

我在progressPaint.setColor(progressColor);

中的invalidate();之前致电setProgress修正了它