2012-04-02 75 views
10

我需要在我正在处理的应用上创建指南针。所以我试图创建一个名为CompassView的新视图,该视图基本上扩展了imageview,显示了一个位于东南西北的位图,它使用传感器来查找手机指向的度数,并相应地旋转图像以创建它一个真正的指南针。但问题是如果我试图旋转图像到一些像45度的角度,它会缩小。这里有一些图像可以更好地解释它。旋转图像而不缩水在Android上

This is normal

This is shrinked

正如你所看到的,第二图像收缩下来的时候我尽量绕45.我希望它做的是这样的: this

这里我正在使用的代码:

 Bitmap bMap = BitmapFactory.decodeResource(getResources(), 
       R.drawable.compass); 
     Matrix xMatrix = new Matrix(); 
     xMatrix.reset(); 
     xMatrix.postRotate(360-mValue, 75, 75); //This is 75 because 150 is the image width 
     Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, 
       bMap.getWidth(), bMap.getHeight(), xMatrix, true); 
     setImageBitmap(bMapRotate); 

任何帮助将升值ated。 THANKs

编辑:(解决方案) 我终于得到它的工作得益于接受的答案。下面是我使用的人谁想要知道它是如何工作的代码:

RotateAnimation rAnimAntiClockWise = new RotateAnimation(
        360 - mValue, 360 - event.values[0], 
        Animation.RELATIVE_TO_SELF, 0.5f, 
        Animation.RELATIVE_TO_SELF, 0.5f); 
//mValue is the angle in degrees and i subtracted it from 360 to make it anticlockwise, and event.values[0] is the same thing as mValue 
rAnimAntiClockWise.setFillAfter(true); 
rAnimAntiClockWise.setInterpolator(new LinearInterpolator()); 
rAnimAntiClockWise.setDuration(0); 
startAnimation(rAnimAntiClockWise); 

回答

4

您可以使用另一种技巧,它将像旋转一样工作,不会调整图像大小。我实际上是以45度角旋转图像,并在动画之后保持改变。

rAnimAntiClockWise = new RotateAnimation(0.0f, 45.0f, 
       Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 
       0.5f); 
     rAnimAntiClockWise.setFillAfter(true); 
     rAnimAntiClockWise.setInterpolator(new LinearInterpolator());  
      bitmap = BitmapFactory.decodeResource(getResources(), 
        R.drawable.rotate);  
     rAnimAntiClockWise.setDuration(100); 
     img_rotate.startAnimation(rAnimAntiClockWise); 
+0

非常感谢,我不知道,这可能是来达到的从旋转动画。我只需修改代码的某些部分即可运行 – KSubedi 2012-04-02 08:06:58

3

的问题是,你的新形象实际上是更大的,由于源的“伸出”的四角,因此视图正在缩小以适应。

几个可能的方法:

  1. 上述代码之后,调用Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height),复制正确尺寸的中心区域。容易给你的代码,但创建一个无用的中间位图。

  2. 不是将变换和源图像提供给createBitmap,只是创建一个正确大小的可变位图,将其包装在Canvas中,并指示Canvas渲染旋转的图像。

    bMapRotate = Bitmap.createBitmap(
        bMap.getWidth(), bMap.getHeight(), bMap.getConfig()); 
    Canvas canvasRotate = new Canvas(bMap); 
    canvasRotate.drawBitmap(bMap, xMatrix, paint); // any opaque Paint should do 
    
  3. 保留您拥有的代码,但在渲染时告诉视图裁剪而不是缩放。

+0

谢谢你让我知道如何..我知道我的选择,但我只是不知道如何实现它们,因为我是新来的帆布和自定义视图 – KSubedi 2012-04-02 08:06:14