原文地址:http://blog.csdn.net/u012215170/article/details/50421023

ndroid手机上有众多的炫酷的设计风格,动画必然是APP的一大亮点,所以一个好看的动画效果的实现一直是很多程序员所追求的,所以今天我就给大家分享一个不错的3D翻转动画,让你的APP炫酷起来.
首先对于3D我们并不陌生我们都知道X轴Y轴,但是对于3D效果来说我们还需要一个Z轴,所以我们直接上代码,看一看我们这个工具类.
import android.graphics.Camera;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.Transformation;public class Rotate3d extends Animation{  private final float mFromDegrees;  private final float mToDegrees;  private final float mCenterX;  private final float mCenterY;  private final float mDepthZ;  private final boolean mReverse;  private Camera mCamera;  public Rotate3d(float fromDegrees, float toDegrees,  float centerX, float centerY, float depthZ, boolean reverse) {  mFromDegrees = fromDegrees;  mToDegrees = toDegrees;  mCenterX = centerX;  mCenterY = centerY;  mDepthZ = depthZ;  mReverse = reverse;  }  @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 fromDegrees = mFromDegrees;  float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);  final float centerX = mCenterX;  final float centerY = mCenterY;  final Camera camera = mCamera;  final Matrix matrix = t.getMatrix();  // 将当前的摄像头位置保存下来,以便变换进行完成后恢复成原位,  camera.save();  // camera.translate,这个方法接受3个参数,分别是x,y,z三个轴的偏移量,我们这里只将z轴进行了偏移,  if (mReverse) {  // z的偏移会越来越大。这就会形成这样一个效果,view从近到远  camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);  } else {  // z的偏移会越来越小。这就会形成这样一个效果,我们的View从一个很远的地方向我们移过来,越来越近,最终移到了我们的窗口上面~  camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));  }  // 是给我们的View加上旋转效果,在移动的过程中,视图还会移Y轴为中心进行旋转。  camera.rotateY(degrees);  // 是给我们的View加上旋转效果,在移动的过程中,视图还会移X轴为中心进行旋转。  // camera.rotateX(degrees);  // 这个是将我们刚才定义的一系列变换应用到变换矩阵上面,调用完这句之后,我们就可以将camera的位置恢复了,以便下一次再使用。  camera.getMatrix(matrix);  // camera位置恢复  camera.restore();  // 以View的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心  matrix.preTranslate(-centerX, -centerY);  matrix.postTranslate(centerX, centerY);  }
}  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364

这个工具类很重要,算是我们3D翻转效果的核心.

接下来让我们看看具体的动画实现吧

