Android--matrix (b)

Source: Internet
Author: User

The processing of a picture transform requires the support of the Matrix class, which is located under the "Android.graphics.Matrix" package, which is a matrix tool class provided by Android, which itself cannot transform an image or view, but it can be combined with other APIs to control graphics, View transformations, such as Canvas.

It's a little hard to understand that it involves advanced mathematics. Fortunately Android provides a series of convenient methods for the matrix operation: Each of the transformations in the Android API provides set, post and pre three modes of operation

    • settranslate (Float dx,float dy): Controls the Matrix for displacement.
    • Setskew (Float kx,float ky):KX, KY is the ratio in x, Y direction.
    • Setskew (Float kx,float ky,float px,float py): Control matrix is tilted with px and py as axis, and KX and KY are inclined proportions in X and y direction.
    • SetRotate (float degrees):depress angle , axis (0,0).
    • setRotate (Float degrees,float px,float py): Controls the matrix for depress angle rotation with the axis (px,py).
    • Setscale (float sx,float sy):SX, SY is the scale in X, y direction .
    • Setscale (Float sx,float sy,float px,float py): Sets the Matrix to be scaled (px,py) to the axis, SX, SY for the X-, y-direction scale.
    • Postrotate (float degrees)
    • Postrotate (float degrees, float px, float py)

The matrix operates in a total of scale (scaling), rotate (rotation), translate (panning) and skew (tilt) four, andin addition to the translate, the other three operations can specify the center point . If not specified, the default is around the (0,0) point.

  

The following are scale (scaling), rotation (rotation) and translation (panning) in the matrix representation of Android:

        

Set is the value of setting the matrix directly, each time set, the entire matrix array will be changed.

Post is a post-multiply (provided data on the left). For example, to rotate a picture 30 degrees and then pan to (100,100) , you can do this:

New Matrix (); M.postrotate(+); M.posttranslate(100, 100);  

The pre is the pre-multiply (provided data on the right). For example above, if you use the Pre

New Matrix (); M.settranslate(+),M.prerotate(30);

 /*** Zoom Picture*/    protected voidBitmapscale (floatXfloaty) {//because you want to enlarge the picture, to recreate the bitmap according to the enlarged size,config is an enumeration that you can use to refer to the location graph "quality". Bitmap Afterbitmap =Bitmap.createbitmap ((int) (Basebitmap.getwidth () *x), (int) (Basebitmap.getheight () *y), basebitmap.getconfig ()); Canvas Canvas=NewCanvas (AFTERBITMAP); Matrix Matrix=NewMatrix ();        Matrix.setscale (x, y); //draw the picture to the canvas based on the zoom ratioCanvas.drawbitmap (Basebitmap, Matrix, paint);    Iv_after.setimagebitmap (AFTERBITMAP); }    /*** Tilt Picture*/    protected voidBitmapskew (floatDxfloatdy) {        //calculates the size of the transformed image according to the image's tilt ratio,Bitmap Afterbitmap =Bitmap.createbitmap (basebitmap.getwidth ()+ (int) (Basebitmap.getwidth () *dx), basebitmap.getheight ()+ (int) (Basebitmap.getheight () *dy), basebitmap.getconfig ()); Canvas Canvas=NewCanvas (AFTERBITMAP); Matrix Matrix=NewMatrix (); //set the scale of the picture tiltmatrix.setskew (dx, dy);        Canvas.drawbitmap (Basebitmap, Matrix, paint);    Iv_after.setimagebitmap (AFTERBITMAP); }    /*** Picture Movement*/    protected voidBitmaptranslate (floatDxfloatdy) {        //you need to create a copy plot size for a picture based on the distance you moveBitmap Afterbitmap =Bitmap.createbitmap ((int) (Basebitmap.getwidth () +dx), (int) (Basebitmap.getheight () +dy), basebitmap.getconfig ()); Canvas Canvas=NewCanvas (AFTERBITMAP); Matrix Matrix=NewMatrix (); //set the distance to movematrix.settranslate (dx, dy);        Canvas.drawbitmap (Basebitmap, Matrix, paint);    Iv_after.setimagebitmap (AFTERBITMAP); }    /*** Picture Rotation*/    protected voidBitmaprotate (floatdegrees) {        //Create a picture of the same size as the originalBitmap Afterbitmap =Bitmap.createbitmap (Basebitmap.getwidth (), Basebitmap.getheight (), BAS        Ebitmap.getconfig ()); Canvas Canvas=NewCanvas (AFTERBITMAP); Matrix Matrix=NewMatrix (); //rotates according to the center position of the original imageMatrix.setrotate (degrees, Basebitmap.getwidth ()/2,basebitmap.getheight ()/2);        Canvas.drawbitmap (Basebitmap, Matrix, paint);    Iv_after.setimagebitmap (AFTERBITMAP); }

Matrix Transformation Considerations

    • For one from Bitmapfactory. decodexxx () method to load a bitmap object, it is a read-only and cannot be processed. A copy of the bitmap object must be recreated using the Bitmap.createbitmap () method before the copy's bitmap can be processed.
    • Because the transformation of the image is for each pixel, some transformations may be lost in pixels, where the Paint.setanitialias (Boolean) setting is used to eliminate aliasing , so that the effect of the image transformation is much better.
    • When recreating a copy of a bitmap object, pay attention to its wide height and, if set incorrectly, it is likely that the transformed pixel has moved beyond the picture.
    • If the direct use of 100/bmp.getwidth (), will get 0, because it is the integer division, so it must be one of the float type, directly with 100f is good.

Android--matrix (b)

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.