Android 3D Rotation

Source: Internet
Author: User

The following example shows how to rotate Images 3D in Android.

 

OpenGL is generally used to implement 3D effects, but OpenGL can be used not directly on the Android platform, but on Camera. The original principle of Camera is OpenGL, but it is more convenient to use Camera. Camera is similar to a video Camera. When an object does not move, we move around with the Camera, and the picture inside the Camera will have a stereoscopic effect, so that we can watch the object from other angles. Let's just look at the example.

 

The running effect is as follows:

 

Project Structure:

 

Code in 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 class
Private Camera mCamera;

Private Bitmap face;
Private Matrix mMatrix = new Matrix ();
Private Paint mPaint = new Paint ();

Private int mLastMotionX, mLastMotionY;

// Coordinates of the center point during image rotation
Private int centerX, centerY;
// The total rotation distance, with a ratio of to the degree
Private int deltaX, deltaY;
// Image width and height
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 ();

// Take the center of the image as the center of rotation. If the two sentences are not added, the image is centered at (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 );
}

}

 

Code in 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>


Finally, I hope to reprint friends can respect the author's labor results, plus reprint address: http://www.cnblogs.com/hanyonglu/archive/2012/02/12/2347636.html thank you.

 

Complete. Pai_^

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.