我的WatchFace太滞后了,加热手表太多了(Android Wear)

时间:2015-03-07 16:15:20

标签: android wear-os watch-face-api

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

问题是,我的表盘正在消耗过多的电池,当手表进入睡眠时它会在1-2分钟内醒来。手表太多了,我希望它顺利运行。它也在重新启动我的手表。

我正在测试Moto 360上的Watch Face。

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 */
    }
}
   }

1 个答案:

答案 0 :(得分:2)

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

<强>更新

请按照此处的文档: http://developer.android.com/training/wearables/watch-faces/drawing.html

WatchFace每分钟自动绘制一次,因此除非您正在进行自定义动画或需要的速度超过每分钟1次,否则您不应该调用“invalidate”。