Created
March 6, 2019 08:48
-
-
Save lizhangqu/fe1f2864aa02993f7ecfefe8c3aac3e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.res.TypedArray; | |
import android.content.res.Resources; | |
import android.util.Log; | |
/** | |
* @author lizhangqu | |
* @version V1.0 | |
* @since 2019-03-06 15:33 | |
*/ | |
public class ActivityAnimationHelper { | |
/** | |
* 修复入场动画失效 | |
* <pre> | |
* | |
* @Override | |
* protected void onCreate(Bundle savedInstanceState) { | |
* super.onCreate(savedInstanceState); | |
* ActivityAnimationHelper.onCreate(this); | |
* } | |
* | |
* </pre> | |
*/ | |
public static void onCreate(Activity activity) { | |
try { | |
long start = System.currentTimeMillis(); | |
if (activity == null) { | |
return; | |
} | |
Resources.Theme theme = activity.getTheme(); | |
if (theme == null) { | |
return; | |
} | |
TypedArray activityStyle = theme.obtainStyledAttributes(new int[]{android.R.attr.windowAnimationStyle}); | |
if (activityStyle == null) { | |
return; | |
} | |
int windowAnimationStyleResId = activityStyle.getResourceId(0, 0); | |
activityStyle.recycle(); | |
activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, new int[]{android.R.attr.activityOpenEnterAnimation, android.R.attr.activityOpenExitAnimation}); | |
if (activityStyle == null) { | |
return; | |
} | |
int activityOpenEnterAnimation = activityStyle.getResourceId(0, 0); | |
int activityOpenExitAnimation = activityStyle.getResourceId(1, 0); | |
activityStyle.recycle(); | |
if (activityOpenEnterAnimation != 0 || activityOpenExitAnimation != 0) { | |
activity.overridePendingTransition(activityOpenEnterAnimation, activityOpenExitAnimation); | |
} | |
long end = System.currentTimeMillis(); | |
Log.e("TAG", "onCreate time:" + (end - start)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* 修复退场动画失效 | |
* <pre> | |
* | |
* @Override | |
* public void finish() { | |
* super.finish(); | |
* ActivityAnimationHelper.finish(this); | |
* } | |
* | |
* </pre> | |
* } | |
*/ | |
public static void finish(Activity activity) { | |
try { | |
long start = System.currentTimeMillis(); | |
if (activity == null) { | |
return; | |
} | |
Resources.Theme theme = activity.getTheme(); | |
if (theme == null) { | |
return; | |
} | |
TypedArray activityStyle = theme.obtainStyledAttributes(new int[]{android.R.attr.windowAnimationStyle}); | |
if (activityStyle == null) { | |
return; | |
} | |
int windowAnimationStyleResId = activityStyle.getResourceId(0, 0); | |
activityStyle.recycle(); | |
activityStyle = theme.obtainStyledAttributes(windowAnimationStyleResId, new int[]{android.R.attr.activityCloseEnterAnimation, android.R.attr.activityCloseExitAnimation}); | |
if (activityStyle == null) { | |
return; | |
} | |
int activityCloseEnterAnimation = activityStyle.getResourceId(0, 0); | |
int activityCloseExitAnimation = activityStyle.getResourceId(1, 0); | |
activityStyle.recycle(); | |
if (activityCloseEnterAnimation != 0 || activityCloseExitAnimation != 0) { | |
activity.overridePendingTransition(activityCloseEnterAnimation, activityCloseExitAnimation); | |
} | |
long end = System.currentTimeMillis(); | |
Log.e("TAG", "end time:" + (end - start)); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment