1

デバイスの向きが変わったときに回転させたい画像ボタンがあります。アニメーションやトランジションで画像を回転するにはどうすればよいですか?

4

1 に答える 1

3

このコード スニペットを試してください。

回転.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);
    }

}

}

うまくいけば、それはあなたを助けます。

于 2014-05-15T12:22:14.970 に答える