Android Manager之SensorManager(传感器)—方向传感器(Orientation sensor)
效果图
2.核心代码
2.1.检测传感器
2.2.绘制文字
private void drawText() {
if (val <= 15 || val >= 345) {
text = "北";
} else if (val > 15 && val <= 75) {
text = "东北";
} else if (val > 75 && val <= 105) {
text = "东";
} else if (val > 105 && val <= 165) {
text = "东南";
} else if (val > 165 && val <= 195) {
text = "南";
} else if (val > 195 && val <= 255) {
text = "西南";
} else if (val > 255 && val <= 285) {
text = "西";
} else if (val > 285 && val < 345) {
text = "西北";
}
mTextPaint.getTextBounds(text, 0, text.length(), mTextRect);
//文字宽度
int mTextWidth = mTextRect.width();
//让文字水平居中显示
mCanvas.drawText(text, width / 2 - mTextWidth / 2, mTextHeight / 2, mTextPaint);
}
2.3.圆心数字
private void drawCenterText() {
String centerText = String.valueOf((int) val + "°");
mCenterPaint.getTextBounds(centerText, 0, centerText.length(), mCenterTextRect);
int centerTextWidth = mCenterTextRect.width();
int centerTextHeight = mCenterTextRect.height();
mCanvas.drawText(centerText, width / 2 - centerTextWidth / 2, mTextHeight + mOutSideRadius + centerTextHeight / 5, mCenterPaint);
}
2.4.camera相关设置
private void set3DMetrix() {
mCameraMatrix.reset();
mCamera.save();
mCamera.rotateX(mCameraRotateX);
mCamera.rotateY(mCameraRotateY);
mCamera.getMatrix(mCameraMatrix);
mCamera.restore();
//camera默认旋转是View左上角为旋转中心
//所以动作之前要,设置矩阵位置 -mTextHeight-mOutSideRadius
mCameraMatrix.preTranslate(-getWidth() / 2, -getHeight() / 2);
//动作之后恢复位置
mCameraMatrix.postTranslate(getWidth() / 2, getHeight() / 2);
mCanvas.concat(mCameraMatrix);
}
代码下载:https://download.csdn.net/download/weixin_37730482/10462764
参考:https://www.jianshu.com/p/cee9b7c3abe8?utm_source=oschina-app