前面介绍了利用Android自带的控件,进行滑动翻页制作效果,现在我们通过代码实现一些滑动翻页的动画效果。
Animation实现动画有两个方式:帧动画(frame-by-frame animation)和补间动画(tweened animation)
本示例通过继承Animation自定义Rotate3D,实现3D翻页效果。效果图如下:
1、Rotate3D(Animation)
首先,自定义Animation的3D动画类Rotate3D
public class Rotate3D extends Animation {
private float fromDegree; // 旋转起始角度
private float toDegree; // 旋转终止角度
private float mCenterX; // 旋转中心x
private float mCenterY; // 旋转中心y
private Camera mCamera;
public Rotate3D(float fromDegree, float toDegree, float centerX, float centerY) {
this.fromDegree = fromDegree;
this.toDegree = toDegree;
this.mCenterX = centerX;
this.mCenterY = centerY;
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float FromDegree = fromDegree;
float degrees = FromDegree + (toDegree - fromDegree) * interpolatedTime; // 旋转角度(angle)
final float centerX = mCenterX;
final float centerY = mCenterY;
final Matrix matrix = t.getMatrix();
if (degrees <= -76.0f) {
degrees = -90.0f;
mCamera.save();
mCamera.rotateY(degrees); // 旋转
mCamera.getMatrix(matrix);
mCamera.restore();
} else if (degrees >= 76.0f) {
degrees = 90.0f;
mCamera.save();
mCamera.rotateY(degrees);
mCamera.getMatrix(matrix);
mCamera.restore();
} else {
mCamera.save();
mCamera.translate(0, 0, centerX); // 位移x
mCamera.rotateY(degrees);
mCamera.translate(0, 0, -centerX);
mCamera.getMatrix(matrix);
mCamera.restore();
}
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
然后,实例化Rotate3D的旋转方向
public void initAnimation() {
// 获取旋转中心
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
mCenterX = dm.widthPixels / 2;
mCenterY = dm.heightPixels / 2;
// 定义旋转方向
int duration = 1000;
lQuest1Animation = new Rotate3D(0, -90, mCenterX, mCenterY); // 下一页的【question1】旋转方向(从0度转到-90,参考系为水平方向为0度)
lQuest1Animation.setFillAfter(true);
lQuest1Animation.setDuration(duration);
lQuest2Animation = new Rotate3D(90, 0, mCenterX, mCenterY); // 下一页的【question2】旋转方向(从90度转到0,参考系为水平方向为0度)(起始第一题)
lQuest2Animation.setFillAfter(true);
lQuest2Animation.setDuration(duration);
rQuest1Animation = new Rotate3D(0, 90, mCenterX, mCenterY); // 上一页的【question1】旋转方向(从0度转到90,参考系为水平方向为0度)
rQuest1Animation.setFillAfter(true);
rQuest1Animation.setDuration(duration);
rQuest2Animation = new Rotate3D(-90, 0, mCenterX, mCenterY); // 上一页的【question2】旋转方向(从-90度转到0,参考系为水平方向为0度)
rQuest2Animation.setFillAfter(true);
rQuest2Animation.setDuration(duration);
}
2、Activity首先,定义两个布局文件,用于旋转的画面切换
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
next.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
...
</LinearLayout>
限于篇幅,完整布局文件请详见源码 ^_^
然后,初始化两个旋转的布局文件资源
private void initMain(){
setContentView(R.layout.main);
layoutmain = (LinearLayout)findViewById(R.id.layout_main);
btn_MainLast = (Button)findViewById(R.id.main_last);
btn_MainNext = (Button)findViewById(R.id.main_next);
btn_MainLast.setOnClickListener(listener);
btn_MainNext.setOnClickListener(listener);
}
private void initNext(){
setContentView(R.layout.next);
layoutnext = (LinearLayout)findViewById(R.id.layout_next);
btn_NextLast = (Button)findViewById(R.id.next_last);
btn_NextNext = (Button)findViewById(R.id.next_next);
btn_NextLast.setOnClickListener(listener);
btn_NextNext.setOnClickListener(listener);
}
最后,设置布局文件中的按钮监听事件,响应3D旋转动画和方向
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_last: // 上一页
layoutmain.startAnimation(lQuest1Animation); // 当前页向左旋转(0,-90)
initNext();
layoutnext.startAnimation(lQuest2Animation); // 下一页向左旋转(90, 0)
break;
case R.id.main_next: // 下一页
layoutmain.startAnimation(rQuest1Animation); // 当前页向右旋转(0,90)
initNext();
layoutnext.startAnimation(rQuest2Animation); // 下一页向右旋转(-90, 0)
break;
case R.id.next_last:
layoutnext.startAnimation(lQuest1Animation);
initMain();
layoutmain.startAnimation(lQuest2Animation);
break;
case R.id.next_next:
layoutnext.startAnimation(rQuest1Animation);
initMain();
layoutmain.startAnimation(rQuest2Animation);
break;
}
}
};
源码下载
参考推荐:
animation
分享到:
相关推荐
Android 滑动效果 3D旋转,Animation动画实现
Android读写XML(上)——package说明.doc Android读写XML(下)——创建XML文档.doc Android读写XML(中)——SAX.doc Android高手进阶教程之----Android Handler的使用!!! .doc Android高手进阶教程之----Android ...
安卓Android源码——3D立方体旋转demo.zip
android 高手 进阶 提高篇 android 高手 进阶 提高篇 android 高手 进阶 提高篇
第17章 益智类游戏——《旋转积木》本章介绍了Android 3D游戏《旋转积木》的开发。主要介绍了积木旋转的不同状态的实现方法和地图设计器的应用 第18章 休闲类游戏——《摩天大楼》本章介绍了Android 3D游戏...
【第一部分】历史文章: ...Android学习笔记(五)——Toast提示、Dialog对话框、Menu菜单 Android学习笔记(六)——自定义ListView布局+AsyncTask异步任务 Android学习笔记(七)——数据存储(共享参数Share
本代码是使用android开发3D娱乐游戏 3D迷宫
android开发进阶篇,实现文件的读写,在android中操作文件就像在自己的pc上一样简单
《Android高级进阶》从8个方面对50个知识点进行分类讲解,包括基础篇、系统架构篇、经验总结篇、新技术篇、性能优化篇、移动安全篇、工具篇、测试篇。熟练掌握这些知识点后,应该就能够应付实际项目开发中的绝大部分...
第14章 Android 3D娱乐游戏——3D迷宫 完整可运行例子
Android手机玩家进阶活用100%
资源为博客示例代码:http://blog.csdn.net/lmj623565791/article/details/24300125 有问题请留言
内容为Android应用开发热门的话题,包括自定义控件、第三方组件的使用和实现原理、Android应用架构等。
安卓Android源码——(旋转关节).rar
安卓Android源码——android桌面悬浮窗效果进阶 仿360手机卫士、淘宝手机助手.zip
2021年最新Android中高进阶面试集锦
《Android高级进阶》从8个方面对50个知识点进行分类讲解,包括基础篇、系统架构篇、经验总结篇、新技术篇、性能优化篇、移动安全篇、工具篇、测试篇。熟练掌握这些知识点后,应该就能够应付实际项目开发中的绝大部分...
Android进阶——Crash异常捕获并发送到服务器
Android 滑动效果 华丽翻页效果 Pager
Android 滑动效果ViewFlipper Android 滑动效果ViewFlipper