Activity切换动画

来源:互联网 发布:数控编程人员 编辑:程序博客网 时间:2024/06/13 01:00

你还在担心你的activity跳转的时候没有动画么,你还在到处翻内容为这些动画苦恼么,这篇文章,解决你所有的关于activity的切换动画效果。


1.左侧进入

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="600"        android:fromXDelta="-100.0%p"        android:toXDelta="0.0" /></set>

2.左侧退出

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="600"        android:fromXDelta="0.0"        android:toXDelta="-100.0%p" /></set>

3.右侧退出

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="600"        android:fromXDelta="0.0"        android:toXDelta="100.0%p" /></set>


4.右侧进入

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="600"        android:fromXDelta="100.0%p"        android:toXDelta="0.0" /></set>


5.放大渐入


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


以上就是常见的activity的切换动画,比如从一个activity跳转到另外一个activity的时候,希望当前activity从左边退出,右边的activity从右边进来。只需要使用这个方法。

overridePendingTransition(slide_left_in, slide_right_out);

这个方法有什么奇怪的地方呢?

打开源码:


/** * Call immediately after one of the flavors of {@link #startActivity(Intent)} * or {@link #finish} to specify an explicit transition animation to * perform next. * * <p>As of {@link android.os.Build.VERSION_CODES#JELLY_BEAN} an alternative * to using this with starting activities is to supply the desired animation * information through a {@link ActivityOptions} bundle to * {@link #startActivity(Intent, Bundle) or a related function.  This allows * you to specify a custom animation even when starting an activity from * outside the context of the current top activity. * * @param enterAnim A resource ID of the animation resource to use for * the incoming activity.  Use 0 for no animation. * @param exitAnim A resource ID of the animation resource to use for * the outgoing activity.  Use 0 for no animation. */

public void overridePendingTransition(int enterAnim, int exitAnim) {    try {        ActivityManagerNative.getDefault().overridePendingTransition(                mToken, getPackageName(), enterAnim, exitAnim);    } catch (RemoteException e) {    }}

我们发现这个方法就是activity中提供好的针对activity的切换动画的,能够自定义动画效果,如果传入0的话就没有动画效果。

特别注意的地方,参数1是要跳转的activity的动画,参数2是当前activity退出的动画,不要弄反了


使用方法:

/*跳转页面*/public void JumpActivityWithAnimator(Class<?> cls ){    Intent intent =new Intent(BaseActivity.this, cls);    startActivity(intent);    overridePendingTransition(getInAnimator(), getOutAnimator());}

把上面这个方法复制过去,直接调用就行了



---------------------------------------------------------------------------------------------------------------------------------------------------------


这上面说的是跳转,跳转一般是点击事件进行跳转,但是我们发现另外一个问题,退出的时候怎么进行动画呢?

不用急,看下面的方案。


一般的大部分都这样想做的:

@Overrideprotected void onDestroy() {    super.onDestroy();    /*动画效果*/    overridePendingTransition(animatio1,animatio2);}

经过验证发现不行。为什么?,因为在ondestory中界面已经销毁了,不会再进行动画效果。

真正的解决方案,监听返回按键:


//处理后退键的情况@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {    if(keyCode==KeyEvent.KEYCODE_BACK){        this.finish();  //finish当前activity        int animator1=getInAnimator();        int animator2=getOutAnimator();        overridePendingTransition(slide_left_in, slide_right_out);        return true;    }    return super.onKeyDown(keyCode, event);}
好啦,就酱紫了,有没有明白!

最后给出封装好的baseavtivity,主要是针对动画和跳转的,其他的没有封装,比如网络请求,加载布局这些,需要的自己家。


package com.eben.zhukeyunfu.Activity.textExample;import android.content.Intent;import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.view.KeyEvent;import static com.eben.zhukeyunfu.R.anim.slide_left_in;import static com.eben.zhukeyunfu.R.anim.slide_right_out;/* *  @项目名:  Zhukeyunfu  *  @包名:    com.eben.zhukeyunfu.Activity.textExample *  @文件名:   BaseActivity *  @创建者:   Administrator *  @创建时间:  2017/2/16 10:39 *  @描述:    TODO */public abstract class BaseActivity extends AppCompatActivity {    private static final String TAG = "BaseActivity";//设置标题栏颜色    public void changeColor() {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            //            View decorView = getWindow().getDecorView();            //            int  visibility = View.SYSTEM_UI_FLAG_FULLSCREEN;// 全屏设置            //            int visibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;// 表示允许应用的主题内容占用状态栏的空间            //            decorView.setSystemUiVisibility(visibility);            int color= setcolor();            getWindow().setStatusBarColor(getResources().getColor(color));// 通知栏颜色        }    }    //处理后退键的情况    @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        if(keyCode==KeyEvent.KEYCODE_BACK){            this.finish();  //finish当前activity            int animator1=getInAnimator();            int animator2=getOutAnimator();            overridePendingTransition(slide_left_in, slide_right_out);            return true;        }        return super.onKeyDown(keyCode, event);    }    /*跳转页面*/    public void JumpActivityWithAnimator(Class<?> cls ){        Intent intent =new Intent(BaseActivity.this, cls);        startActivity(intent);        overridePendingTransition(getInAnimator(), getOutAnimator());    }       /*抽象方法,主要是设置动画,标题栏颜色*/    protected abstract int getOutAnimator();    protected abstract int getInAnimator();    protected abstract int setcolor();}










0 0
原创粉丝点击