当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程实现3D滑动旋转效果的方法

Android编程实现3D滑动旋转效果的方法

2019年07月24日  | 移动技术网IT编程  | 我要评论

捐精门,极品太子之烈焰红唇,狼桥

本文实例讲述了android编程实现3d滑动旋转效果的方法。分享给大家供大家参考,具体如下:

这里我们通过代码实现一些滑动翻页的动画效果。

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; 
    } 
  } 
};

完整实例代码代码点击此处。

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网