Android. graphics. Camera achieves simple 3D Effects

Source: Internet
Author: User


In fact, this code was found at the time of learning, but it was too early to forget where it was.

Add some comments in it by the way. It is very clear to look at the control code, a variety of combinations of Very dazzling results

/*** Comparison between Camera and Matrix:
* The rotate () method of Camera is to specify the Rotation Angle of a certain dimension.
* The rotate () method of Matrix achieves the specified Rotation Angle clockwise. The rotation effect is the same as that of the specified Z axis of Camera, but the opposite direction is true.
** The translate () method of Camera scales the Image Based on the displacement of the viewpoint in a certain dimension, which is similar to the scale () method of Matrix. * It is only the scale () method of Matrix () you can directly specify the scaling ratio.
* Camera does not support tilting operations. Matrix can directly perform tilting operations.
** @ Author Sodino E-mail: sodinoopen@hotmail.com * @ version Time: 04:17:49 */public class MainActivity extends Activity implements OnSeekBarChangeListener {private Camera camera; // views private SeekBar seekbarXRotate; private SeekBar seekbarYRotate; private SeekBar seekbarzrtate; private TextView txtXRotate; private TextView txtYRotate; private TextView txtzrtate; private SeekBar se EkbarXSkew; private SeekBar values; private TextView txtXTranslate; private TextView txtYTranslate; private TextView txtZTranslate; private ImageView imgResult; // integer params private int rotateX, rotateY, rotateZ; private float skewX, skewY; private int translateZ; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceS Tate); setContentView (R. layout. activity_main); // camera = new Camera (); // rotate seekbarXRotate = (SeekBar) findViewById (R. id. seekbarXRotate); seekbarXRotate. setOnSeekBarChangeListener (this); seekbarYRotate = (SeekBar) findViewById (R. id. seekbarYRotate); seekbarYRotate. setOnSeekBarChangeListener (this); seekbarzrtate = (SeekBar) findViewById (R. id. seekbarrotate); seekbarrotate. setOnSeekBa RChangeListener (this); txtXRotate = (TextView) returns XRotate); txtYRotate = (TextView) returns YRotate); txtzrtate = (TextView) returns zrtate); // translate returns = (SeekBar) findViewById (R. id. seekbarXSkew); seekbarXSkew. setOnSeekBarChangeListener (this); seekbarYSkew = (SeekBar) findViewById (R. id. seekbarYSkew); seekbarYSkew. setOnSeekBarChangeListe Ner (this); seekbarZTranslate = (SeekBar) findViewById (R. id. seekbarZTranslate); seekbarZTranslate. setOnSeekBarChangeListener (this); txtXTranslate = (TextView) Export XSkew); txtYTranslate = (TextView) findViewById(R.id.txt YSkew); txtZTranslate = (TextView) Export ZTranslate); imgResult = (ImageView) findViewById (R. id. imgResult); System. out. println ("33333333333333333333 "); // Refresh refreshImage (); // The post pre set actually represents the order of method transformations in the Matrix. pre is executed in the forward queue and post is added to the queue. // For example: // matrix. preScale (2f, 1f); matrix. preTranslate (5f, 0f); matrix. postScale (0.2f, 1f); matrix. postTranslate (0.5f, 0f); // The method execution sequence is: // preTranslate (5, 0)-> preScale (2f, 1f)-> postScale (0.2f, 1f) -> postTranslate (0.5f, 0f) // while the set Method of matrix clears the previous pre and post operations, and then sets its value, such as the following method calls: // matrix. postTranslate (2f, 0f); matrix. preScale (0.2f, 1f); matrix. setScale (1f, 1f); matrix. postScale (5f, 1f); matri X. preTranslate (0.5f, 0f); The method execution sequence is: // translate (0.5f, 0f)-> scale (1f, 1f)-> scale (5f, 1 ). // The scale, translate, rotate, and concat methods in the Canvas are pre methods. If you want to perform more transformations, You can first obtain the matrix from the canvas, and then set it back to the canvas .} private void refreshImage () {// obtain the image to be processed BitmapDrawable tmpBitDra = (BitmapDrawable) getResources (). getDrawable (R. drawable. ic_launcher); Bitmap tmpBit = tmpBitDra. getBitmap (); // start processing the image // 1. obtain the processing matrix // record the initial status. Save () and restore () can soften the image transition. // Each save shocould be balanced with a call to restore (). camera. save (); Matrix matrix = new Matrix (); // rotate camera. rotateX (rotateX); camera. rotateY (rotateY); camera. rotateZ (rotateZ); // translate camera. translate (0, 0, translateZ); camera. getMatrix (matrix); // restore to the initial state. Camera. restore (); // set the central point matrix for image processing. preTranslate (tmpBit. getWidth ()> 1, tmpBit. getHeight ()> 1); matrix. preSkew (skewX, skewY); // setScale (float sx, float sy, float px, float py) Enlarge // setSkew (float kx, float ky, float px, float py) diagonal cut // setTranslate (float dx, float dy) Pan // setRotate (float degrees, float px, float py) rotate // matrix. postSkew (skewX, skewY); // setSkew () directly, the rotate (), transl And so on. // Matrix. setSkew (skewX, skewY); // 2. generate a new image (or act directly on the Canvas) Log through the matrix. d ("ANDROID_LAB", "width =" + tmpBit. getWidth () + "height =" + tmpBit. getHeight (); Bitmap newBit = null; try {// the width and height of the converted matrix image may not be greater than 0. In this case, IllegalArgumentException newBit = Bitmap is thrown. createBitmap (tmpBit, 0, 0, tmpBit. getWidth (), tmpBit. getHeight (), matrix, true);} catch (IllegalArgumentException iae) {iae. printStackTrace ();} if (newBi T! = Null) {imgResult. setImageBitmap (newBit) ;}@ Override public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {if (seekBar = seekbarXRotate) {txtXRotate. setText (progress + "progress"); rotateX = progress;} else if (seekBar = seekbarYRotate) {txtYRotate. setText (progress + "progress"); rotateY = progress;} else if (seekBar = seekbarzrtate) {txtzrtate. setText (progress + "progress"); rotateZ = progress;} else if (seekBar = seekbarXSkew) {skewX = (progress-100) * 1.0f/100; txtXTranslate. setText (String. valueOf (skewX);} else if (seekBar = seekbarYSkew) {skewY = (progress-100) * 1.0f/100; txtYTranslate. setText (String. valueOf (skewY);} else if (seekBar = seekbarZTranslate) {translateZ = progress-100; txtZTranslate. setText (String. valueOf (translateZ);} refreshImage () ;}@ Override public void onStartTrackingTouch (SeekBar seekBar) {}@ Override public void onStopTrackingTouch (SeekBar seekBar ){}}

Project address:

Http://download.csdn.net/detail/kongbaidepao/6885105

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.