activity页面切换动画
2016-03-13 09:44
281 查看
actiyity之间动画切换
界面切换,用到了ViewFlipper控件,可以切换图片,触摸切换
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper"
>
<!-- 第一页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一页"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开新Activity"
android:onClick="openActivity"
/>
</LinearLayout>
<!-- 第二页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#339900"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二页"
/>
</LinearLayout>
</ViewFlipper>
</LinearLayout
>
实现代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
out_lefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
startX = event.getX();
}else if(event.getAction()==MotionEvent.ACTION_UP){
float endX = event.getX();
if(endX > startX){
viewFlipper.setInAnimation(in_lefttoright);
viewFlipper.setOutAnimation(out_lefttoright);
viewFlipper.showNext();//显示下一页
}else if(endX < startX){
viewFlipper.setInAnimation(in_righttoleft);
viewFlipper.setOutAnimation(out_righttoleft);
viewFlipper.showPrevious();//显示前一页
}
return true;
}
return super.onTouchEvent(event);
}
相应的xml文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <alpha android:fromAlpha="0" android:toAlpha="1" android:duration="5000" /> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="5000" /> </set> public void openActivity(View v){ Intent intent = new Intent(this, OtherActivity.class); startActivity(intent); this.overridePendingTransition(R.anim.enteralpha, R.anim.outalpha);//实现Activity切换动画效果 }
界面切换,用到了ViewFlipper控件,可以切换图片,触摸切换
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper"
>
<!-- 第一页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第一页"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开新Activity"
android:onClick="openActivity"
/>
</LinearLayout>
<!-- 第二页 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#339900"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="第二页"
/>
</LinearLayout>
</ViewFlipper>
</LinearLayout
>
实现代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
in_lefttoright = AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
out_lefttoright = AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
in_righttoleft = AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
out_righttoleft = AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
startX = event.getX();
}else if(event.getAction()==MotionEvent.ACTION_UP){
float endX = event.getX();
if(endX > startX){
viewFlipper.setInAnimation(in_lefttoright);
viewFlipper.setOutAnimation(out_lefttoright);
viewFlipper.showNext();//显示下一页
}else if(endX < startX){
viewFlipper.setInAnimation(in_righttoleft);
viewFlipper.setOutAnimation(out_righttoleft);
viewFlipper.showPrevious();//显示前一页
}
return true;
}
return super.onTouchEvent(event);
}
相应的xml文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="5000" /> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="5000" /> </set>
相关文章推荐
- BestCoder Round #75 King's Game
- 学习进度第二周
- 使用GitHub pages + Hexo 快速搭建个性化博客(一)-部署Hexo默认blog模板至远程GitHub
- Windows 中 cmd 下 FTP 命令的简单使用
- 剑指offer代码分析——面试题13在O(1)内删除链表结点
- 剑指offer代码分析——面试题13在O(1)内删除链表结点
- 建立.bat文件,快速打开putty终端
- Java正则表达式
- for循环不加退出条件、equals的作用
- hdu 5641 King's Phone【暴力枚举】
- HDU 1503 Advanced Fruits
- volatile关键字
- form表单提交过程
- 百人团队敏捷转型日记 第一集 架构师没了、版本管理的蜕变
- 日经春秋 20160313
- Debian编译内核
- 按照Right-BICEP要求对实验二进行测试
- 【Android】解决Genymotion下载设备失败的方法(Connection Timeout)
- 枚举大小sizeof 中枚举的大小详解
- 【翻译自mos文章】已经安装的Oracle database组件和自带的用户信息