Android camera achieves 3D Effects

Source: Internet
Author: User

The image is displayed using imageview.

Below 50 is actually a text box

Deformation: click the button to rotate the image to the specified angle in the text box along the Y axis.

 

ImplementedCodeSimple:

Public class base3dimp extends activity {
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. base_3d_imp );

This. findviewbyid (R. Id. btn_changeshape). setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
Edittext tvgress = (edittext) base3dimp. This. findviewbyid (R. Id. et_degress );
Int degress = integer. parseint (tvgress. gettext (). tostring (). Trim ());
If (degress % 90 = 0)
Degress + = 1;
Bitmap BMP = bitmapfactory. decoderesource (getresources (), R. drawable. P1 );

Matrix matrix = new matrix (); system. Out. println ("martrix:" + matrix. tostring ());
Camera camera = new camera ();
Camera. Save ();
// Camera. rotatey (degress );
// Camera. rotatex (degress );
Camera. getmatrix (matrix );
Camera. Restore (); system. Out. println ("martrix1:" + matrix. tostring ());

Imageview IMG = (imageview) base3dimp. This. findviewbyid (R. Id. iv_base_3d );
Int centerx = IMG. getwidth ()/2;
Int centery = IMG. getheight ()/2;

Bitmap BMP 1 = bitmap. createbitmap (BMP, 0, 0, BMP. getwidth (), BMP. getheight (), matrix, true );
System. Out. println ("martrix1:" + matrix. tostring ());

IMG. setimagebitmap (BMP 1 );

}
});

}
}

 

 

The above code is used to observe the implementation process of the rotation of your rotatey (degress) method. The following shows the rotation Animation:

First:

The openglactivity code is as follows:

Package com. example. OpenGL;

Import Android. App. activity;
Import Android. content. intent;
Import Android. OpenGL. glsurfaceview;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. View. window;
Import Android. View. animation. accelerateinterpolator;
Import Android. widget. imageview;

Import com. example. OpenGL. animation. rotate3danimation;
Import com. example. OpenGL. Render. cylinderrender;
Import com. example. OpenGL. Render. haxagonrender;
Import com. example. OpenGL. Render. rotatetriangle;
/**
* OpenGL exercises
* @ Author yangbaobao
*
*/
Public class openglactivity extends activity {

Private view V;
Private imageview IMG;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
// Glsurfaceview surface = new glsurfaceview (this );
// Surface. requestfocus (); // obtain the focus
// Surface. setfocusableintouchmode (true); // set to touch
// Trigle (surface );
// Sixshape (surface );
// Clindershape (surface );
Setcontentview (R. layout. activity_open_gl );
V = This. findviewbyid (R. Id. rl_main );
IMG = (imageview) This. findviewbyid (R. Id. img_animation );
This. findviewbyid (R. Id. btn_startanimation). setonclicklistener (New onclicklistener (){

@ Override
Public void onclick (view v ){
Applyrotation (0, 0,360 );
//
// Intent I = new intent (openglactivity. This, transition3d. Class );
// Openglactivity. This. startactivity (I );
}
});

// View v = NULL; V. Set

}
Private void trigle (glsurfaceview surface)
{
Rotatetriangle rtgl = new rotatetriangle ();
Surface. setontouchlistener (New rotatetriangle. mytouchlistener (rtgl ));
Surface. setrenderer (rtgl );
}
Private void sixshape (glsurfaceview surface)
{
Haxagonrender hR = new haxagonrender ();
Surface. setontouchlistener (New haxagonrender. mytouchlistener (HR, surface ));
Surface. setrenderer (HR );
}
Private void clindershape (glsurfaceview surface)
{
Cylinderrender hR = new cylinderrender ();
Surface. setontouchlistener (New cylinderrender. mytouchlistener (HR, surface ));
Surface. setrenderer (HR );
}
// Public void staticanimation ()
//{
// Rotate3danimation Ra = new rotate3danimation (0,180, IMG. getx ()/2, IMG. Gety ()/2,310, false );
// IMG. startanimation (RA );
//}
Private void applyrotation (INT position, float start, float end ){
// Find the center of the container
Final float centerx = IMG. getwidth ()/2.0f;
Final float centery = IMG. getheight ()/2.0f;

// Create a new 3D rotation with the supplied Parameter
// The animation listener is used to trigger the next Animation
Final rotate3danimation rotation =
New rotate3danimation (START, end, centerx, centery, 0, true );
Rotation. setduration (5000 );
Rotation. setfillafter (true );
Rotation. setinterpolator (New accelerateinterpolator ());
// Rotation. setanimationlistener (New displaynextview (position ));

IMG. startanimation (rotation );
}
}

 

Rotate3danimation is an animation class, and it is also very simple:

First:

Package com. example. OpenGL. animation;

Import Android. View. animation. animation;
Import Android. View. animation. transformation;
Import Android. Graphics. camera;
Import Android. Graphics. matrix;

Public class rotate3danimation extends animation {
Private Final float mfromdegrees;
Private Final float mtodegrees;
Private Final float mcenterx;
Private Final float mcentery;
Private Final float mdepthz;
Private Final Boolean mreverse;
Private camera mcamera;

Public rotate3danimation (float fromdegrees, float todegrees,
Float centerx, float centery, float depthz, Boolean reverse ){
Mfromdegrees = fromdegrees;
Mtodegrees = todegrees;
Mcenterx = centerx;
Mcentery = centery;
Mdepthz = depthz;
Mreverse = reverse;
}

@ Override
Public void initialize (INT width, int height, int parentwidth, int parentheight ){
Super. initialize (width, height, parentwidth, parentheight );
Mcamera = new camera ();
}

/**

Animation implementation methods

*/

@ Override
Protected void applytransformation (float interpolatedtime, transformation T ){
Final float fromdegrees = mfromdegrees;
Float degrees = fromdegrees + (mtodegrees-fromdegrees) * interpolatedtime );
System. Out. println ("degress:" + degrees );

System. Out. println ("interpolatedtime:" + (mtodegrees-fromdegrees) * interpolatedtime ));

Final float centerx = mcenterx;
Final float centery = mcentery;
Final camera = mcamera;

Final matrix = T. getmatrix ();

Camera. Save ();
If (mreverse ){
Camera. Translate (0.0f, 0.0f, mdepthz * interpolatedtime); // mdepthz * interpolatedtime + 100
} Else {
Camera. Translate (0.0f, 0.0f, mdepthz * (1.0f-interpolatedtime); // mdepthz * (1.0f-interpolatedtime) + 100
}
Camera. rotatey (degrees );
Camera. getmatrix (matrix );
Camera. Restore ();

matrix. pretranslate (-centerx,-centery);
matrix. posttranslate (centerx, centery);
}< BR >}

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.