2010-04-14 88 views
1

我想創建兩個紡車輪,如滑輪,所以每次連接的繩索移動時,兩個皮帶輪都會旋轉。我曾嘗試兩種方法:android紡紗圖像

1)的onDraw()View類的方法,它調用以下內使用Matrix.postRotate:

private void drawSpinningWheel(Canvas canvas) 
{ 
    try 
    { 
     canvas.save(); 

      Bitmap bitmapOrg = null; 

     int iDrawable = R.drawable.spinning_button; 

     bitmapOrg = BitmapFactory.decodeResource(getResources(), iDrawable); 

     if(bitmapOrg != null) 
     { 
      int width = bitmapOrg.getWidth(); 
      int height = bitmapOrg.getHeight(); 
      int newWidth = 24; 
      int newHeight = 24; 

      // calculate the scale - in this case = 0.4f 
      float scaleWidth = ((float) newWidth)/width; 
      float scaleHeight = ((float) newHeight)/height; 

      // createa matrix for the manipulation 
      Matrix matrix = new Matrix(); 
      // resize the bit map 
      matrix.postScale(scaleWidth, scaleHeight); 
      matrix.postRotate((float) mDegrees++); 

      Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
        width, height, matrix, true); 

      canvas.drawBitmap(resizedBitmap, matrix, null); 

     } 

     canvas.restore(); 
    } 
    catch(Exception e) 
    { 
     Log.e(TAG + "drawSpinningWheel", e.getMessage()); 
    } 

} 

但似乎圖像不僅旋轉,但旋轉圍繞另一軸線

2)使用SurfaceView和一個單獨的線程,在run()調用此:

private void doDraw(Canvas canvas) { 
     // Draw the background image. Operations on the Canvas accumulate 
     // so this is like clearing the screen. 
     canvas.drawBitmap(mBackgroundImage, 0, 0, null); 

     int yTop = mCanvasHeight - ((int) mY + mSpinningWheelImageHeight/2); 
     int xLeft = (int) mX - mSpinningWheelImageWidth/2; 

...

 // Draw the ship with its current rotation 
     canvas.save(); 
     canvas.rotate((float) mHeading++, (float) mX, mCanvasHeight 
       - (float) mY); 

      mSpinningWheelImage.setBounds(xLeft, yTop, xLeft + mSpinningWheelImageWidth, yTop 
        + mSpinningWheelImageHeight); 
      mSpinningWheelImage.draw(canvas); 


     canvas.restore(); 
    } 

我讓紡紗工作,但我不能添加另一個紡車。我甚至試圖爲第二個紡車創建另一個線程,只有一個出現。有人能指引我走向正確的方向嗎?謝謝。

3)好了,現在我也嘗試RotateAnimation:

公共類GraphicsView擴展查看 { 私有靜態最後絃樂QUOTE = 「沒有人使用Java了它的這個大權重股鎖鏈。」

private Animation anim; 
private Animation anim2; 

private Bitmap jobs; 
private Bitmap wheel; 

private int jobsXOffset;   
private int jobsYOffset; 

private int wheelXOffset;   
private int wheelYOffset; 

public GraphicsView(Context context) 
{ 
    super(context); 
    jobs = BitmapFactory 
    .decodeResource(getResources(), R.drawable.spinning_button); 

    jobsXOffset = jobs.getWidth()/2; 
    jobsYOffset = jobs.getHeight()/2; 

    wheel = BitmapFactory 
    .decodeResource(getResources(), R.drawable.wheel); 

    wheelXOffset = jobs.getWidth()/2; 
    wheelYOffset = jobs.getHeight()/2; 


} 

private void createAnim(Canvas canvas) 
{ 
    anim = new RotateAnimation(0, 360, canvas.getWidth()/2, canvas 
      .getHeight()/2); 
    anim.setRepeatMode(Animation.INFINITE); 
    anim.setRepeatCount(Animation.INFINITE); 
    anim.setDuration(10000L); 
    anim.setInterpolator(new AccelerateDecelerateInterpolator()); 

    startAnimation(anim); 
} 

private void createAnim2(Canvas canvas) 
{ 
    anim2 = new RotateAnimation(0, 360, canvas.getWidth()/2+30, canvas 
      .getHeight()/2); 
    anim2.setRepeatMode(Animation.INFINITE); 
    anim2.setRepeatCount(Animation.INFINITE); 
    anim2.setDuration(10000L); 
    anim2.setInterpolator(new AccelerateDecelerateInterpolator()); 

    startAnimation(anim); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    // creates the animation the first time 
    if (anim == null) { 
     createAnim(canvas); 
    } 

    int centerX = canvas.getWidth()/2; 
    int centerY = canvas.getHeight()/2; 

    canvas.drawBitmap(jobs, centerX - jobsXOffset, 
      centerY - jobsYOffset, null); 

    canvas.drawBitmap(wheel, centerX - wheelXOffset + 30, 
      centerY - wheelYOffset, null); 

} 

}

但結果卻是相似Canvas.Rotate,我能得到第一圖像旋轉正常,但第二圖象僅僅圍繞第一圖像周圍,但我注意到第二個圖像實際上也在旋轉。看起來像我將需要一個單獨的視圖爲第二個圖像,我希望避免這一點,因爲我不知道如何使輪子旋轉,但附加的繩子不旋轉。

回答

1

你試過RotateAnimation

+0

我做了,它只能繞左上角旋轉。我無法找到設置讓它圍繞我的drawable的中心旋轉(旋轉)。你能提供一些細節嗎? – mobibob 2010-06-29 02:09:36

0

看看我的問題,它的答案,我想這是你(和我)在找什麼? How to spin an android icon on its center point?

我放三個動畫,我的標誌的文字和我的標誌圖標。然後我對文字進行了反轉,並在圖標上顯示了正面。這很簡單,但很酷。我的問題的答案有代碼清單。