Android Wear Watch Face开发颜色

时间:2015-07-29 22:23:16

标签: java android wear-os

我正在尝试开发表盘,我能够添加时间,日期和其他信息并定位文本。我的风格很麻烦,主要是颜色。我有一个红色文本和另一个白色文本。进入环境模式并返回交互模式后,颜色为白色或灰色,具体取决于以下代码。我只能让它恢复到一种颜色。

SimpleWatchFaceService.java

$Color = "white";
$Selection = $_POST["Colour"];
if($Selection == "Pink"){
    $Color = "pink";
}elseif($Selection == "Blue"){
    $Color = "blue";
}elseif($Selection == "Green"){
    $Color = "green";
}elseif($Selection == "Grey"){
    $Color = "SlateGrey";
}

SimpleWatchFace.java

package com.me.me.androidwearweather;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.support.wearable.watchface.WatchFaceStyle;
import android.view.SurfaceHolder;

import java.util.concurrent.TimeUnit;

public class SimpleWatchFaceService extends CanvasWatchFaceService {
    @Override
    public Engine onCreateEngine() {
        return new SimpleEngine();
    }

    private class SimpleEngine extends CanvasWatchFaceService.Engine {

        private final long TICK_PERIOD_MILLIS = TimeUnit.SECONDS.toMillis(1);
        private Handler timeTick;
        private SimpleWatchFace watchFace;

        @Override
        public void onCreate(SurfaceHolder holder) {
            super.onCreate(holder);

            setWatchFaceStyle(new WatchFaceStyle.Builder(SimpleWatchFaceService.this)
                    .setCardPeekMode(WatchFaceStyle.PEEK_MODE_SHORT)
                    .setAmbientPeekMode(WatchFaceStyle.AMBIENT_PEEK_MODE_HIDDEN)
                    .setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
                    .setShowSystemUiTime(false)
                    .build());
            timeTick = new Handler(Looper.myLooper());
            startTimerIfNecessary();
            watchFace = SimpleWatchFace.newInstance(SimpleWatchFaceService.this);

        }

        private void startTimerIfNecessary() {
            timeTick.removeCallbacks(timeRunnable);
            if (isVisible() && !isInAmbientMode()) {
                timeTick.post(timeRunnable);
            }
        }

        private final Runnable timeRunnable = new Runnable() {
            @Override
            public void run() {
                onSecondTick();

                if (isVisible() && !isInAmbientMode()) {
                    timeTick.postDelayed(this, TICK_PERIOD_MILLIS);
                }
            }
        };

        private void onSecondTick() {
            invalidateIfNecessary();
        }

        private void invalidateIfNecessary() {
            if (isVisible() && !isInAmbientMode()) {
                invalidate();
            }
        }

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            startTimerIfNecessary();
        }

        @Override
        public void onDraw(Canvas canvas, Rect bounds) {
            super.onDraw(canvas, bounds);
            watchFace.draw(canvas, bounds);
        }

        @Override
        public void onAmbientModeChanged(boolean inAmbientMode) {
            super.onAmbientModeChanged(inAmbientMode);
            watchFace.setAntiAlias(!inAmbientMode);
            watchFace.setColor(inAmbientMode ? Color.GRAY : Color.WHITE);
            // THIS IS WERE I THINK THE PROBLEM IS I tried the method on top and bottom of this comment
            // if(inAmbientMode){
                // watchFace.setColor(Color.GRAY);
            // }
            invalidate();
        }

        @Override
        public void onTimeTick() {
            super.onTimeTick();
            invalidate();
        }

        @Override
        public void onDestroy() {
            timeTick.removeCallbacks(timeRunnable);
            super.onDestroy();
        }

    }
}

我还想知道是否可以使用多种字体和颜色设置样式。

1 个答案:

答案 0 :(得分:0)

编辑2:好的,你可以在每次更改模式时实例化一个新的表盘(Java会自动垃圾收集旧表,所以不用担心内存)或者,我个人会这样做。

在SimpleWatchFace类中添加两个新函数来替换 setColor 函数

public void setTimeColor(int color) {
        timePaint.setColor(color);
}

public void setDateColor(int color) {
        datePaint.setColor(color);
}

然后将您的功能更改为此

    @Override
    public void onAmbientModeChanged(boolean inAmbientMode) {

        super.onAmbientModeChanged(inAmbientMode);
        watchFace.setAntiAlias(!inAmbientMode);

        if(inAmbientMode)
        {
            watchFace.setTimeColor(Color.BLACK);
            watchFace.setDateColor(Color.GREY);
        }
        else
        {
            watchFace.setTimeColor(Color.RED);
            watchFace.setDateColor(Color.WHITE);
        }
    }

当手表进入环境模式时,屏幕会自动变为黑白(或者您正在经历的黑色和灰色)

  

注意:在低位环境模式下,系统无法可靠地渲染   图像中的颜色

来自The Android Webpage