广告
返回顶部
首页 > 资讯 > 移动开发 >Android利用Camera实现中轴3D卡牌翻转效果
  • 671
分享到

Android利用Camera实现中轴3D卡牌翻转效果

3dAndroid 2022-06-06 09:06:06 671人浏览 薄情痞子
摘要

在Android系统api中,有两个Camera类: android.graphics.Camera android.hardware.Camera 第二个应用

Android系统api中,有两个Camera类:

android.graphics.Camera android.hardware.Camera

第二个应用于手机硬件中的相机相关的操作,本文讲述的是利用第一个Camera类实现中轴3D转换的卡牌翻转效果,开始之前,先看一下Android系统中的坐标系:

对应于三维坐标系中的三个方向,Camera提供了三种旋转方法:

rotateX() rotateY() rotateX()

调用这三种方法,传入旋转角度参数,即可实现视图沿着坐标轴旋转的功能。本文的中轴3D旋转效果就是让视图沿着Y轴旋转的。

系统API Demos中已经为我们提供了一个非常好用的3D旋转动画的工具类:
Rotate3dAnimation.java:


package com.feng.androidtest;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.TransfORMation;

public class Rotate3dAnimation 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 Rotate3dAnimation(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();
  Log.i("interpolatedTime", interpolatedTime+"");
  camera.save();
  if (mReverse) {
   camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
  } else {
   camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
  }
  camera.rotateY(degrees);
  camera.getMatrix(matrix);
  camera.restore();
  matrix.preTranslate(-centerX, -centerY);
  matrix.postTranslate(centerX, centerY);
 }
}

可以看出, Rotate3dAnimation 总共做了两件事:在构造函数中赋值了旋转动画所需要的参数,以及重写(override)父类Animation中的applyTransformation()方法,下面分类阐述一下:

fromDegrees与toDegrees 视图旋转的开始角度和结束角度,当toDegree处于90倍数时,视图将变得不可见。 centerX与centerY 视图旋转的中心点。 depthZ Z轴移动基数,用于计算Camera在Z轴移动距离 reverse boolean类型,控制Z轴移动方向,达到视觉远近移动导致的视图放大缩小效果。 applyTransformation() 根据动画播放的时间 interpolatedTime (动画start到end的过程,interpolatedTime从0.0变化到1.0),让Camera在Z轴方向上进行相应距离的移动,实现视觉上远近移动的效果。然后调用 rotateX()方法,让视图围绕Y轴进行旋转,产生3D立体旋转效果。最后再通过Matrix来确定旋转的中心点的位置。

activity_main.xml布局文件:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="Http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="@android:color/white" >
 <Button
  android:id="@+id/btn_open"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="16dp"
  android:onClick="onClickView"
  android:text="打开"
  android:textColor="@android:color/black"
  android:textSize="16sp" />
 <RelativeLayout
  android:id="@+id/rl_content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_below="@id/btn_open"
  android:layout_marginTop="16dp" 
  android:background="@android:color/black">
  <ImageView
   android:id="@+id/iv_loGo"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:contentDescription="@null"
   android:src="@drawable/ic_qrcode" 
   android:scaleType="centerInside"/>
  <TextView
   android:id="@+id/tv_desc"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="16dp"
   android:text="编程网。"
   android:textColor="@android:color/white"
   android:textSize="18sp" 
   android:visibility="gone"/>
 </RelativeLayout>
</RelativeLayout>

布局中配置了卡牌正面的图片控件,卡牌背面的文本控件,以及他们的parent容器,也就是本文中的旋转动画的执行对象。

MainActivity.java文件:


