2012-09-13 39 views
0

我想旋轉一個ImageView,以特定的角度和特殊的程度。我在谷歌搜索並找到了一些解決方案,但我沒有找到完整的答案(如this answer)。在API 10中旋轉Android ImageView(不使用動畫)

我試過用這個代碼,但是ImageView沒有旋轉!只是它的背景旋轉(不旋轉視圖矩形)

public class ImageViewCustom extends ImageView { 
    public Context M_context; 

    public ImageViewCustom(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     this.M_context = context; 
    } 

    public ImageViewCustom(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.M_context = context; 
    } 

    public ImageViewCustom(Context context) { 
     super(context); 
     this.M_context = context; 
    } 

    public float xPivot; 
    public float yPivot; 
    public float degree; 

    public void draw(Canvas canvas) { 
     canvas.save(); 
     canvas.rotate(degree, xPivot, yPivot); 
     super.draw(canvas); 
     canvas.restore(); 
    } 
} 

所以,我怎麼能旋轉的ImageView不使用Animation,只是用覆蓋或添加旋轉的方法來ImageViewCustom

感謝提前:)

回答

-1
Bitmap bmp = arrow.copy(Bitmap.Config.ARGB_8888, true); 
Matrix matrix = new Matrix(); 
matrix.postRotate(direction); 
Bitmap bmpRotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),bmp.getHeight(), matrix, true); 
iv.setImageBitmap(bmpRotated); 

IV是ImageView的和方向在whicht位圖需要被旋轉的程度。

+0

它只是旋轉ImageView的背景,而不是ImageView。 –

+0

是真實的,但根據您的需求,它可能是高效的,只是試圖幫助 – ePeace

+0

非常感謝:) –