当前位置: 移动技术网 > 移动技术>移动开发>Android > 基于Android实现3D翻页效果

基于Android实现3D翻页效果

2019年07月24日  | 移动技术网移动技术  | 我要评论
最近做了一个简单的3d效果翻页特效,先说说我的思路吧,首先我这个翻页效果并不是两个activity之间的跳转,而是在同一个activity类切换不同的view而已。我现在的

最近做了一个简单的3d效果翻页特效,先说说我的思路吧,首先我这个翻页效果并不是两个activity之间的跳转,而是在同一个activity类切换不同的view而已。我现在的做法是单击一个button然后gone当前的布局,然后把需要呈现的布局visible,在隐藏当前布局的时候启动动画,然后给动画添加监听,在动画结束时开始另外一个view的入场动画就行了。
下面来看下我的主页面的布局文件:

<framelayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/container" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" > 
 
 <include 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 layout="@layout/layout2" /> 
 
 <include 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 layout="@layout/layout1" /> 
 
</framelayout> 

我这个布局文件使用<include>标签包含了另外2个布局文件,这些布局文件才是呈现数据的,下面是另外2个布局文件:

layout1:

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@drawable/test1" 
 android:orientation="vertical" 
 android:id="@+id/container1" 
 > 
 
 <button 
 android:id="@+id/bt_towhile" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center" 
 android:text="白色" /> 
 
</linearlayout> 

layout2:

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/container2" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:background="@drawable/test2" 
 android:orientation="vertical" > 
 
 <button 
 android:id="@+id/bt_toblack" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center" 
 android:text="黑色" /> 
 
</linearlayout> 

我这里只是举个例子并没有放什么实际的类容,只是放了2个button,当我点击其中一个跳转到另外一个layout。

有了布局文件那我们就开始要实现功能了,我们的想法是点击按钮的时候开始一个动画等动画结束时再开启另外一个动画并隐藏和展示layout1和layout2。

下面是我写的一个动画工具类源码:

package com.test.view; 
 
import android.view.view; 
import android.view.viewgroup; 
import android.view.animation.animation; 
import android.view.animation.decelerateinterpolator; 
 
public class rotateanimationutil { 
 
 private viewgroup context; 
 
 private view[] views; 
 
 public rotateanimationutil(viewgroup context, view... views) { 
 super(); 
 this.context = context; 
 this.views = views; 
 } 
 
 /** 
 * 应用自定义的rotate3danimation动画 
 * 
 * @param flag 
 *  当前控件的顺序坐标 
 * @param startdegress 
 *  开始的角度 
 * @param enddegress 
 *  结束的角度 
 */ 
 public void applyrotateanimation(int flag, float startdegress, 
  float enddegress) { 
 final float centerx = context.getwidth() / 2.0f; 
 final float centery = context.getheight() / 2.0f; 
 
 rotate3danimation rotate = new rotate3danimation(startdegress, 
  enddegress, centerx, centery, 310.0f, true); 
 rotate.setduration(1000); 
 rotate.setfillafter(false); 
 rotate.setinterpolator(new decelerateinterpolator()); 
 
 rotate.setanimationlistener(new displaynextview(flag)); 
 context.startanimation(rotate); 
 } 
 
 private final class displaynextview implements animation.animationlistener { 
 
 private final int mflag; 
 
 private displaynextview(int flag) { 
  mflag = flag; 
 } 
 
 public void onanimationstart(animation animation) { 
 
 } 
 
 // 动画结束 
 
 public void onanimationend(animation animation) { 
  context.post(new swapviews(mflag)); 
 } 
 
 public void onanimationrepeat(animation animation) { 
 
 } 
 } 
 
 /** 
 * 新开一个线程动画结束后再开始一次动画效果实现翻屏特效 
 * 
 * @author yangzhiqiang 
 * 
 */ 
 private final class swapviews implements runnable { 
 
 private final int mflag; 
 
 public swapviews(int mflag) { 
  this.mflag = mflag; 
 } 
 
 public void run() { 
  final float centerx = context.getwidth() / 2.0f; 
  final float centery = context.getheight() / 2.0f; 
  rotate3danimation rotation; 
  if (mflag > -1) { 
  views[0].setvisibility(view.gone); 
  views[1].setvisibility(view.visible); 
  views[1].requestfocus(); 
  rotation = new rotate3danimation(270, 360, centerx, centery, 
   310.0f, false); 
  } else { 
  views[1].setvisibility(view.gone); 
  views[0].setvisibility(view.visible); 
  views[0].requestfocus(); 
  rotation = new rotate3danimation(90, 0, centerx, centery, 
   310.0f, false); 
  } 
  rotation.setduration(1000); 
  rotation.setfillafter(false); 
  rotation.setinterpolator(new decelerateinterpolator()); 
  // 开始动画 
  context.startanimation(rotation); 
 
 } 
 
 } 
 
} 

解释一下这个类的构造方法:

public rotateanimationutil(viewgroup context, view... views) { 
 super(); 
 this.context = context; 
 this.views = views; 
 } 

有2个参数,第一个参数就是我们的主布局页面的framelayout,第2个参数就是我们要进行动画切换的2个子layout,我这使用的是一个可变长参数只是为了方便而已。

public void applyrotateanimation(int flag, float startdegress,float enddegress)方法第一个参数是标记当前是第是从哪个个layout跳转,因外我们必须知道当前开始跳转的layout才能推算角度。
下面是我自定义动画的源码:

