gpt4 book ai didi

android 计时器 View (就像进度条一样)在 Canvas 中更新颜色

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:57 25 4
gpt4 key购买 nike

我的应用程序,当我进入一个 Activity 时,我会显示一个计时器 View (看起来像一个进度条),它根据来自服务器的计时器值运行。在开始时(例如 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();
}

}
}

现在我的问题是当我进入 Activity 时,如果服务器响应低于 25 和高于 10,我的计时器显示黄色,但它从末尾开始,我希望它从中间或其他位置开始 View ,我无法更改 Canvas 中的起点,任何建议.....

最佳答案

由于矩形坐标(尤其是左上坐标)是根据当前的 alpha 值计算的,因此 AlphaAnimation 的起始 alpha 值应该基于已经过去的时间。

例如如果定时器设置为 30 秒并且用户在第 10 秒进入 Activity ,则动画需要播放 20 秒。所以起始 alpha 值应该是 20/30,即 0.67f。 AlphaAnimation 应该如下实例化:

mAnimation = new AlphaAnimation(0.67f, 0.0f);

关于android 计时器 View (就像进度条一样)在 Canvas 中更新颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23881656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com