A case study of matrix usage in Android _android

Source: Internet
Author: User
Tags reflection

This example describes the matrix usage in Android. Share to everyone for your reference, specific as follows:

Matrix, in the Chinese called Matrices, advanced mathematics in the introduction, in the image processing, mainly for the plane scaling, translation, rotation and other operations.

First, we introduce the matrix operation. Addition and subtraction are needless to say, and the corresponding bits are added well. The main use of image processing is multiplication. Here's a formula for multiplication:

In Android, the matrix is made up of 9 float values and is a 3*3. The following figure:

To explain, the above Sinx and COSX, which represent the Cos value and the sin value of the rotation angle, note that the rotation angle is calculated in the clockwise direction. Translatex and Translatey represent the amount of translation for x and Y. Scale is the scaling ratio, 1 is invariant, and 2 is the zoom 1/2.

Matrix operations, a total of translate (translation), rotate (rotation), scale (scaling) and skew (tilt) four, each of the changes in the Android API provides set, post and pre three modes of operation, In addition to translate, all three other operations can specify a central point.

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

The post is a multiplier, and the current matrix is multiplied by the matrix given by the parameter. You can use post for many consecutive times to complete the entire transformation you want.  For example, to rotate a picture 30 degrees and then pan to (100,100), you can do this: matrix M = new Matrix ();  M.postrotate (30); M.posttranslate (100, 100); This will achieve the desired effect.

The pre is a forward multiplication, and the matrix given by the argument is multiplied by the current matrix. So the operation takes place at the very front of the current matrix. For example, if you use the pre, it's like this: matrix M = new Matrix ();  M.settranslate (100, 100); M.prerotate (30);

Rotation, scaling, and skewing can be done around a central point, which, if not specified, is done around the (0,0) point by default.

Special attention:

Matrix operations, a total of translate (translation), rotate (rotation), scale (scaling) and skew (tilt) four, each of the changes in the Android API provides set, post and pre three modes of operation, In addition to translate, all three other operations can specify a central point.

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

The post is a multiplier, and the current matrix is multiplied by the matrix given by the parameter. You can use post for many consecutive times to complete the entire transformation you want.

Mirror effect:

Reflection effect:

Image synthesis (watermark):

Not much to say, directly on the code.

The main code in the Mainactivity.java is as follows:

Package Net.loonggg.testmatrix;
Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Matrix;
Import Android.graphics.Paint;
Import Android.graphics.PorterDuff.Mode;
Import Android.graphics.PorterDuffXfermode;
Import Android.os.Bundle;
Import Android.view.Window;
Import Android.widget.ImageView;
public class Mainactivity extends activity {private ImageView iv1, Iv2;
Private Canvas Canvas;
Private Paint Paint;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
Iv1 = (ImageView) Findviewbyid (R.ID.IV1);
Iv2 = (ImageView) Findviewbyid (R.ID.IV2);
Bitmap Bitmap1 = Bitmapfactory.decoderesource (Getresources (), R.drawable.weibo); Bitmap Updatebitmap = Bitmap.CreateBitmap (Bitmap1.getwidth () * 2, Bitmap1.getheight () * 2, Bitmap1.getconfig ());
Canvas = new canvas (UPDATEBITMAP);
Paint = new paint ();
Paint.setcolor (Color.Black);
Matrix matrix = new Matrix ();
Setmirrorone (Bitmap1, Matrix);
Setinvertedimage (Bitmap1, Matrix);
Setbasechange (matrix);
Canvas.drawbitmap (Bitmap1, Matrix, paint);
Setimagesynthesis (matrix);
Iv1.setimagebitmap (BITMAP1);
Iv2.setimagebitmap (UPDATEBITMAP);
/** * There are some basic changes * * * private void Setbasechange (Matrix matrix) {//Matrix.setrotate (60);//This is how many degrees of rotation Matrix.setrotate (degrees, px, py);//This method is how many degrees/Matrix.setskew (KX, KY) is rotated at which point, and/or tilt to x-axis or y-axis//skew X and Y
Axis, with (100,100) as the center.
Matrix.postskew (0 2f, 0 2f, 100, 100);
Matrix.setscale (0.5f, 1);//Shrink relaxation to the original half, height invariant}/** * Set reflection effect * * @param bitmap1 * @param matrix * private void Setinvertedimage (Bitmap bitmap1, Matrix Matrix) {Matrix.setscale (1,-1);
Matrix.posttranslate (0, Bitmap1.getheight ()); /** * Set Specular effect Method A * * @param bitmap1 * @param matrix/private void Setmirrorone (Bitmap bitmap1
, matrix matrix) {Matrix.settranslate (Bitmap1.getwidth (), 0);//This is the mobile Matrix.prescale (-1, 1); }//---------------------------------------------------------/** * Explanation: Mirror effect method The difference between one and two: * I do not know if you see it, in fact, the effectiveness of these two methods The fruit is the same, except that the set step is not the same, the post is the multiplier, the current matrix times the parameter given by the matrix *. You can use post for many consecutive times to complete the entire transformation you want. The pre is a forward multiplication, and the matrix given by the argument is multiplied by the current matrix.
So the operation takes place at the very front of the current matrix. * You can use the post * for several consecutive times to complete the desired transformation, but you cannot use set to complete the entire transformation of the matrix. Set is directly set the value of the matrix, each time set *, the entire matrix array will be changed, the first time you can use set, after the transformation must be replaced by post or pre, you can always use the Post also line//------------------ ---------------------------------------/** * Set Specular Effect Method II * * @param BITMAP1 * @param Matrix * * PR
ivate void Setmirrortwo (Bitmap bitmap1, Matrix matrix) {Matrix.setscale (-1, 1); Matrix.posttranslate (Bitmap1.getwidth (), 0);
/** * Set Picture synthesis * * @param matrix/private void Setimagesynthesis (Matrix matrix) {Bitmap
BITMAP2 = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
Sets the various modes Paint.setxfermode (new Porterduffxfermode (Mode.darken)) when the picture is synthesized;
The synthesis of the picture is very simple, that is, to BITMAP2 as the base map to the target picture once Canvas.drawbitmap (Bitmap2, Matrix, paint);

}
}

The code for the layout file is as follows:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:orientation= "vertical"
tools:context= ". Mainactivity ">
<imageview
android:id=" @+id/iv1 "
android:layout_width=" Wrap_content " android:layout_height= "Wrap_content"/>
<imageview
android:id= "@+id/iv2"
android:layout_ Width= "Wrap_content"
android:layout_height= "wrap_content"/>
</LinearLayout>

Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem 11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off，Oct.24-Nov.13 (UTC+8)

Get It Now > Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD