Java 浮动位图,编码冲突?
您好,我开始学习Java,我认为可以帮助我开始学习的第一个项目之一是为WearOS设计一个手表表面,我是一名艺术家/插画家(目前正在回学校学习计算机编程),出生和上学,所以我目前正在尝试一些我有想法的工作 我的问题是。我设计并实现了为手表指针旋转的位图,并尝试使用“mCenterX和Y”将其居中,甚至手动尝试使用canvas.translate将其居中,并输入坐标。抱歉,如果这是一个绝对的新手问题,但我已经在谷歌上搜索并尝试了至少一周。(将包括漂浮的手的图像,但不能嵌入。) “时针”是我目前唯一使用的,我只能通过canvas.translate手动输入坐标。另一只手每隔一段时间就会进入画面,然后浮出画面,围绕某个“未知”的中心点旋转 代码如下:(我已经包括了所有与位图相关的小时/分钟/秒代码,以查看是否存在冲突。我正在使用来自不同项目的代码,这可能解释了偏离中心的原因,但我还不知道为什么,这是我在这里的最终目标。) 感谢您的关注和回复!如果你需要更多的信息,请告诉我Java 浮动位图,编码冲突?,java,wear-os,centering,watch,Java,Wear Os,Centering,Watch,您好,我开始学习Java,我认为可以帮助我开始学习的第一个项目之一是为WearOS设计一个手表表面,我是一名艺术家/插画家(目前正在回学校学习计算机编程),出生和上学,所以我目前正在尝试一些我有想法的工作 我的问题是。我设计并实现了为手表指针旋转的位图,并尝试使用“mCenterX和Y”将其居中,甚至手动尝试使用canvas.translate将其居中,并输入坐标。抱歉,如果这是一个绝对的新手问题,但我已经在谷歌上搜索并尝试了至少一周。(将包括漂浮的手的图像,但不能嵌入。) “时针”是我目前唯一
private class Engine extends CanvasWatchFaceService.Engine {
private static final float HOUR_STROKE_WIDTH = 5f;
private static final float MINUTE_STROKE_WIDTH = 3f;
private static final float SECOND_TICK_STROKE_WIDTH = 2f;
private static final float CENTER_GAP_AND_CIRCLE_RADIUS = 4f;
private static final int SHADOW_RADIUS = 6;
/* Handler to update the time once a second in interactive mode. */
private final Handler mUpdateTimeHandler = new EngineHandler(this);
private Calendar mCalendar;
private final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mCalendar.setTimeZone(TimeZone.getDefault());
invalidate();
}
};
private boolean mRegisteredTimeZoneReceiver = false;
private boolean mMuteMode;
private float mCenterX;
private float mCenterY;
private int mWatchHandColor;
private int mWatchHandHighlightColor;
private int mWatchHandShadowColor;
private Paint mHourPaint;
private Paint mMinutePaint;
private Paint mSecondPaint;
private Paint mTickAndCirclePaint;
private Paint mBackgroundPaint;
private Bitmap mHourBitmap;
private Bitmap mMinuteBitmap;
private Bitmap mSecondBitmap;
private Bitmap mBackgroundBitmap;
private Bitmap mGrayBackgroundBitmap;
private void initializeWatchFace() {
/* Set defaults for colors */
mWatchHandColor = Color.WHITE;
mWatchHandHighlightColor = Color.RED;
mWatchHandShadowColor = Color.BLACK;
mHourPaint = new Paint();
mHourPaint.setColor(mWatchHandColor);
mHourPaint.setStrokeWidth(HOUR_STROKE_WIDTH);
mHourPaint.setAntiAlias(true);
mHourPaint.setStrokeCap(Paint.Cap.ROUND);
mHourPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mMinutePaint = new Paint();
mMinutePaint.setColor(mWatchHandColor);
mMinutePaint.setStrokeWidth(MINUTE_STROKE_WIDTH);
mMinutePaint.setAntiAlias(true);
mMinutePaint.setStrokeCap(Paint.Cap.ROUND);
mMinutePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mSecondPaint = new Paint();
mSecondPaint.setColor(mWatchHandHighlightColor);
mSecondPaint.setStrokeWidth(SECOND_TICK_STROKE_WIDTH);
mSecondPaint.setAntiAlias(true);
mSecondPaint.setStrokeCap(Paint.Cap.ROUND);
mSecondPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mTickAndCirclePaint = new Paint();
mTickAndCirclePaint.setColor(mWatchHandColor);
mTickAndCirclePaint.setStrokeWidth(SECOND_TICK_STROKE_WIDTH);
mTickAndCirclePaint.setAntiAlias(true);
mTickAndCirclePaint.setStyle(Paint.Style.STROKE);
mTickAndCirclePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
}
private void updateWatchHandStyle() {
if (mAmbient) {
mHourPaint.setColor(Color.WHITE);
mMinutePaint.setColor(Color.WHITE);
mSecondPaint.setColor(Color.WHITE);
mTickAndCirclePaint.setColor(Color.WHITE);
mHourPaint.setAntiAlias(false);
mMinutePaint.setAntiAlias(false);
mSecondPaint.setAntiAlias(false);
mTickAndCirclePaint.setAntiAlias(false);
mHourPaint.clearShadowLayer();
mMinutePaint.clearShadowLayer();
mSecondPaint.clearShadowLayer();
mTickAndCirclePaint.clearShadowLayer();
} else {
mHourPaint.setColor(mWatchHandColor);
mMinutePaint.setColor(mWatchHandColor);
mSecondPaint.setColor(mWatchHandHighlightColor);
mTickAndCirclePaint.setColor(mWatchHandColor);
mHourPaint.setAntiAlias(true);
mMinutePaint.setAntiAlias(true);
mSecondPaint.setAntiAlias(true);
mTickAndCirclePaint.setAntiAlias(true);
mHourPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mMinutePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mSecondPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mTickAndCirclePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
}
@Override
public void onInterruptionFilterChanged(int interruptionFilter) {
super.onInterruptionFilterChanged(interruptionFilter);
boolean inMuteMode = (interruptionFilter == WatchFaceService.INTERRUPTION_FILTER_NONE);
/* Dim display in mute mode. */
if (mMuteMode != inMuteMode) {
mMuteMode = inMuteMode;
mHourPaint.setAlpha(inMuteMode ? 100 : 255);
mMinutePaint.setAlpha(inMuteMode ? 100 : 255);
mSecondPaint.setAlpha(inMuteMode ? 80 : 255);
invalidate();
}
}
@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);
/*
* Find the coordinates of the center point on the screen, and ignore the window
* insets, so that, on round watches with a "chin", the watch face is centered on the
* entire screen, not just the usable portion.
*/
mCenterX = width / 2f;
mCenterY = height / 2f;
/*
* Calculate lengths of different hands based on watch screen size.
*/
mSecondHandLength = (float) (mCenterX * 0.875);
sMinuteHandLength = (float) (mCenterX * 0.75);
sHourHandLength = (float) (mCenterX * 0.5);
/* Scale loaded background image (more efficient) if surface dimensions change. */
float scale = ((float) width) / (float) mBackgroundBitmap.getWidth();
mBackgroundBitmap = Bitmap.createScaledBitmap(mBackgroundBitmap,
(int) (mBackgroundBitmap.getWidth() * scale),
(int) (mBackgroundBitmap.getHeight() * scale), false);
float scale2 = ((float) width) / (float) mSecondBitmap.getWidth();
mSecondBitmap = Bitmap.createScaledBitmap(mSecondBitmap,
12,
222, false);
float scale3 = ((float) width) / (float) mHourBitmap.getWidth();
mHourBitmap = Bitmap.createScaledBitmap(mHourBitmap,
12,139,false);
float scale4 = ((float) width) / (float) mMinuteBitmap.getWidth();
mMinuteBitmap = Bitmap.createScaledBitmap(mMinuteBitmap,
12,
162, false);
private void drawWatchFace(Canvas canvas) {
/*
* Draw ticks. Usually you will want to bake this directly into the photo, but in
* cases where you want to allow users to select their own photos, this dynamically
* creates them on top of the photo.
*/
float innerTickRadius = mCenterX - 10;
float outerTickRadius = mCenterX;
for (int tickIndex = 0; tickIndex < 12; tickIndex++) {
float tickRot = (float) (tickIndex * Math.PI * 2 / 12);
float innerX = (float) Math.sin(tickRot) * innerTickRadius;
float innerY = (float) -Math.cos(tickRot) * innerTickRadius;
float outerX = (float) Math.sin(tickRot) * outerTickRadius;
float outerY = (float) -Math.cos(tickRot) * outerTickRadius;
canvas.drawLine(mCenterX + innerX, mCenterY + innerY,
mCenterX + outerX, mCenterY + outerY, mTickAndCirclePaint);
}
/*
* These calculations reflect the rotation in degrees per unit of time, e.g.,
* 360 / 60 = 6 and 360 / 12 = 30.
*/
final float seconds =
(mCalendar.get(Calendar.SECOND) + mCalendar.get(Calendar.MILLISECOND) / 1000f);
final float secondsRotation = seconds * 6f;
final float minutesRotation = mCalendar.get(Calendar.MINUTE) * 6f;
final float hourHandOffset = mCalendar.get(Calendar.MINUTE) / 2f;
final float hoursRotation = (mCalendar.get(Calendar.HOUR) * 30) + hourHandOffset;
/*
* Save the canvas state before we can begin to rotate it.
*/
canvas.save();
canvas.rotate(hoursRotation, mCenterX, mCenterY);
Matrix matrixHour = new Matrix();
matrixHour.setRotate(0, mCenterX, mCenterY);
canvas.translate(175, 68);
canvas.drawBitmap(mHourBitmap, matrixHour, mHourPaint);
canvas.rotate(minutesRotation - hoursRotation, mCenterX, mCenterY);
Matrix matrixMinute = new Matrix();
matrixMinute.setRotate(0, mCenterX, mCenterY);
canvas.translate(175, -60);
canvas.drawBitmap(mMinuteBitmap, matrixMinute, mMinutePaint);
/*
* Ensure the "seconds" hand is drawn only when we are in interactive mode.
* Otherwise, we only update the watch face once a minute.
*/
if (!mAmbient) {
canvas.rotate(secondsRotation - minutesRotation, mCenterX, mCenterY);
Matrix matrix = new Matrix();
matrixHour.setRotate(0, mCenterX, mCenterY);
canvas.translate(-175,35);
canvas.drawBitmap(mSecondBitmap, matrix, mSecondPaint);
}
/* Restore the canvas' original orientation. */
canvas.restore();
}
私有类引擎扩展了CanvasWatchFaceService.Engine{
专用静态最终浮动小时\行程\宽度=5f;
专用静态最终浮动分钟\行程\宽度=3f;
专用静态最终浮动秒刻度笔划宽度=2f;
专用静态最终浮动中心\间隙\和\圆\半径=4f;
私有静态最终整数阴影半径=6;
/*处理程序在交互模式下每秒更新一次时间*/
私有最终处理程序mUpdateTimeHandler=新引擎处理程序(此);
私人日历;
private final BroadcastReceiver mTimeZoneReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
setTimeZone(TimeZone.getDefault());
使无效();
}
};
私有布尔值mrRegisteredTimeZoneReceiver=false;
私有布尔mMuteMode;
私人浮动mCenterX;
私人浮动麦肯锡;
私人内特mWatchHandColor;
私密的国际mWatchHandHighlightColor;
私人内特mWatchHandShadowColor;
私人涂料;
私人油漆;
私人涂料;
私人油漆、金属漆和电路漆;
私人油漆;背景漆;
私有位图;
私有位图mmitmap;
私有位图;
私有位图MBackground位图;
私有位图mGrayBackgroundBitmap;
私有void初始化watchface(){
/*设置颜色的默认值*/
mWatchHandColor=Color.WHITE;
mWatchHandHighlightColor=Color.RED;
mWatchHandShadowColor=Color.BLACK;
mHourPaint=新油漆();
mHourPaint.setColor(mWatchHandColor);
mHourPaint.设置行程宽度(小时行程宽度);
mHourPaint.setAntiAlias(真);
mHourPaint.setStrokeCap(油漆盖圆形);
mHourPaint.setShadowLayer(阴影半径,0,0,mWatchHandShadowColor);
mMinutePaint=新油漆();
mMinutePaint.setColor(mWatchHandColor);
M螺母油漆设置行程宽度(分钟行程宽度);
mMinutePaint.setAntiAlias(真);
M螺母油漆固定行程盖(油漆盖圆形);
mMinutePaint.setShadowLayer(阴影半径,0,0,mWatchHandShadowColor);
mSecondPaint=新油漆();
mSecondPaint.setColor(mWatchHandHighlightColor);
mSecondPaint.setStrokeWidth(第二个刻度线宽度);
mSecondPaint.setAntiAlias(真);
msecondant.setStrokeCap(油漆盖圆形);
mSecondPaint.setShadowLayer(阴影半径,0,0,mWatchHandShadowColor);
mTickAndCirclePaint=新油漆();
mTickAndCirclePaint.setColor(mWatchHandColor);
MTICK循环绘制设置行程宽度(第二次勾选行程宽度);
mTickAndCirclePaint.setAntiAlias(真);
mTickAndCirclePaint.setStyle(油漆、样式、笔划);
mTickAndCirclePaint.setShadowLayer(阴影半径,0,0,mWatchHandShadowColor);
}
私有void updateWatchHandStyle(){
如果(妈妈){
mHourPaint.setColor(颜色:白色);
mMinutePaint.setColor(颜色:白色);
mSecondPaint.setColor(颜色为白色);
mTickAndCirclePaint.setColor(Color.WHITE);
mHourPaint.setAntiAlias(假);
mMinutePaint.setAntiAlias(假);
mSecondPaint.setAntiAlias(false);
mTickAndCirclePaint.setAntiAlias(假);
mHourPaint.ClearHadoLayer();
mMinutePaint.ClearHadowLayer();
mSecondPaint.ClearHadowLayer();
mTickAndCirclePaint.ClearHadowLayer();
}否则{
mHourPaint.setColor(mWatchHandColor);
mMinutePaint.setColor(mWatchHandColor);
mSecondPaint.setColor(mWatchHandHighlightColor);
mTickAndCirclePaint.setColor(mWatchHandColor);
mHourPaint.setAntiAlias(真);
mMinutePaint.setAntiAlias(真);
mSecondPaint.setAntiAlias(真);
mTickAndCirclePaint.setAntiAlias(真);
mHourPaint.setShadowLayer(阴影半径,0,0,mWatchHandShadowColor);
mMinutePaint.setShadowLayer(阴影半径,0,0,mWatchHandShadowColor);