1

ボタンのクリックでImageViewを回転させる必要があります。最初のクリックで右に回転し、2番目のクリックで左に回転する必要があります。

問題は、「回転したばかり」の画像を2回回転させようとすると、回転が「最初の回転後」の点からではなく、元の点から開始することです。

前の回転で得られた画像を回転させる必要があります。以下に私はコードを過ぎました。

public class Rotate extends Activity {
    boolean mDirRight = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rotate);
        final ImageView imageArray = (ImageView) findViewById(R.id.ImageViewArray);
        imageArray.setImageResource(R.drawable.array01);
        imageArray.setAdjustViewBounds(true);
        final Button btnRotate = (Button) findViewById (R.id.ButtonRotate);
        btnRotate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doRotation();
            }
        });    
    }

    private void doRotation(){
        final int rotationRight = 30;
        final int rotationLeft = -20;
        final RotateAnimation rAnim;
        int degree;
        if (mDirRight) {
            degree = rotationRight;
            mDirRight = false;
        } else {
            degree = rotationLeft;
            mDirRight = true;
        }
        final ImageView image = (ImageView) findViewById(R.id.ImageViewArray);
        rAnim = new RotateAnimation(0f, degree, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        rAnim.setStartOffset(0);
        rAnim.setDuration(2000);
        rAnim.setFillAfter(true);
        rAnim.setFillEnabled(true);
        image.startAnimation(rAnim);
    }
}
4

1 に答える 1

2

この行で

rAnim = new RotateAnimation(0f、degree、RotateAnimation.RELATIVE_TO_SELF、0.5f、RotateAnimation.RELATIVE_TO_SELF、0.5f);

0fを目的の開始角度に変更します。

于 2011-09-05T12:51:17.370 に答える