您的位置:首页 > 移动开发 > Android开发

Android_Activity开启动画的设置

2016-04-18 19:54 507 查看
Android开发过程中,很多时候我们需要Activity开启和关闭的时候有一定的动画效果,如何通过配置文件实现动画效果?

一般分为二步:

1、在res资源文件夹下面的anim(没有的话创建一个)创建.xml的动画文件

创建一个从上到下的动画文件top_to_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="100%p"
android:duration="200"
/>
</set>


创建一个从下到上的动画文件bottom_to_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate  
    android:fromYDelta="100%p"  
    android:toYDelta="0"  
<span style="white-space:pre">	</span>android:duration="200"  
  /> 
</set>


创建一个从左到右的动画文件top_to_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="400"
    android:fromXDelta="-100%p"
    android:toXDelta="0" />
</set>


创建一个从右到左的动画文件top_to_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="400"
    android:fromXDelta="0%p"
    android:toXDelta="-100%p"/>
</set>


2、代码中使用

设置启动动画
Intent intent = new Intent(this,DemoActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.activity_open,0);
设置关闭动画:
finish();
overridePendingTransition(R.anim.bottom_end,0);


PS:不需要状态出现时需要设置activity状态栏透明,在androidmanifest.xml里配置:

android:theme="@android:style/Theme.Translucent"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: