我有一个图像按钮,我想在设备方向改变时旋转。如何通过一些动画或过渡来旋转图像?
答案 0 :(得分:3)
试试这段代码。
<强> rotate.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="2000" />
</set>
<强> rorate_anticlockwise.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="2000" />
</set>
用于检查手机的方向使用此代码
int orientation =this.getResources().getConfiguration().orientation;
MainActivity.java 的完整代码是
public class MainActivity extends Activity{
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
ImageButton image_btn;
Animation ranim_clockwise, ranim_anticlockwise;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image_btn= (ImageButton)findViewById(R.id.imageButton1);
ranim_clockwise = AnimationUtils.loadAnimation(this,R.anim.rotate);
ranim_anticlockwise = AnimationUtils.loadAnimation(this,R.anim.rotate_anticlock);
int orientation =this.getResources().getConfiguration().orientation;
if(orientation==1){ // portrait mode
image_btn.setAnimation(ranim_clockwise);
}
if(orientation==2){ //landscape mode
image_btn.setAnimation(ranim_anticlockwise);
}
}
}
希望它会对你有所帮助。