Modify the transition animation of two Activity in Android

There will be a default animation effect when the two activities in Android are switched. It is very simple to modify this default effect. You only need to call the overridePendingTransition() method after StartActivity().
This method requires two parameters, both of which are animation effects defined by xml. The first parameter refers to the effect of the second activity entering, and the second parameter is the effect when it exits. First, create a new folder named anim under the res folder, and select the resource type as anim, and then create an xml file under the folder. The file of the anim resource type has four basic animation effects, alpha, rotate, scale and translate.
First create the xml code and define the animation effect in it, this is used as the first parameter as the entry animation of the second activity

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="@android:integer/config_longAnimTime"/>

Then define an animation effect

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromXDelta="0.0"
    android:toXDelta="0.0"
    android:duration="@android:integer/config_longAnimTime">
</translate>

Then now that the animation has been defined, you can modify the jump animation of the activity in the java code

public class ChangePendingTransitonActivity extends AppCompatActivity{

    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pending_transiton);
        button = (Button) findViewById(R.id.fadein);
               button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ChangePendingTransitonActivity.this, MainActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.fade, R.anim.hold);
            }
        });
    }
}

The basic use is as simple as that. You only need to call the
overridePendingTransition() method after starting the activity to change the transition animation of the activity. Then there is another tag in the xml code which is the <set> tag. In this tag, the animation can be combined Get up and hit for better looking, more complex animations

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325640543&siteId=291194637
Recommended