Siv*_*a K 5 android canvas progress android-activity
我的应用程序,当我进入活动时,我正在显示一个计时器视图(看起来像一个进度条),它根据服务器的计时器值运行.在开始时(例如40秒),计时器将为绿色,中间(例如25秒)变为黄色,最后变为红色(例如10秒),以下是我正在使用的代码
public class TimerView extends View
{
private LinearInterpolator mInterpolator;
private Transformation mTransformation;
private AlphaAnimation mAnimation;
int orange = Color.rgb(255, 153, 0);
private int green = Color.rgb(107, 155, 21);
private int red = Color.rgb(194, 21, 30);
private int yellow = Color.rgb(225, 170, 0);
private RectF rect;
private Integer mTimerVal = 0;
private Paint tmrPaint;
private Paint txtPaint;
private float mDensity = 1.0f;
public TimerView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mDensity = getResources().getDisplayMetrics().density;
txtPaint = new Paint();
tmrPaint = new Paint();
tmrPaint.setAntiAlias(true);
txtPaint.setColor(Color.WHITE);
txtPaint.setTextSize(15.0f * mDensity);
txtPaint.setAntiAlias(true);
if (!isInEditMode()) {
txtPaint.setTypeface(Typefaces.get(getContext(), "OpenSans-CondBold.ttf"));
}
rect = new RectF(0, 8 * mDensity, 0, 22 * mDensity);
mInterpolator = new LinearInterpolator();
// Timer Animation
mTransformation = new Transformation();
mAnimation = new AlphaAnimation(1.0f, 0.0f);
mAnimation.setRepeatMode(AlphaAnimation.RESTART);
mAnimation.setRepeatCount(0);
mAnimation.setDuration(0);
mAnimation.setInterpolator(mInterpolator);
mAnimation.setStartTime(Animation.START_ON_FIRST_FRAME);
mAnimation.cancel();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// System.out.println("onLayout: " + left + " top: " + top + " r: " +
// right + " b: " + bottom);
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// System.out.println("onLayout onMeasure: " + widthMeasureSpec + " x "
// + heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas)
{
long time = getDrawingTime();
mAnimation.getTransformation(time, mTransformation);
float scale = mTransformation.getAlpha();
int rem = (int) Math.floor(mTimerVal * scale);
if (rem > 0)
{
if (rem < 5)
{
tmrPaint.setColor(red);
}
else if (rem <= 10)
{
tmrPaint.setColor(yellow);
}
else
{
tmrPaint.setColor(green);
}
rect.right = this.getWidth() - mDensity * 8;
rect.left = (1 - scale) * (this.getWidth());
canvas.drawRoundRect(rect, 4 * mDensity, 4 * mDensity, tmrPaint);
canvas.drawText(String.format("%d", rem), 13 * mDensity, 20 * mDensity, txtPaint);
}
if (scale > 0.0f) {
postInvalidateDelayed(40);
}
super.onDraw(canvas);
}
public void setTimer(TimerTag timer) {
if (timer.getTimerValue() > 0) {
mAnimation.reset();
mTimerVal = timer.getTimerValue();
mAnimation.setDuration(timer.getTimerValue() * 1000);
mAnimation.start();
postInvalidate();
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是当我进入活动时,如果服务器响应低于25且高于10我的计时器显示黄色,但它从结束开始,我希望它从视图的中间或其他位置开始,我无法改变画布的起点,任何建议......
由于矩形坐标(尤其是左上角)是根据当前 alpha 值计算的,因此起始 alpha 值AlphaAnimation应基于已经过去的时间。
例如,如果计时器设置为 30 秒,并且用户在第 10 秒进入 Activity,则动画需要播放 20 秒。所以起始 alpha 值应该是 20/30,即 0.67f。AlphaAnimation 应实例化如下:
mAnimation = new AlphaAnimation(0.67f, 0.0f);
Run Code Online (Sandbox Code Playgroud)