本文主要是介绍LayoutAnimation和overridePendingTransition,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、LayoutAnimation
LayoutAnimationController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置动画效果,比如listview,recyclerview的item进入动画,可以在XML文件中设置,亦可以在Java代码中设置。
XML中使用
1、在res.anim.slide_right.xml定义一个动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"><translate
android:duration="2000"android:fromXDelta="-100%p"android:toXDelta="0" />
</set>
2、在res.anim.list_anim_layout.xml引用刚才的动画
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"android:animation="@anim/slide_right"android:animationOrder="reverse"android:delay="30%" /><!--Android:delay 子类动画时间间隔 (延迟) 70% 也可以是一个浮点数 如“1.2”等--><!--android:animationOrder="random" 子类的显示方式 random表示随机--><!--android:animationOrder 的取值有--><!--normal 0 默认--><!--reverse 1 倒序--><!--random 2 随机--><!--android:animation="@anim/slide_right" 表示孩子显示时的具体动画是什么-->
说明:其中delay的单位为秒;animation为设置动画的文件。animationOrder为进入方式
3、在viewgroup的XML文件中引用layoutAnimation
<ListView
android:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layoutAnimation="@anim/list_anim_layout" />
java代码中使用:
1. 同上;
2. 3. 在Acitivty中添加如下代码:
//通过加载XML动画设置文件来创建一个Animation对象;Animation animation=AnimationUtils.loadAnimation(this, R.anim.list_anim);//得到一个LayoutAnimationController对象;LayoutAnimationController lac=new LayoutAnimationController(animation);//设置控件显示的顺序;lac.setOrder(LayoutAnimationController.ORDER_REVERSE);//设置控件显示间隔时间;lac.setDelay(1);//为ListView设置LayoutAnimationController属性;datalist.setLayoutAnimation(lac);
二、Activity的切换效果
public void overridePendingTransition(int enterAnim, int exitAnim) {...}
Android Activity切换动画是指从Activity A 跳转至Activity B的时候,
Activity A 有退出动画exitAnim,
Activity B 有进入动画enterAnim。
overridePendingTransition 方法必须在startActivity()或者 finish()方法的后面。
如:
Intent intent = new Intent(MainActivity.this, MainActivityI.class);
startActivity(intent);
overridePendingTransition(R.anim.in, R.anim.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.//enterAnim:用于进入的activity的动画资源ID,设置为0:没有动画。* @param exitAnim A resource ID of the animation resource to use for* the outgoing activity. Use 0 for no animation.//用于出去的activity的动画资源ID,设置为0:没有动画。*/public void overridePendingTransition(int enterAnim, int exitAnim) {try {ActivityManagerNative.getDefault().overridePendingTransition(mToken, getPackageName(), enterAnim, exitAnim);} catch (RemoteException e) {}}
例子:
in.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate
android:duration="4000"android:fromXDelta="100%p"android:toXDelta="0%p"></translate>
</set>
out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"><translate
android:duration="4000"android:fromXDelta="0%p"android:toXDelta="-100%p"></translate>
</set>
java类:
public class MainActivity extends AppCompatActivity {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);tv = ((TextView) findViewById(R.id.tv));tv.setText("我是MainActivity === 1111111111111111");
// overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);tv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this, TwoActivity.class));
// overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);}});}
}
效果图:
public class MainActivity extends AppCompatActivity {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
// overridePendingTransition(R.anim.in, R.anim.out);overridePendingTransition(R.anim.in, 0);tv = ((TextView) findViewById(R.id.tv));tv.setText("我是MainActivity === 1111111111111111");
// overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);tv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this, TwoActivity.class));
// overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);}});}
}
效果图:
public class MainActivity extends AppCompatActivity {private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
// overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);tv = ((TextView) findViewById(R.id.tv));tv.setText("我是MainActivity === 1111111111111111");
// overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);tv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this, TwoActivity.class));overridePendingTransition(R.anim.in, R.anim.out);
// overridePendingTransition(R.anim.in, 0);}});}
}
效果图:
总结:
startActivity前设置overridePendingTransition:in动画是所在activity的进入动画,out是前一个activity的出去动画。
startActivit后设置overridePendingTransition:
in动画是下一个activity的进入动画,out是当前activity的出去动画。
这篇关于LayoutAnimation和overridePendingTransition的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!