最近在研究iphone上的翻转效果,就是类似于passbook里面的查看票据的时候,正面是一种布局,点击某个按钮会翻转到背面,然后呈现的又是另一个布局。就在想怎么用到android里面呢?正好,最近项目有这个需求。于是便上网搜寻有关android的翻转动画的资料。在网上找到原来apidemos里面就提供了翻转动画,不过是针对图片的翻转,但是我想只要有动画就是一样的调用,管它图片还是布局呢,最后还不是xxx.startAnimation。翻转动画如下,如果没有apidemos的童鞋可以直接拿去用。
package com.huiian.kelu.util; 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 fromDegrees; private final float toDegrees; private final float centerX; private final float centerY; private final float depthZ; private final boolean reverse; private Camera camera; public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) { this.fromDegrees = fromDegrees; this.toDegrees = toDegrees; this.centerX = centerX; this.centerY = centerY; this.depthZ = depthZ; this.reverse = reverse; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); camera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = this.fromDegrees; float degrees = fromDegrees + ((toDegrees - fromDegrees) * interpolatedTime); final float centerX = this.centerX; final float centerY = this.centerY; final Camera camera = this.camera; final Matrix matrix = t.getMatrix(); camera.save(); if (reverse) { camera.translate(0.0f, 0.0f, depthZ * interpolatedTime); } else { camera.translate(0.0f, 0.0f, depthZ * (1.0f - interpolatedTime)); } camera.rotateY(degrees); camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
有了这个动画类之后,就可以像调用android基本动画的方式来调用了,因为大家可以看到这个翻转动画是继承于Animation的。那具体该如何实现布局的翻转呢?首先你的布局代码是:
<FrameLayout android:id="@id/main_fl" android:layout_width="214dp" android:layout_height="246dp" android:layout_centerInParent="true" android:orientation="vertical" android:visibility="invisible" > <include layout="@layout/zheng" /> <include layout="@layout/fan" /> </FrameLayout>
一个正,一个反,剩下的你自己布局就行。
剩下的在Activity里面的用法apidemos也给出了,大家只需要把一些布局替换成自己的就行了。好了,这个时候坑爹的来了,因为你会发现翻转过去的布局是倒着的,我被这个鬼东西折腾了好久,因为我发现apidemos里面的例子也是这样的,它的图片也是倒的。这让人情何以堪。于是试了许久终于解决了。首先我们定义一个记录正反的bool值
private boolean nowZhengFan = true;// 默认当前是正面
然后我们是点击布局来切换正反面的,所以将布局设置OnClickListener监听。然后在onClick方法里面写:
case R.id.main_fl: if(nowZhengFan) { applyRotation(0, 0, 90); } else { applyRotation(-1, 360, 270); } break;
private void applyRotation(int position, float start, float end) { float centerX = mainFL.getWidth() / 2.0f; float centerY = mainFL.getWidth() / 2.0f; final Rotate3dAnimation animation = new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true); animation.setDuration(500); animation.setFillAfter(false); animation.setInterpolator(new AccelerateInterpolator()); animation.setAnimationListener(new DisplayNextView(position, true)); mainFL.startAnimation(animation); } private class DisplayNextView implements AnimationListener { private int position; private boolean b; public DisplayNextView(int pos,boolean b) { this.b = b; this.position = pos; } @Override public void onAnimationEnd(Animation animation) { mainFL.post(new Swap(position)); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } } private class Swap implements Runnable { private int position; public Swap(int pos) { this.position = pos; } @Override public void run() { float centerX = missionFL.getWidth() / 2.0f; float centerY = missionFL.getWidth() / 2.0f; Rotate3dAnimation rotation = null; if(position > -1) { rotation = new Rotate3dAnimation(270, 360, centerX, centerY, 310.0f, false); zheng.setVisibility(View.GONE); fan.setVisibility(View.VISIBLE); nowZhengFan = false; } else { rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false); zheng.setVisibility(View.VISIBLE); fan.setVisibility(View.GONE); nowZhengFan = true; } rotation.setDuration(500); rotation.setFillAfter(false); rotation.setInterpolator(new DecelerateInterpolator()); mainFL.startAnimation(rotation); } }
这里的关键是点击之后的第一次翻转是0--90度,然后调用swap进行第二次翻转,apidemos里面的是90-180度,但是这样就会导致布局是反的,于是要改成270-360度,这样就ok了。如果是翻回去,那么就是在点击之后是360-270度,这样就成功了。大家可以试试。
相关推荐
类似于支付宝五福图片翻转效果
一个自定义的android的翻转textview,效果很不错的
通过android的简单图片翻转动画,来熟悉android普通动画的使用方法。
android 两个activity 翻转动画切换。谁如果把activity切换时的动画连贯了。给我说一下。
Simple flip animation demo.
android卡片翻转效果,使用view的draw方法直接绘制,节省性能,如果放到liestview或者recycleview中的话,请自行稍加修改,不要使用bitmap来做存储即可,本想0分,但是目前至少要选1分,没法办
NULL 博文链接:https://hz-chenwenbiao-91.iteye.com/blog/1991264
实现android图片翻转效果,可先到我博客看看效果再下载
android中图片翻转Demo,
这是一个可以给头像的添加翻转动画的 demo , 做到了 一个翻转,循环翻转,每一面的不同点击事件监听
Android 利用Camera实现中轴3D卡牌翻转效果,博客地址:http://blog.csdn.net/growing_tree/article/details/50428148
css3产品展示卡片布局翻转特效,一款样式精美的云服务器介绍展示布局效果,鼠标悬停卡片翻转查看更多信息。
最近发现另外一哥们改3d翻转的代码,改的还不错,将代码分享出来
NULL 博文链接:https://b275518834201204034558.iteye.com/blog/1486820
adroid简单实现3D翻转动画,一封装成类,可以拿来即用。----------->赶快下载
android 自适应屏幕 翻转 网络转发 共享给初学者
android,Gallery,页面翻转。画廊每个条目有页面翻转效果。
android实现仿IOS翻转动画效果,在谷歌官方方案基础上改进。使用kotlin语言。
近几年,随着移动计算机技术的快速发展,移动生活和学习越来越方便老师和学生。它旨在让学生与老师在学习和...翻转课堂可以满足以上要求,更加贴近学习,让学生更加了解自己在课堂的信息,让老师更加明白学生的情况。
Android点击翻转控件源码