代码之家  ›  专栏  ›  技术社区  ›  Lars D

图像视图中的旋转图像

  •  0
  • Lars D  · 技术社区  · 14 年前

    我想用方向传感器和当前GPS位置显示一个箭头,指示朝向目标的方向。除了我想在我的图像视图中旋转箭头图像外,一切都工作正常。

    当前显示向上箭头的代码是:

    ImageViewArrow.setImageResource(R.drawable.arrow);
    

    显示旋转n度的箭头的最佳解决方案是什么?

    我试过这个,但它给了我混乱的图形:

    Matrix matrix = new Matrix(); 
    matrix.postRotate(Rotation);
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
      R.drawable.arrow); 
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
      bitmapOrg.getWidth(),bitmapOrg.getHeight(), matrix, true); 
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
    InfoArrow.setScaleType(ScaleType.CENTER);
    InfoArrow.setImageDrawable(bmd);
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   anon    14 年前
        2
  •  0
  •   Andras M.    14 年前

    CreateBitmap方法中有一个矩阵参数,可用于调整图像大小和旋转图像。您可以尝试如下操作:

        Matrix matrix = new Matrix(); 
        // to rotate the image bitmap use post Rotate
        matrix.postRotate(45); 
    
    
        Bitmap rotatedImage = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                          width, height, matrix, true); `