2013-01-17 43 views
0

我已经构建了一个指南针应用程序,我要在一个固定位置将图像旋转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]); 
} 

是否存在应适用和完美的旋转图像的任何公式?能否请您解决这个...感谢

更新:

这里是屏幕的更多信息

enter image description here

更新方向

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; 

更新::

enter image description here enter image description here

+0

为什么你这样做'matrix.postTranslate(cxCompass,cyCompass);'? – Fildor

+1

我认为问题在于图像从左上角旋转。如果居中是问题,那么试试这个:通过(-image.width/2,-image.height/2)进行平移,然后从加速度计角度旋转,然后转换到屏幕中心。 –

+0

@NicolasBrown你正在研究矩阵。 postTranslate'? – user123

回答

1

这里是链接,它显示了如何旋转图像: http://android-er.blogspot.in/2010/07/rotate-bitmap-image-using-matrix.html

只是看看这个链接,并修改为按您的要求。这个链接只显示图片的旋转,它只是你问题的参考而不是正确答案。

+0

我之前已经看到它的旋转,但在这里在我的情况下它的cavas对象,并需要根据传感器更改 – user123

+0

更新我已更新我的问题。 – user123

+0

你可以检查更新的代码 – user123