我已经构建了一个指南针应用程序,我要在一个固定位置将图像旋转360°。所以我拍了一张矩阵,用于在角度上旋转图像。就像这样:问题图像不能在静态360度位置旋转
Matrix matrix = new Matrix();
matrix.postRotate(direction);
Bitmap originalImage = BitmapFactory.decodeResource(getResources(),
R.drawable.resources_compass_arrow);
matrix.postTranslate(cxCompass, cyCompass);
canvas.drawBitmap(originalImage, matrix, paint);
,但问题是:当我运行它,它在不同的位置(它改变了位置),而我需要的是不准确给出罗盘方向旋转。
这里是更换传感器代码:
public void onSensorChanged(SensorEvent event)
{
myCompassView.updateDirection((float)event.values[0]);
}
是否存在应适用和完美的旋转图像的任何公式?能否请您解决这个...感谢
更新:
这里是屏幕的更多信息
更新方向
public void updateDirection(float dir) {
firstDraw = false;
direction = dir;
Log.e(VIEW_LOG_TAG, "direction"+direction);
invalidate();
}
计算罗盘X &ÿ :
int cxCompass = getMeasuredWidth()/2;
int cyCompass = getMeasuredHeight()/2;
更新::
为什么你这样做'matrix.postTranslate(cxCompass,cyCompass);'? – Fildor
我认为问题在于图像从左上角旋转。如果居中是问题,那么试试这个:通过(-image.width/2,-image.height/2)进行平移,然后从加速度计角度旋转,然后转换到屏幕中心。 –
@NicolasBrown你正在研究矩阵。 postTranslate'? – user123