How can I turn on animation programatically?

2019-06-13 18:13发布

问题:

How can I turn on animation programatically? I am using overridePendingTransition();. It doesn't work unless I turn on animation myself. Can anybody tell me how to turn on animations programatically which are inside display/Animation setting? I am using overridePendingTransition(); to open new activity .

here are my xml

grow_fadein_center

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

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="0.6" android:toXScale="1.0"
           android:fromYScale="0.6" android:toYScale="1.0"
           android:pivotX="50%" android:pivotY="50%"
           android:duration="@android:integer/config_longAnimTime" />
    <alpha android:interpolator="@anim/decelerate_interpolator"
            android:fromAlpha="0.0" android:toAlpha="1.0"
            android:duration="@android:integer/config_longAnimTime" />
</set>

shrink_fadeout_center :

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="1.0" android:toXScale="0.5"
           android:fromYScale="1.0" android:toYScale="0.5"
           android:pivotX="50%" android:pivotY="50%"
           android:duration="@android:integer/config_longAnimTime" />
    <alpha android:interpolator="@anim/accelerate_interpolator"
            android:fromAlpha="1.0" android:toAlpha="0.0"
            android:duration="@android:integer/config_longAnimTime"/>
</set>

and here is the code i am using to open intent

btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ContentResolver cr = getContentResolver();
                Settings.System.putInt(cr, Settings.System.WINDOW_ANIMATION_SCALE, 0);

                Intent intent=new Intent(context,Second.class);


                startActivity(intent);
                overridePendingTransition(R.anim.grow_fade_in_center, R.anim.shrink_fade_out_center);
                finish();

//testing on textview worked without turning system animation on.
                Animation animation=AnimationUtils.loadAnimation(context, R.anim.grow_fade_in_center);
                txt.startAnimation(animation);




            }
        });

This works only when animations are turned on. and i am doing so by settings->display->animation

I found

ContentResolver cr = getContentResolver();
Settings.System.putInt(cr, Settings.System.WINDOW_ANIMATION_SCALE, 0);

to turn on or off animation by setting integer! but it didn't work for me! what i need is how can i turned on/off animation programatically which are in system setting?

回答1:

You do not need to initiate the Animation pragmatically like that.

You just need to call overridePendingTransition(R.anim.grow_fade_in_center, R.anim.shrink_fade_out_center); in the correct place.

public void onClick(View v) {
    startActivity(HomeActivity.class);
    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    finish();
}

Also doing:

Settings.System.putInt(cr, Settings.System.WINDOW_ANIMATION_SCALE, 0);

Will disabled window animations!