如何在android中旋转按钮

时间:2012-02-24 10:03:50

标签: android android-orientation

嗨,在我的应用程序中,我试图围绕其中心旋转按钮,但它不是基于其中心旋转而是从它的位置移动。下面是我的代码,请帮助我如何解决该问题。

 public class example extends Activity {
float newAngle,oldAngle=0;


int flag=0;
int n=340;
 RotateAnimation animation,animation1;

    Button arrow;
    Button left,right;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     arrow=(Button)findViewById(R.id.img);
     left=(Button)findViewById(R.id.left);
     right=(Button)findViewById(R.id.right);
    final int width = arrow.getWidth();
    final int height = arrow.getHeight();      

     left.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(flag>=0)
            {
                   newAngle = oldAngle+ 30;

                    oldAngle=newAngle;
            }
            flag++;
            Log.i("flag",""+flag);
             animation = new RotateAnimation(oldAngle, newAngle);
             animation.setFillAfter(true);
             animation.setDuration(200);
             arrow.startAnimation(animation);
        }
    });
     }
       }

4 个答案:

答案 0 :(得分:7)

...更容易

http://developer.android.com/reference/android/view/animation/RotateAnimation.html#RotateAnimation(float,float,int,float,int,float)

new RotateAnimation(_oldAngle, _newAngle, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

答案 1 :(得分:1)

完整示例:

 public class MyActivity extends Activity implements View.OnClickListener {

    private ImageView _image;
    private float _newAngle, _oldAngle;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
        _image = (ImageView) findViewById(R.id.image);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.button) {
            _newAngle = _oldAngle + 30;

            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) _image.getLayoutParams();
            int centerX = layoutParams.leftMargin + (_image.getWidth()/2);
            int centerY = layoutParams.topMargin + (_image.getHeight()/2);
            RotateAnimation animation = new RotateAnimation(_oldAngle, _newAngle, centerX, centerY);
            animation.setDuration(0);
            animation.setRepeatCount(0);
            animation.setFillAfter(true);
            _image.startAnimation(animation);

            _oldAngle = _newAngle;
        }
    }
}

答案 2 :(得分:0)

使用

RotateAnimation(float fromDegrees,float toDegrees,float pivotX,float pivotY)

pivotX,pivotY - 按钮中心的坐标。看起来你的角度是一样的

答案 3 :(得分:0)

最简单的解决方案值得一提:

button.setRotation( angle );

将其放入onClick方法中。例如:

public void onClick( View v ) { v.setRotation( v.getRotation() + 30f ); }

请参阅View API