0

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

提前致谢。

4

2 回答 2

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", "");
}
}
于 2012-04-23T09:33:08.977 回答
0

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

于 2012-04-23T09:35:17.027 回答