2015-07-02 79 views
0

這是我的代碼,它使用另一種傳感器類和其他活動,但
我只想羅盤平滑的方式旋轉,我使用位圖圖像 帆布位圖一路暢通
旋轉羅盤發佈的Android

不能旋轉
public class CompassView extends View 
    { 
    private int mWidth; 
    private int mHeight; 
    private float position = 0; 
    private Bitmap myCompassPointer; 
    public CompassView(Context context) 
    { 
    super(context); 
    myCompassPointer=BitmapFactory.decodeResource(getResources(), 
    R.drawable.pin_finder); 
    } 
    @Override 
    protected void onDraw(Canvas canvas) 
    { 
    int cx = (mWidth - myCompassPointer.getWidth()) >> 1; 
    int cy = (mHeight - myCompassPointer.getHeight()) >> 1; 
    if (position > 0) 
    { 
    canvas.rotate(position, mWidth >> 1, mHeight >> 1); 
    } 
    //it set the bitmap to cx,cy position 
    canvas.drawBitmap(myCompassPointer, cx, cy, null); 
    canvas.restore(); 
    } 
    public void updateData(float position) 
    { 
    this.position = position; 
    invalidate(); 
    } 
    protected void onMeasure(int widthMeasureSpec, int 
    heightMeasureSpec) 
    { 
    // TODO Auto-generated method stub 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    mWidth = View.MeasureSpec.getSize(widthMeasureSpec); 
    mHeight = View.MeasureSpec.getSize(heightMeasureSpec); 
    } 
    } 

回答

0

您應該將畫布的當前旋轉存儲在某個字段變量中,並在每次數據更新時使用ObjectAnimator將畫布當前旋轉值平滑更新爲新值。一定要使每個動畫幀的視圖無效。您可以在這裏閱讀更多關於動畫屬性和ObjectAnimator的信息:Property Animation