2012-04-23 45 views

回答

0

以下是旋轉圖像的示例代碼。

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
requestWindowFeature(Window.FEATURE_NO_TITLE); 
setContentView(R.layout.main); 

baseView = (View) findViewById(R.id.baseView); 
turntable = (ImageView) findViewById(R.id.turntable); 

turntable.setOnTouchListener(onTableTouched); 
baseView.setOnTouchListener(onTableTouched); 

}

public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() { 
public boolean onTouch(View v, MotionEvent evt) { 
    double r = Math.atan2(evt.getX() - turntable.getWidth()/2, 
      (turntable.getHeight()/2) - evt.getY()); 
    int rotation = (int) Math.toDegrees(r); 
    Log.i("R is ", ""+r); 
    if (evt.getAction() == MotionEvent.ACTION_DOWN) { 
    } 

    if (evt.getAction() == MotionEvent.ACTION_MOVE) { 
     x= evt.getX(); 
     y= evt.getY(); 
     updateRotation(rotation); 
    } 

    if (evt.getAction() == MotionEvent.ACTION_UP) { 
      // 
    } 
    return true; 
} 
}; 
private void updateRotation(double rot) { 
float newRot = new Float(rot); 
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
R.drawable.orsl_circle_transparent); 
Matrix matrix = new Matrix(); 
//  matrix.setTranslate(getWindowManager().getDefaultDisplay().getWidth()/2,   getWindowManager().getDefaultDisplay().getHeight()); 
    matrix.postRotate(newRot,bitmap.getWidth()/2,bitmap.getHeight()/2); 
//  matrix.setSinCos(newRot, newRot/2, 100, 100); 
//  matrix.postRotate(newRot); 
    Log.i("THE ROATTION ", " "+ newRot); 

if(y>250) 
{ 
    Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap 
      .getWidth(), bitmap.getHeight(), matrix, true); 
    turntable.setImageBitmap(redrawnBitmap); 
}else 
{ 
    Bitmap redrawnBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap 
      .getWidth(), bitmap.getHeight(), matrix, true); 
    turntable.setImageBitmap(redrawnBitmap); 
    Log.i("GUITAR _IMAGE", ""); 
} 
} 
+0

在這段代碼中,圖像旋轉但也改變其位置。 – Rajesh 2012-04-23 09:48:04

0

你需要鍵入 「補間動畫」 的使用XML。比起java代碼,你可以根據這個xml做出一個視圖行爲。 這裏是一個鏈接,幫助: http://www.edumobile.org/android/android-beginner-tutorials/tween-animation/

+0

Hi Roiberg,我想基於onTouch()方向旋轉此圖像。我必須使用這個旋轉圖像音量控制器。所以,必須通過onTouch監聽器來實現這個循環。如果我們在xml文件中使用,onTouch控件將不會發生。感謝您的回覆。 – Rajesh 2012-04-23 10:00:41