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

項(xià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)時(shí)的中心點(diǎn)坐標(biāo)
private int centerX, centerY;
//轉(zhuǎn)動(dòng)的總距離,跟度數(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();
//以圖片的中心點(diǎn)為旋轉(zhuǎn)中心,如果不加這兩句,就是以(0,0)點(diǎn)為旋轉(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>
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android 3D旋轉(zhuǎn)動(dòng)畫(huà)效果實(shí)現(xiàn)分解
- Android高級(jí)圖片滾動(dòng)控件實(shí)現(xiàn)3D版圖片輪播器
- Android酷炫動(dòng)畫(huà)效果之3D星體旋轉(zhuǎn)效果
- Android動(dòng)畫(huà)之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android開(kāi)發(fā) OpenGL ES繪制3D 圖形實(shí)例詳解
- 安卓(Android)實(shí)現(xiàn)3DTouch效果
- Android 使用Gallery實(shí)現(xiàn)3D相冊(cè)(附效果圖+Demo源碼)
- Android編程實(shí)現(xiàn)3D立體旋轉(zhuǎn)效果的實(shí)例代碼
相關(guān)文章
實(shí)現(xiàn)輪轉(zhuǎn)廣告帶底部指示的自定義ViewPager控件
在項(xiàng)目中經(jīng)常需要使用輪轉(zhuǎn)廣告的效果,在android-v4版本中提供的ViewPager是一個(gè)很好的工具,而一般我們使用Viewpager的時(shí)候,都會(huì)選擇在底部有一排指示物指示當(dāng)前顯示的是哪一個(gè)page,下面我們就做這個(gè)功能的實(shí)現(xiàn)2013-11-11
分析Android Activity的啟動(dòng)過(guò)程
這篇文章主要介紹了分析Android Activity的啟動(dòng)過(guò)程的相關(guān)資料,需要的朋友可以參考下2017-07-07
Android實(shí)現(xiàn)騰訊新聞的新聞?lì)悇e導(dǎo)航效果
這篇文章主要介紹了Android實(shí)現(xiàn)騰訊新聞的新聞?lì)悇e導(dǎo)航效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
kotlin 官方學(xué)習(xí)教程之基礎(chǔ)語(yǔ)法詳解
這篇文章主要介紹了kotlin 官方學(xué)習(xí)教程之基礎(chǔ)語(yǔ)法詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android SeekBar在刷新使用中需要注意的問(wèn)題
SeekBar在刷新使用中需要注意的問(wèn)題:在使用SeekBar的過(guò)程中需要注意刷新頻率,避免頻繁刷新造成的性能問(wèn)題;同時(shí),需要對(duì)SeekBar的監(jiān)聽(tīng)事件進(jìn)行適當(dāng)?shù)膬?yōu)化,減少回調(diào)次數(shù),提高響應(yīng)速度2023-05-05
Android實(shí)現(xiàn)上傳文件到服務(wù)器實(shí)例詳解
本篇文章詳細(xì)介紹了Android實(shí)現(xiàn)上傳文件到服務(wù)器實(shí)例詳解,實(shí)現(xiàn)了文件每隔5秒進(jìn)行上傳,有需要的可以了解一下。2016-11-11
總結(jié)Android App內(nèi)存優(yōu)化之圖片優(yōu)化
網(wǎng)上有很多大拿分享的關(guān)于Android性能優(yōu)化的文章,主要是通過(guò)各種工具分析,使用合理的技巧優(yōu)化APP的體驗(yàn),提升APP的流暢度,但關(guān)于內(nèi)存優(yōu)化的文章很少有看到。下面是我在實(shí)踐過(guò)程中使用的一些方法,很多都是不太成熟的項(xiàng)目,只是將其作為一種處理方式分享給大家。2016-08-08
Android Studio 常見(jiàn)問(wèn)題及解決方法(推薦)
這篇文章主要介紹了Android Studio 常見(jiàn)問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08