package com.feng.androidtest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.androidtest.R;
public class MainActivity extends Activity {
 private RelativeLayout mContentRl;
 private ImageView mLogoIv;
 private TextView mDescTv;
 private Button mOpenBtn;
 private int centerX;
 private int centerY;
 private int depthZ = 400;
 private int duration = 600;
 private Rotate3dAnimation openAnimation;
 private Rotate3dAnimation closeAnimation;
 private boolean isOpen = false;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mContentRl = (RelativeLayout) findViewById(R.id.rl_content);
  mLogoIv = (ImageView) findViewById(R.id.iv_logo);
  mDescTv = (TextView) findViewById(R.id.tv_desc);
  mOpenBtn = (Button) findViewById(R.id.btn_open);
 }
 
 private void initOpenAnim() {
  //从0到90度,顺时针旋转视图,此时reverse参数为true,达到90度时动画结束时视图变得不可见,
  openAnimation = new Rotate3dAnimation(0, 90, centerX, centerY, depthZ, true);
  openAnimation.setDuration(duration);
  openAnimation.setFillAfter(true);
  openAnimation.setInterpolator(new AccelerateInterpolator());
  openAnimation.setAnimationListener(new AnimationListener() {
   @Override
   public void onAnimationStart(Animation animation) {
   }
   @Override
   public void onAnimationRepeat(Animation animation) {
   }
   @Override
   public void onAnimationEnd(Animation animation) {
    mLogoIv.setVisibility(View.GONE);
    mDescTv.setVisibility(View.VISIBLE);
    //从270到360度,顺时针旋转视图,此时reverse参数为false,达到360度动画结束时视图变得可见
    Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(270, 360, centerX, centerY, depthZ, false);
    rotateAnimation.setDuration(duration);
    rotateAnimation.setFillAfter(true);
    rotateAnimation.setInterpolator(new DecelerateInterpolator());
    mContentRl.startAnimation(rotateAnimation);
   }
  });
 }
 
 private void initCloseAnim() {
  closeAnimation = new Rotate3dAnimation(360, 270, centerX, centerY, depthZ, true);
  closeAnimation.setDuration(duration);
  closeAnimation.setFillAfter(true);
  closeAnimation.setInterpolator(new AccelerateInterpolator());
  closeAnimation.setAnimationListener(new AnimationListener() {
   @Override
   public void onAnimationStart(Animation animation) {
   }
   @Override
   public void onAnimationRepeat(Animation animation) {
   }
   @Override
   public void onAnimationEnd(Animation animation) {
    mLogoIv.setVisibility(View.VISIBLE);
    mDescTv.setVisibility(View.GONE);
    Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(90, 0, centerX, centerY, depthZ, false);
    rotateAnimation.setDuration(duration);
    rotateAnimation.setFillAfter(true);
    rotateAnimation.setInterpolator(new DecelerateInterpolator());
    mContentRl.startAnimation(rotateAnimation);
   }
  });
 }
 public void onClickView(View v) {
  //以旋转对象的中心点为旋转中心点,这里主要不要再onCreate方法中获取,因为视图初始绘制时,获取的宽高为0
  centerX = mContentRl.getWidth()/2;
   centerY = mContentRl.getHeight()/2;
   if (openAnimation == null) {
   initOpenAnim();
   initCloseAnim();
  }
   //用作判断当前点击事件发生时动画是否正在执行
  if (openAnimation.hasStarted() && !openAnimation.hasEnded()) {
   return;
  }
  if (closeAnimation.hasStarted() && !closeAnimation.hasEnded()) {
   return;
  }
  //判断动画执行
  if (isOpen) {
   mContentRl.startAnimation(closeAnimation);
  }else {
   mContentRl.startAnimation(openAnimation);
  }
  isOpen = !isOpen;
  mOpenBtn.setText(isOpen ? "关闭" : "打开");
 }
}

代码中已对核心的地方做了注释解释,主要弄清楚 rotate3dAnimation构造参数中的 fromDegrees和toDegrees、depthZ、reverse参数,同时在动画中设置了速度插播器,如动画的前半程使用加速器 AccelerateInterpolator,后半程使用减速器 DecelerateInterpolator,使动画体验更加人性化。

您可能感兴趣的文章:Android实现图片反转、翻转、旋转、放大和缩小Android动画之3D翻转效果实现函数分析Android图片翻转动画简易实现代码Android实现Flip翻转动画效果Android实现文字翻转动画的效果Android实现卡片翻转动画Android使用animator实现fragment的3D翻转效果Android实现3D翻转动画效果android使用FlipAnimation实现3D垂直翻转动画


--结束END--

本文标题: Android利用Camera实现中轴3D卡牌翻转效果

本文链接: https://www.lsjlt.com/news/26054.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android利用Camera实现中轴3D卡牌翻转效果
    在Android系统API中,有两个Camera类: android.graphics.Camera android.hardware.Camera 第二个应用...
    99+
    2022-06-06
    3d Android
  • 利用Vue实现卡牌翻转的特效
    目录前言实现鼠标移入选中效果卡片翻转效果完整代码结语前言 今天是正月初九,也是活动的倒数第二天,复工都三天了,而我三篇春节文章还没写完,实在是太混了!这次带来的是一个春节抽福卡页面,...
    99+
    2022-11-13
  • Unity中怎么利用Shader实现一个3D翻页效果
    本篇文章给大家分享的是有关Unity中怎么利用Shader实现一个3D翻页效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。效果图:原理:Shader顶点动画在顶点着色器进行对...
    99+
    2023-06-20
  • 利用CSS实现卡片翻转效果的方法和示例
    在现代的网页设计中,翻转效果是一种常见且炫酷的特效,可以为网页增添一份动感和交互性。利用CSS的转换属性和动画属性,我们可以轻松地实现卡片的翻转效果。本文将介绍一种基础的卡片翻转效果,并提供具体的代码示例供读者参考。卡片翻转效果是指将卡片从...
    99+
    2023-10-21
    CSS 卡片翻转 方法和示例
  • 如何在Android中利用TextSwitcher实现一个文字上下翻牌效果
    今天就跟大家聊聊有关如何在Android中利用TextSwitcher实现一个文字上下翻牌效果,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。tvNotice = (TextSwitc...
    99+
    2023-05-31
    android textswitcher roi
  • 怎么在android应用中利用ViewPager实现一个滑动翻页效果
    这期内容当中小编将会给大家带来有关怎么在android应用中利用ViewPager实现一个滑动翻页效果,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。实现ViewPager的滑动翻页效果可以使用ViewPa...
    99+
    2023-05-31
    viewpager android age
  • 怎么在Android应用中利用Bitmap实现一个位图旋转效果
    怎么在Android应用中利用Bitmap实现一个位图旋转效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。位图的旋转也可以借助Matrix或者Canvas来实现。通过po...
    99+
    2023-05-31
    android bitmap roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作