-1

我使用 CanvasWatchFaceService 创建了一个表盘。我已经创建了表盘。并为手机创建了配置应用程序。

问题是,我的表盘耗电太多,当手表进入睡眠状态时,它会在 1-2 分钟内唤醒。并且过于滞后手表,我希望它运行顺畅。它也在重新启动我的手表。

我正在 Moto 360 上测试表盘。

public class DigitalWatchFaceService extends CanvasWatchFaceService {

String[] days = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };
String[] months = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG",
        "SEP", "OCT", "NOV", "DEC" };

Prefs prefs;

@Override
public Engine onCreateEngine() {
    /* provide your watch face implementation */
    return new Engine();
}

/* implement service callback methods */
private class Engine extends CanvasWatchFaceService.Engine {

    boolean hr24Format = false;
    int hourColor = -1, minuteColor = -16742014, secondColor = -16723470,
            backgroundColor = -16761271;
    int width, centerX, centerY;

    Canvas canvas;

    Typeface typeFace;
    Paint hourPaint;
    Paint minutePaint;
    Paint datePaint;
    Paint hourCircle, minuteCircle, secondCircle;
    RectF ovalHour, ovalMinute, ovalSecond;
    Calendar cal;

    int day, month, date;
    Rect hourBounds, minuteBounds, dateBounds;
    String mHour = "00", mMinutes, mDate;
    int hh, mm, ss;

    boolean inAmbientMode;

    @Override
    public void onCreate(SurfaceHolder holder) {
        super.onCreate(holder);
        /* initialize your watch face */
        setWatchFaceStyle(new WatchFaceStyle.Builder(
                DigitalWatchFaceService.this)
                .setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
                .setBackgroundVisibility(
                        WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
                .setShowSystemUiTime(false).build());
        prefs = new Prefs(DigitalWatchFaceService.this);
        typeFace = Typeface.createFromAsset(getAssets(), "roboto.ttf");

        initAssets();
        setAssets();

    }

    private void setAssets() {
        // TODO Auto-generated method stub
        hourPaint.setColor(hourColor);
        hourPaint.setTextSize(70);
        hourPaint.setTypeface(typeFace);
        hourPaint.setAntiAlias(true);

        minutePaint.setColor(minuteColor);
        minutePaint.setTextSize(70);
        minutePaint.setTypeface(typeFace);
        minutePaint.setAntiAlias(true);

        datePaint.setColor(hourColor);
        datePaint.setTextSize(20);
        datePaint.setTypeface(typeFace);
        datePaint.setAntiAlias(true);

        hourCircle.setColor(hourColor);
        hourCircle.setStyle(Paint.Style.FILL);
        hourCircle.setStyle(Paint.Style.STROKE);
        hourCircle.setStrokeWidth(7);
        hourCircle.setAntiAlias(true);

        minuteCircle.setColor(minuteColor);
        minuteCircle.setStyle(Paint.Style.FILL);
        minuteCircle.setStyle(Paint.Style.STROKE);
        minuteCircle.setStrokeWidth(5);
        minuteCircle.setAntiAlias(true);

        secondCircle.setColor(secondColor);
        secondCircle.setStyle(Paint.Style.FILL);
        secondCircle.setStyle(Paint.Style.STROKE);
        secondCircle.setStrokeWidth(2);
        secondCircle.setAntiAlias(true);

        ovalHour.set(32, 32, width - 32, width - 32);
        ovalMinute = new RectF();
        ovalMinute.set(22, 22, width - 22, width - 22);
        ovalSecond = new RectF();
        ovalSecond.set(15, 15, width - 15, width - 15);
    }

    private void initAssets() {
        // TODO Auto-generated method stub
        hourPaint = new Paint();
        minutePaint = new Paint();
        datePaint = new Paint();

        hourCircle = new Paint();

        minuteCircle = new Paint();

        secondCircle = new Paint();

        ovalHour = new RectF();

        hourBounds = new Rect();
        minuteBounds = new Rect();
        dateBounds = new Rect();
    }

    @Override
    public void onPropertiesChanged(Bundle properties) {
        super.onPropertiesChanged(properties);
        /* get device features (burn-in, low-bit ambient) */
    }

    @Override
    public void onTimeTick() {
        super.onTimeTick();
        /* the time changed */
    }

    @Override
    public void onAmbientModeChanged(boolean inAmbientMode) {
        super.onAmbientModeChanged(inAmbientMode);
        /* the wearable switched between modes */
        this.inAmbientMode = inAmbientMode;
        if (inAmbientMode) {
            backgroundColor = -16777216;
            hourColor = -1;
            minuteColor = -1;
        } else {
            hourColor = prefs.getPrefs(Prefs.hourColor, -1);
            minuteColor = prefs.getPrefs(Prefs.minuteColor, -16742014);
            backgroundColor = prefs.getPrefs(Prefs.backgroundColor,
                    -16761271);
        }
        setAssets();
    }

    private final String HOUR_FORMAT = "hh";
    private final String HOUR_FORMAT_24 = "kk";
    private final String MINUTE_FORMAT = "mm";

    @Override
    public void onDraw(Canvas canvas, Rect bounds) {
        /* draw your watch face */
        this.canvas = canvas;
        width = bounds.width();
        centerX = bounds.centerX();
        centerY = bounds.centerY();
        backgroundColor = prefs.getPrefs(Prefs.backgroundColor, -16761271);
        canvas.drawColor(backgroundColor);
        drawForNormalMode();
    }

    public void drawForNormalMode() {
        hr24Format = prefs.getPrefs(Prefs.format24Key, false);
        if (!inAmbientMode) {
            hourColor = prefs.getPrefs(Prefs.hourColor, -1);
            minuteColor = prefs.getPrefs(Prefs.minuteColor, -16742014);
            secondColor = prefs.getPrefs(Prefs.secondColor, -16723470);
            backgroundColor = prefs.getPrefs(Prefs.backgroundColor,
                    -16761271);
        }
        setTimeNormal();
    }

    public void setTimeNormal() {
        cal = Calendar.getInstance();
        day = cal.get(Calendar.DAY_OF_WEEK);
        month = cal.get(Calendar.MONTH);
        date = cal.get(Calendar.DATE);

        // draw hour
        if (hr24Format == false)
            mHour = new SimpleDateFormat(HOUR_FORMAT).format(cal.getTime());
        else
            mHour = new SimpleDateFormat(HOUR_FORMAT_24).format(cal
                    .getTime());

        hourPaint.getTextBounds(mHour, 0, mHour.length(), hourBounds);
        canvas.drawText(mHour, centerX - hourBounds.width() - 15, centerY
                - hourBounds.centerY(), hourPaint);

        // draw minute
        mMinutes = new SimpleDateFormat(MINUTE_FORMAT)
                .format(cal.getTime());
        minutePaint.getTextBounds(mMinutes, 0, mMinutes.length(),
                minuteBounds);
        canvas.drawText(mMinutes, centerX,
                centerY - minuteBounds.centerY(), minutePaint);

        // draw date
        if (!inAmbientMode) {
            mDate = days[day - 1] + ", " + months[month] + " " + date;
            datePaint.getTextBounds(mDate, 0, mDate.length(), dateBounds);
            canvas.drawText(mDate, centerX - dateBounds.centerX(), centerY
                    + hourBounds.height() + 10, datePaint);
        }

        // draw minute circle
        mm = cal.get(Calendar.MINUTE);
        circularMinute(mm, 60);

        // draw hour circle
        hh = cal.get(Calendar.HOUR);
        circularHour((hh * 60) + mm, 720);

        // draw second circle
        if (!inAmbientMode)
            ss = cal.get(Calendar.SECOND);
        circularSecond(ss, 60);

        invalidate();
    }

    private void circularHour(int number, int max) {
        canvas.drawArc(ovalHour, 270, ((number * 360) / max), false,
                hourCircle);
    }

    private void circularMinute(int number, int max) {
        canvas.drawArc(ovalMinute, 270, ((number * 360) / max), false,
                minuteCircle);
    }

    private void circularSecond(int number, int max) {
        canvas.drawArc(ovalSecond, 270, ((number * 360) / max), false,
                secondCircle);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);
        /* the watch face became visible or invisible */
    }
}
   }
4

1 回答 1

2

您在“onDraw”(“setTimeNormal”的最后一行)中调用“invalidate”,因此您的表盘将不断地一遍又一遍地重新绘制。

更新:

遵循此处的文档:http: //developer.android.com/training/wearables/watch-faces/drawing.html

WatchFace 每分钟自动绘制一次,因此您不应调用“无效”,除非您正在执行自定义动画或需要更新速度快于每分钟 1 次。

于 2015-03-07T18:18:58.990 回答