如何通过onTouchListener旋转图像?

时间:2012-04-23 09:25:05

标签: android android-layout

我必须实现通过onTouchListener()旋转图像的功能。请提供旋转图像的示例代码。请帮帮我。

先谢谢。

2 个答案:

答案 0 :(得分: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", "");
}
}

答案 1 :(得分:0)

您需要在“补间动画”类型中使用xml。 从java代码中你可以根据这个xml进行视图操作。 这是一个帮助的链接: http://www.edumobile.org/android/android-beginner-tutorials/tween-animation/