简体   繁体   中英

Android Animation Background (Rotate, Translate)

I make my first small game. I use the Rotate Animation, that was given from Android. I write this code in every class file:

this.overridePendingTransition(R.anim.rotate, R.anim.translate);

is this the best way or can I set this up somewhere for my whole app? And also, if I use this animation, the new Activity "rotates" into the screen very nice but the background gets black. (I think because of the translate animation?!) How can I set up my own Background?

the code from the translate animation is:

<?xml version="1.0" encoding="utf-8"?>
<translate>

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="200%"
    android:toXDelta="0%"
    android:fromYDelta="200%"
    android:toYDelta="0%"
    android:duration="1000"
    android:zAdjustment="top" />

and the rotate animation:

<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:toDegrees="0"
    android:pivotX="-5%"
    android:pivotY="5%p"
    android:duration="1000" />

Abstraction

The current way you have it:

overridePendingTransition(R.anim.rotate, R.anim.translate);

The best way to set this is either via an abstract Activity or composition which is more to do with programming practice than Android specifically.

The short being:

public abstract BaseActivity extends FragmentActivity{
  @Override
  protected onCreate(Bundle savedInstance){
    overridePendingTransition(R.anim.rotate, R.anim.translate);
  }
}

Then your concrete Activity extends this.

public class MyActivity extends BaseActivity{}

Animation

So the current setup means that your New Activity will Rotate in , and your old activity (should) translate out .

Seen as your translating your old activity out (not in) the animation is a bit wrong. A correct translate animation for translating out would be something like:

<translate
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:fromXDelta="0%"
  android:toXDelta="100%"
  android:fromYDelta="0%"
  android:toYDelta="0%"
  android:duration="1000" />

Which will translate the exiting activity from centre screen to slide to the right.

Also ignore the zAdjustment , by default your new activity starts on top of the old one, you dont want to leave the old on top.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM