-7

我正在尝试制作一个计时器应用程序,它可以直观地显示不同颜色的剩余时间。像这样 -

在此处输入图像描述

我用自己的观点制作了模拟时钟,但不知道如何制作这种变色的东西。如果任何机构可以提供任何建议或示例代码,那将是非常棒的。我找到了一个代码,但它是 CoreDova,我是在本地做的。https://github.com/shaungallagher/timer-for-kids

4

2 回答 2

1

我最终形成了自己的看法。它看起来像这样-

public class TimerView extends View {

private static final String TAG = TimerView.class.getName();

private static final int RADIUS = 600;
private static final int BACKGROUND_COLOR = 0x50ff0000;
private static final int FOREGROUND_COLOR = 0xa0ff0000;

private static final float STARTING_ANGLE = -90f;
private static final float FULL_CIRCLE = 360f;

private ShapeDrawable circle;
private ShapeDrawable arc;

private float lastFraction;

public TimerView(Context context) {
    super(context);
    this.init();
}

public TimerView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);
    this.init();
}

private void init() {
    this.circle = new ShapeDrawable(new OvalShape());
    this.circle.getPaint().setColor(BACKGROUND_COLOR);
    this.circle.setBounds(0, 0, RADIUS, RADIUS);
    this.lastFraction = 0f;
    this.arc = new ShapeDrawable(new ArcShape(STARTING_ANGLE,
            this.lastFraction));
    this.arc.getPaint().setColor(FOREGROUND_COLOR);
    this.arc.setBounds(0, 0, RADIUS, RADIUS);
}

protected void onDraw(Canvas canvas) {
    this.circle.draw(canvas);
    this.arc.draw(canvas);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.setMeasuredDimension(RADIUS, RADIUS);
}

public void setFraction(float fraction) {
    if (fraction < 0f || fraction > 1.f) {
        throw new IllegalArgumentException("Out of range: " + fraction);
    }
    if (fraction != this.lastFraction) {
        float sweepingAngle = FULL_CIRCLE * fraction;
        this.arc.setShape(new ArcShape(STARTING_ANGLE, sweepingAngle));
        this.postInvalidate();
        this.lastFraction = fraction;
    } else {
        Log.v(TAG, "Error");
    }
 }
}

并调用布局 -

<com.abc.xyz.TimerView
    android:id="@+id/pieChart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp" />

要更改值需要调用活动或片段 -

pieChart.setFraction((float) .99);

谢谢大家这么多的支持

于 2017-05-21T12:00:42.533 回答
1

尝试绘制一个自定义视图圆 (using canvas.drawCircle(...)),然后在每次时间(角度)变化时重新绘制一个弧,同时改变背景颜色。 (using canvas.drawArc(...))你需要自己弄清楚其余的。

于 2017-05-21T10:05:42.503 回答