Use of the android Matrix tool

Source: Internet
Author: User

The Matrix tool class is used to process special effects on images.

Matrix is a Matrix tool class that does not transform graphs and can be used with other APIs.

Obtain the Matrix object. You can create it directly and obtain it from other encapsulated Matrix classes. Transformation encapsulates the Matrix object.

You can call the Matrix object method to translate, zoom, rotate, and tilt a graphic image.

You need to apply the transformation performed by the Program on the Matrix to the specified image or component.

The following is an example of how to use a Matrix, using buttons to control Bitmap Skew and scaling.

Class MyView extends View {/** source map */private Bitmap bitmap;/** Matrix object */private Matrix matrix = new Matrix (); /** skew */public float ox = 0.0f;/** Zoom */public float scale = 1.0f;/** source image size */private int width, height; /** zoom or tilt */private boolean isScale = false; public MyView (Context context, AttributeSet attrs) {super (context, attrs ); // obtain bitmap = (BitmapDrawable) this. getResources (). getDrawable (R. drawable. ic_launcher )). getBitmap (); width = bitmap. getWidth (); height = bitmap. getHeight (); // key control, first obtain the focus this. setFocusable (true) ;}@ SuppressLint ("DrawAllocation") @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // resets the Matrix, restores it to the source image when it is skewed, and then scales the matrix. reset (); if (isScale) {// x/y axis yoy zoom in and out matrix. setScale (scale, scale);} else {matrix. setSkew (ox, ox);} // obtain the new map Bitmap B = Bitmap. createBitmap (bitmap, 0, 0, width, height, matrix, true); // applies the transformation performed by the Program on the Matrix to the canvas on the specified image or component. drawBitmap (B, matrix, null) ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent. KEYCODE_DPAD_LEFT: // tilt isScale = false; ox + = 0.1; // refresh the interface. The view class also uses this method postInvalidate (); break; case KeyEvent. KEYCODE_DPAD_RIGHT: // skewed isScale = false; ox-= 0.1; postInvalidate (); break; case KeyEvent. KEYCODE_DPAD_UP: // enlarge isScale = true; if (scale <2.0) {scale + = 0.1;} postInvalidate (); break; case KeyEvent. KEYCODE_DPAD_DOWN: // reduce isScale = true; if (scale> 0.5) {scale-= 0.1;} postInvalidate (); break; default: break;} return super. onKeyDown (keyCode, event );}}



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.