Android佩戴表盘开发颜色



我正在尝试开发表盘,并且我能够添加时间,日期和其他信息并定位文本。我不喜欢款式,主要是颜色。我有一个红色文本,另一个白色文本。在进入环境模式并返回交互模式后,颜色是白色还是灰色取决于下面的代码。我只能让它变回一种颜色。

SimpleWatchFaceService.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();
        }
    }
}

SimpleWatchFace.java

package com.me.me.androidwearweather;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.format.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class SimpleWatchFace {
    private final Paint timePaint;
    private final Paint datePaint;
    private final Time time;
    public static SimpleWatchFace newInstance(Context context) {
        Paint timePaint = new Paint();
        timePaint.setColor(Color.RED);
        timePaint.setTextSize(context.getResources().getDimension(R.dimen.time_size));
        timePaint.setAntiAlias(true);
        Paint datePaint = new Paint();
        datePaint.setColor(Color.WHITE);
        datePaint.setTextSize(context.getResources().getDimension(R.dimen.date_size));
        datePaint.setAntiAlias(true);
        return new SimpleWatchFace(timePaint, datePaint, new Time());
    }
    SimpleWatchFace(Paint timePaint, Paint datePaint, Time time) {
        this.timePaint = timePaint;
        this.datePaint = datePaint;
        this.time = time;
    }
    public void draw(Canvas canvas, Rect bounds) {
        time.setToNow();
        canvas.drawColor(Color.BLACK);
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat twelvehour = new SimpleDateFormat("h");
        SimpleDateFormat ampm = new SimpleDateFormat("a");
        String TimeAmPmNoSec = String.format("%2s:%02d %2s", twelvehour.format(cal.getTime()), time.minute, ampm.format(cal.getTime()));
        float timeXOffset = computeXOffset(TimeAmPmNoSec, timePaint, bounds);
        float timeYOffset = computeTimeYOffset(TimeAmPmNoSec, timePaint, bounds);
        canvas.drawText(TimeAmPmNoSec, timeXOffset, timeYOffset, timePaint);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        SimpleDateFormat day_text = new SimpleDateFormat("E");
        String DateNew = String.format("%s | %s %02d", day_text.format(cal.getTime()), month_date.format(cal.getTime()), time.monthDay);
        float dateXOffset = computeXOffset(DateNew, datePaint, bounds);
        float dateYOffset = computeDateYOffset(DateNew, datePaint);
        canvas.drawText(DateNew, dateXOffset, timeYOffset + dateYOffset, datePaint);
    }
    private float computeXOffset(String text, Paint paint, Rect watchBounds) {
        float centerX = watchBounds.exactCenterX();
        float timeLength = paint.measureText(text);
        return centerX - (timeLength / 2.0f);
    }
    private float computeTimeYOffset(String timeText, Paint timePaint, Rect watchBounds) {
        float centerY = watchBounds.exactCenterY();
        Rect textBounds = new Rect();
        timePaint.getTextBounds(timeText, 0, timeText.length(), textBounds);
        int textHeight = textBounds.height();
        return centerY + (textHeight / 2.0f) - 50f;
    }
    private float computeDateYOffset(String dateText, Paint datePaint) {
        Rect textBounds = new Rect();
        datePaint.getTextBounds(dateText, 0, dateText.length(), textBounds);
        return textBounds.height() + 25f;
    }
    public void setAntiAlias(boolean antiAlias) {
        timePaint.setAntiAlias(antiAlias);
        datePaint.setAntiAlias(antiAlias);
    }
    public void setColor(int color) {
        timePaint.setColor(color);
        datePaint.setColor(color);
    }
}

我还想知道是否有可能使用多种字体和颜色来样式文本

编辑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);
        }
    }

当手表进入环境模式屏幕变成黑白(或你所经历的黑色和灰色)

注意:在低位环境模式下,系统不能可靠地呈现图片中的颜色

来自Android网页

相关内容

  • 没有找到相关文章

最新更新