我正在尝试开发手表面孔,我能够添加时间、日期和其他信息并定位文本。我在风格上有问题,主要是颜色。我有一个红色文本,另一个白色文本。转到环境模式并返回到交互模式后,颜色为白色或灰色,具体取决于下面的代码。我只能让它恢复到一种颜色。
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);
}
}
我还想知道是否可以使用多种字体和颜色来设置文本的样式。