private void applyRotation(int position, float start, float end, ViewGroup vg, ImageView imageView, int IMAGE) {        //获取FrameLayout的x、y值。这样图片在翻转的时候会以这个x、y值为中心翻转。//这就是为什么我要用FrameLayout的原因。如果直接使用的是父容器RelativeLayout将会以RelativeLayout的中心为轴心//翻转。由于我的图片不是处于RelativeLayout的中心,翻转时就会有差错.效果可以看看下面的图片。//当然,有时候你就想要那样的效果。你也可以在自行调整centerX和centerY的值来达到你想要的效果final float centerX = vg.getWidth() / 2.0f;        final float centerY = vg.getHeight() / 2.0f;        final Rotate3d rotation =                new Rotate3d(start, end, centerX, centerY, 310.0f, true);rotation.setDuration(1000);  //可设置翻转的时间,以ms为单位rotation.setFillAfter(true);rotation.setInterpolator(new AccelerateInterpolator());rotation.setAnimationListener(new DisplayNextView(imageView, IMAGE, vg));vg.startAnimation(rotation);  //开始翻转前90度}12345678910111213141516
private final class DisplayNextView implements Animation.AnimationListener {private ImageView imageView;        private int IMAGE;        private ViewGroup vg;        public DisplayNextView(ImageView imageView, int IMAGE, ViewGroup vg) {            this.IMAGE = IMAGE;            this.vg = vg;            this.imageView = imageView;}        public void onAnimationStart(Animation animation) {}        public void onAnimationEnd(Animation animation) {            //前90度翻转完成后,根据图片的状态翻转剩下的90度imageView.setImageResource(IMAGE);vg.post(new SwapViews(1, vg));}        public void onAnimationRepeat(Animation animation) {}}12345678910111213141516171819202122232425

让图片自动翻转

    private final class SwapViews implements Runnable {private final int mdirection;        private ViewGroup vg;        //这里用一个方向变量来指明剩下的90度应该怎么翻转。public SwapViews(int direction, ViewGroup vg) {mdirection = direction;            this.vg = vg;}        public void run() {            final float centerX = vg.getWidth() / 2.0f;            final float centerY = vg.getHeight() / 2.0f;Rotate3d rotation;            if (mdirection == 0) {rotation = new Rotate3d(90, 0, centerX, centerY, 310.0f, false);which = true;//待翻转完成后,修改图片状态} else {rotation = new Rotate3d(-90, 0, centerX, centerY, 310.0f, false);which = false;}rotation.setDuration(1000);rotation.setFillAfter(true);rotation.setInterpolator(new DecelerateInterpolator());vg.startAnimation(rotation);  //开始翻转余下的90度}}1234567891011121314151617181920212223242526272829
 /*** 开启子线程自动翻转** @param what*/public void roll(final int what) {        new Thread(new Runnable() {            @Overridepublic void run() {                for (int i = 0; i < 0xABCL; i++) {handler.sendEmptyMessage(what);                    try {Thread.sleep(2 * rand() * 1000);} catch (InterruptedException e) {e.printStackTrace();}}}}).start();}1234567891011121314151617181920
/*** 图片翻转动画*/private void overturn() {handler = new Handler(new Handler.Callback() {            @Overridepublic boolean handleMessage(Message message) {                if (message.what == 201) {                    if (!which) {applyRotation(0, 0, -90, vp1, img1, 0);//左旋90度} else {applyRotation(0, 0, 90, vp1, img1, IMAGE1);//右旋90度}} else if (message.what == 202) {                    if (!which) {applyRotation(0, 0, 90, vp2, img2, 4);//右旋90度} else {applyRotation(0, 0, -90, vp2, img2, IMAGE2);//左旋90度}}                 return false;}});}123456789101112131415161718192021222324

这样我们的图片翻转功能就可以实现了,把IMAGE设置成自己想使用的图片就OK了.

转载于:https://my.oschina.net/u/2531415/blog/651428

图片3D翻转效果 --摘自李硕老师博客160305相关推荐

  1. 基于Android自带插入器的图形波动效果 --转载自李硕老师博客160303

    原文地址:http://blog.csdn.net/u012215170/article/details/50598747 对于Android的各种动画我们并不陌生,但是可能很多同学不知道Interp ...

  2. CSS3之图片3D翻转效果(网页效果--每日一更)

    今天,带来的是纯CSS3的效果--图片3D翻转. 请看效果:亲,请点击这里 这个效果主要还是运用了CSS3的transform变形属性,与上个效果不同的是,这次并不是动画,所以没有采用animatio ...

  3. html3d上下翻转4面效果,花式实现图片3D翻转效果

    闲话 曾经在闲逛时,看到有一个很炫的3D翻转切换图片的效果.地址在这里:https://tympanus.net/Development/Slicebox/index.html 一直想搞一个,最近撸出 ...

  4. 花式实现图片3D翻转效果

    闲话 曾经在闲逛时,看到有一个很炫的3D翻转切换图片的效果.地址在这里:https://tympanus.net/Development/Slicebox/index.html 一直想搞一个,最近撸出 ...

  5. html5 鼠标旋转动画效果,CSS3鼠标滑过图片3D翻转动画特效

    这是一款效果非常炫酷的CSS3鼠标滑过图片3D翻转动画特效.该特效基于Bootstrap网格系统来布局,通过简单的CSS3代码,在鼠标滑过图片时对图片进行3D翻转,效果非常的酷. 使用方法 HTML结 ...

  6. html关于实现图片2D,3D翻转效果

    如何实现图片2D,3D翻转效果? 直接上代码: 一.2D翻转: /*2D翻转*/ .cartoon_0 {transition: transform 2s/*反转动画关键*/} /*利用hover设置 ...

  7. 用css实现扑克牌,图片的翻转效果

    用css实现扑克牌,图片的翻转效果 话不多说,直接上代码! 1.实现商品图片的翻转以及信息的展示(下面代码仅是个人想要效果的HTML代码,所有的class名称可根据自身需求进行改动) <!doc ...

  8. css和js实现3d图片,JavaScript_纯JS实现旋转图片3D展示效果,CSS:style type=text/cssgt - phpStudy...

    纯JS实现旋转图片3D展示效果 CSS: #show{position:relative;margin:20px auto;width:800px;} .item{position:absolute; ...

  9. android左右旋转动画效果图,Android新姿势:3D翻转效果原理

    首先,android里是没有3D翻转的动画效果的,但是呢,android有提供一个Camera的类,可以利用这个类来实现. 先看代码,Rotate3d是继承了Animation的一个动画类,多余的代码 ...

最新文章

  1. Apache Unable to find the wrapper https - did you forget to enable it when you configured PHP?
  2. 03flex弹性布局父项常见属性总结
  3. zookeeper-一个关于paxos的故事
  4. JAVa读取文件的附加属性,Java 读取属性文件
  5. 【DIY】最简单粗暴便宜的DIY定时器方法,没有之一
  6. 纯CSS实现气泡聊天框的方法
  7. Codechef Yet another cute girl
  8. Ubuntu下安装配置JDK1.7
  9. HDU1856More is better(并查集)
  10. 本地html如何导出pdf,html表格以pdf格式导出到本地
  11. 什么是数据脱敏(Data Masking)?
  12. c语言二级题库中会有错题吗,二级C语言题库-改错题
  13. android 沙盒 ios,iOS之沙盒机制
  14. Android怎么改图标都不生效安卓开发如何修改APP图标和名字
  15. 【转】Excel表格的35招必学秘技
  16. 君不密则失臣,臣不密则失身,机事不密则害成
  17. graphpad prism横坐标怎么设置不显示数值_graphpad,prism,符号显示有问题
  18. 利用Git命令进行版本控制之常见命令汇总
  19. 外国人入境日本 后天起须留指印头像
  20. 第2关:求五边形的面积

热门文章

  1. [完整]首届盘古石杯电子数据取证大赛晋级赛Writeup
  2. 数据包分析——数据链路层 和 网络层
  3. Programming with Multiple Paradigms in Lua(Object-Oriented Programming)
  4. 3.Flask基础-2
  5. FANUC机器人调试时必会遇到的故障
  6. excel 汇总 mysql_Excel中用SQL语句实现多工作簿汇总
  7. 列生成算法求解矩形下料问题(Matlab代码)
  8. 用c#在excel中插入图片和设置表格宽度
  9. 关于ThreeJs纹理贴图动画的实现
  10. 企业实施5S管理经典推行步骤及注意事项(完整收藏版)