Android編程實現(xiàn)3D旋轉(zhuǎn)效果實例
本文實例講述了Android編程實現(xiàn)3D旋轉(zhuǎn)效果的方法。分享給大家供大家參考,具體如下:
下面的示例是在Android中實現(xiàn)圖片3D旋轉(zhuǎn)的效果。
實現(xiàn)3D效果一般使用OpenGL,但在Android平臺下可以不直接使用OpenGL,而是使用Camera實現(xiàn),Camera中原理最終還是使用OpenGL,不過使用Camera比較方便。 Camera類似一個攝像機,當(dāng)物體不動時,我們帶著攝像機四處移動,在攝像機里面的畫面就會有立體感,就可以從其它的角度觀看這個物體。廢話不多說,直接看示例。
運行效果如下:

項目結(jié)構(gòu):

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;
//圖片旋轉(zhuǎn)時的中心點坐標(biāo)
private int centerX, centerY;
//轉(zhuǎn)動的總距離,跟度數(shù)比例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();
//以圖片的中心點為旋轉(zhuǎn)中心,如果不加這兩句,就是以(0,0)點為旋轉(zhuǎn)中心
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程序設(shè)計有所幫助。
相關(guān)文章
Android實現(xiàn)支持所有View的通用的下拉刷新控件
這篇文章主要介紹了Android實現(xiàn)支持所有View的通用的下拉刷新控件的相關(guān)資料,需要的朋友可以參考下2016-06-06
Android?Material組件庫日期選擇和時間選擇器的使用方法
這篇文章主要介紹了Android?Material組件庫(日期選擇和時間選擇器)基本使用,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11
Android編程實現(xiàn)通知欄進度條效果的方法示例
這篇文章主要介紹了Android編程實現(xiàn)通知欄進度條效果的方法,結(jié)合實例形式較為詳細(xì)的分析了Android通知欄進度條效果的功能、布局相關(guān)實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2018-02-02
Android 頂部標(biāo)題欄隨滑動時的漸變隱藏和漸變顯示效果
這篇文章主要介紹了Android 頂部標(biāo)題欄隨滑動時的漸變隱藏和漸變顯示效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
Flutter調(diào)用Android和iOS原生代碼的方法示例
這篇文章主要給大家介紹了關(guān)于Flutter調(diào)用Android和iOS原生代碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android中的RecyclerView新組件初步上手指南
RecyclerView是Android L版本開始采用的一個組件,被人們認(rèn)為用來代替?zhèn)鹘y(tǒng)的ListView,下面我們就一起來看一下Android中的RecyclerView新組件初步上手指南2016-06-06

