2012-04-23 51 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