服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android编程实现3D旋转效果实例

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

2021-05-06 15:28Healtheon Android

这篇文章主要介绍了Android编程实现3D旋转效果的方法,基于Android的Camera类实现坐标变换达到图片3D旋转效果,需要的朋友可以参考下

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

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

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

运行效果如下:

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

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

项目结构:

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

mainview.java中代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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中代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
<?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程序设计有所帮助。

延伸 · 阅读

精彩推荐