package com.test.view; 
 
import android.graphics.camera; 
import android.graphics.matrix; 
import android.view.animation.animation; 
import android.view.animation.transformation; 
 
public class rotate3danimation extends animation { 
 
 private final float mformdegress; 
 
 private final float mtodegress; 
 
 private final float mcenterx; 
 
 private final float mcentery; 
 
 /** 
 * 控制z轴的一个常量值,主要是控制动画的升降距离 
 */ 
 private final float mdepthz; 
 
 /** 
 * 控制z轴是像上移动还是向下移动,从而实现升降效果 
 */ 
 private final boolean mreverse; 
 
 private camera mcamera; 
 
 public rotate3danimation(float formdegress, float todegress, float centerx, 
  float centery, float depthz, boolean reverse) { 
 super(); 
 this.mformdegress = formdegress; 
 this.mtodegress = todegress; 
 this.mcenterx = centerx; 
 this.mcentery = centery; 
 this.mdepthz = depthz; 
 this.mreverse = reverse; 
 } 
 
 @override 
 public void initialize(int width, int height, int parentwidth, 
  int parentheight) { 
 super.initialize(width, height, parentwidth, parentheight); 
 mcamera = new camera(); 
 } 
 
 /** 
 * interpolatedtime 取值范围是0-1之间当每次,当动画启动后会系统会不停的调用applytransformation方法, 
 * 并改变interpolatedtime的值 
 */ 
 @override 
 protected void applytransformation(float interpolatedtime, transformation t) { 
 final float formdegress = mformdegress; 
 // 通过差点值计算出转变的角度 
 float degress = formdegress 
  + ((mtodegress - formdegress) * interpolatedtime); 
 final float centerx = mcenterx; 
 final float centery = mcentery; 
 final camera camera = mcamera; 
 
 // 得到当前矩阵 
 matrix matrix = t.getmatrix(); 
 // 报错当前屏幕的状态 
 camera.save(); 
 // 判断是降还是升 
 if (mreverse) { 
  // 正向改变z轴角度 
  camera.translate(0.0f, 0.0f, mdepthz * interpolatedtime); 
 } else { 
  // 反向改变z轴角度 
  camera.translate(0.0f, 0.0f, mdepthz * (1.0f - interpolatedtime)); 
 } 
 // 旋转y轴角度 
 camera.rotatey(degress); 
 // 把当前改变后的矩阵信息复制给transformation的matrix 
 camera.getmatrix(matrix); 
 // 根据改变后的矩阵信息从新恢复屏幕 
 camera.restore(); 
 
 // 让动画在屏幕中间运行 
 matrix.pretranslate(-centerx, -centery); 
 matrix.posttranslate(centerx, centery); 
 } 
} 

如果你不需要沉降效果那么你把下面的代码删除掉即可:

if (mreverse) { 
  // 正向改变z轴角度 
  camera.translate(0.0f, 0.0f, mdepthz * interpolatedtime); 
 } else { 
  // 反向改变z轴角度 
  camera.translate(0.0f, 0.0f, mdepthz * (1.0f - interpolatedtime)); 
 } 

好了核心代码已经上完,下面是主界面代码:

package com.test.rotateanimation; 
 
import android.app.activity; 
import android.os.bundle; 
import android.view.menu; 
import android.view.view; 
import android.view.viewgroup; 
import android.widget.button; 
import android.widget.framelayout; 
import android.widget.linearlayout; 
 
import com.test.view.rotateanimationutil; 
 
public class mainactivity extends activity { 
 
 private framelayout container; 
 
 private linearlayout container1; 
 
 private linearlayout container2; 
 
 private rotateanimationutil rotateanimationutil; 
 
 private button bt_towhile; 
 
 private button bt_toblack; 
 
 @override 
 public void oncreate(bundle savedinstancestate) { 
 super.oncreate(savedinstancestate); 
 setcontentview(r.layout.activity_main); 
 initview(); 
 
 bt_towhile.setonclicklistener(new view.onclicklistener() { 
 
  @override 
  public void onclick(view v) { 
  rotateanimationutil.applyrotateanimation(1, 0, 90); 
  } 
 }); 
 bt_toblack.setonclicklistener(new view.onclicklistener() { 
 
  @override 
  public void onclick(view v) { 
  rotateanimationutil.applyrotateanimation(-1, 0, -90); 
  } 
 }); 
 
 // 设置当前view控件的缓存 
 container 
  .setpersistentdrawingcache(viewgroup.persistent_animation_cache); 
 } 
 
 private void initview() { 
 container = (framelayout) findviewbyid(r.id.container); 
 bt_toblack = (button) findviewbyid(r.id.bt_toblack); 
 bt_towhile = (button) findviewbyid(r.id.bt_towhile); 
 
 container1 = (linearlayout) findviewbyid(r.id.container1); 
 container2 = (linearlayout) findviewbyid(r.id.container2); 
 
 rotateanimationutil = new rotateanimationutil(container, container1, 
  container2); 
 } 
 
 @override 
 public boolean oncreateoptionsmenu(menu menu) { 
 getmenuinflater().inflate(r.menu.activity_main, menu); 
 return true; 
 } 
 
} 

下面是运行效果,剪切效果不好,呵呵.

 源码下载:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网