Android编程实现3D旋转效果实例

 更新时间:2016年01月11日 09:23:08   作者:Healtheon  
这篇文章主要介绍了Android编程实现3D旋转效果的方法,基于Android的Camera类实现坐标变换达到图片3D旋转效果,需要的朋友可以参考下

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

下面的示例是在Android中实现图片3D旋转的效果。

实现3D效果一般使用OpenGL,但在Android平台下可以不直接使用OpenGL,而是使用Camera实现,Camera中原理最终还是使用OpenGL,不过使用Camera比较方便。 Camera类似一个摄像机,当物体不动时,我们带着摄像机四处移动,在摄像机里面的画面就会有立体感,就可以从其它的角度观看这个物体。废话不多说,直接看示例。

运行效果如下:

  

 

项目结构:

MainView.java中代码:

package com.android.graphics;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MainView extends View{
   //Camera类
   private Camera mCamera;
   private Bitmap face;
   private Matrix mMatrix = new Matrix();
   private Paint mPaint = new Paint();
   private int mLastMotionX, mLastMotionY;
   //图片旋转时的中心点坐标
   private int centerX, centerY;
   //转动的总距离,跟度数比例1:1
   private int deltaX, deltaY;
   //图片宽度高度
   private int bWidth, bHeight;
   public MainView(Context context,AttributeSet attributeSet) {
   super(context,attributeSet);
   setWillNotDraw(false);
   mCamera = new Camera();
   mPaint.setAntiAlias(true);
   face = BitmapFactory.decodeResource(getResources(), R.drawable.x);
   bWidth = face.getWidth();
   bHeight = face.getHeight();
   centerX = bWidth>>1;
   centerY = bHeight>>1;
   }
   void rotate(int degreeX, int degreeY) {
   deltaX += degreeX;
   deltaY += degreeY;
   mCamera.save();
   mCamera.rotateY(deltaX);
   mCamera.rotateX(-deltaY);
   mCamera.translate(0, 0, -centerX);
   mCamera.getMatrix(mMatrix);
   mCamera.restore();
   //以图片的中心点为旋转中心,如果不加这两句,就是以(0,0)点为旋转中心
   mMatrix.preTranslate(-centerX, -centerY);
   mMatrix.postTranslate(centerX, centerY);
   mCamera.save();
   postInvalidate();
   }
   @Override
   public boolean onTouchEvent(MotionEvent event) {
   int x = (int) event.getX();
   int y = (int) event.getY();
   switch(event.getAction()) {
   case MotionEvent.ACTION_DOWN:
    mLastMotionX = x;
    mLastMotionY = y;
    break;
   case MotionEvent.ACTION_MOVE:
    int dx = x - mLastMotionX;
    int dy = y - mLastMotionY;
    rotate(dx, dy);
    mLastMotionX = x;
    mLastMotionY = y;
    break;
   case MotionEvent.ACTION_UP:
    break;
   }
   return true;
   }
   @Override
   public void dispatchDraw(Canvas canvas) {
   super.dispatchDraw(canvas);
   canvas.drawBitmap(face, mMatrix, mPaint);
   }
}

main.xml中代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 <com.android.graphics.MainView
  android:id="@+id/cv"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
 />
</LinearLayout>

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

相关文章

  • 实现轮转广告带底部指示的自定义ViewPager控件

    实现轮转广告带底部指示的自定义ViewPager控件

    在项目中经常需要使用轮转广告的效果,在android-v4版本中提供的ViewPager是一个很好的工具,而一般我们使用Viewpager的时候,都会选择在底部有一排指示物指示当前显示的是哪一个page,下面我们就做这个功能的实现
    2013-11-11
  • Android中处理apple-touch-icon详解

    Android中处理apple-touch-icon详解

    这篇文章主要介绍了Android中处理apple-touch-icon详解,在Android如果想把网页放到桌面上,也需要使用Touch Icon图标才可实现,本文即讲解Android中处理Touch Icon的一些知识,需要的朋友可以参考下
    2015-01-01
  • 分析Android Activity的启动过程

    分析Android Activity的启动过程

    这篇文章主要介绍了分析Android Activity的启动过程的相关资料,需要的朋友可以参考下
    2017-07-07
  • Android实现腾讯新闻的新闻类别导航效果

    Android实现腾讯新闻的新闻类别导航效果

    这篇文章主要介绍了Android实现腾讯新闻的新闻类别导航效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-03-03
  • kotlin 官方学习教程之基础语法详解

    kotlin 官方学习教程之基础语法详解

    这篇文章主要介绍了kotlin 官方学习教程之基础语法详解的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android SeekBar在刷新使用中需要注意的问题

    Android SeekBar在刷新使用中需要注意的问题

    SeekBar在刷新使用中需要注意的问题:在使用SeekBar的过程中需要注意刷新频率,避免频繁刷新造成的性能问题;同时,需要对SeekBar的监听事件进行适当的优化,减少回调次数,提高响应速度
    2023-05-05
  • Android实现上传文件到服务器实例详解

    Android实现上传文件到服务器实例详解

    本篇文章详细介绍了Android实现上传文件到服务器实例详解,实现了文件每隔5秒进行上传,有需要的可以了解一下。
    2016-11-11
  • Android利用Intent读取和更新通讯录

    Android利用Intent读取和更新通讯录

    这篇文章主要介绍了Android利用Intent读取和更新通讯录的相关资料,通过用户配置文件(user profile)读取和更新该手机的所有联系人信息,需要的朋友可以参考下
    2016-06-06
  • 总结Android App内存优化之图片优化

    总结Android App内存优化之图片优化

    网上有很多大拿分享的关于Android性能优化的文章,主要是通过各种工具分析,使用合理的技巧优化APP的体验,提升APP的流畅度,但关于内存优化的文章很少有看到。下面是我在实践过程中使用的一些方法,很多都是不太成熟的项目,只是将其作为一种处理方式分享给大家。
    2016-08-08
  • Android Studio 常见问题及解决方法(推荐)

    Android Studio 常见问题及解决方法(推荐)

    这篇文章主要介绍了Android Studio 常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08

最新评论