Android picture rotation, scaling, displacement, tilt, symmetry complete example (i)--imageview.setimagematrix (matrix) and matrix

Source: Internet
Author: User

Mainactivity as follows:
Import Android.os.bundle;import Android.view.motionevent;import Android.view.view;import Android.view.view.ontouchlistener;import Android.widget.imageview;import Android.app.activity;import android.graphics.matrix;/** * Demo Description: * Use Mimageview.setimagematrix (Matrix) to achieve * image translation, scaling, rotation, tilt and symmetry * * Reference: * 0 http://b log.csdn.net/pathuang68/article/details/6991988 * 1 http://blog.csdn.net/mingli198611/article/details/7830633 * * Thank you very much */public class Mainactivity extends Activity {private Testmatriximageview mtestmatriximageview; @Ov errideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); mtestmatriximageview= New Testmatriximageview (Mainactivity.this); Mtestmatriximageview.setscaletype (ImageView.ScaleType.MATRIX);//?? Mtestmatriximageview.setontouchlistener (New Touchlistenerimpl ()); Setcontentview (Mtestmatriximageview);} Private class Touchlistenerimpl implements ontouchlistener{@Overridepublic boolean OnTouch (View V, motionevent event) { if (EVEnt.getaction () ==motionevent.action_up) {//1 test pan testtranslate ();//2 test rotates//testrotate () around the center point of the picture;//3 test rotates around the origin and pans// Testrotateandtranslate ();//4 scale//testscale ()//5 horizontal Tilt//testskewx ()//6 vertical Tilt//testskewy ()//7 horizontal and vertical tilt//testskewxy () ;//8 horizontal symmetrical//testsymmetryx ();//9 vertical symmetry//testsymmetryy ();//10 about x=y symmetry//testsymmetryxy ();} return true;}} Translate private void Testtranslate () {Matrix matrix=new matrix (); int Width=mtestmatriximageview.getbitmap (). GetWidth (); int Height=mtestmatriximageview.getbitmap (). GetHeight (); matrix.posttranslate (width, height); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Rotates the private void testrotate () {Matrix matrix=new matrix (), int width=mtestmatriximageview.getbitmap (), around the center point of the picture. GetWidth (); int Height=mtestmatriximageview.getbitmap (). GetHeight (); Matrix.postrotate (45f, WIDTH/2, HEIGHT/2); Matrix.posttranslate (width, height); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Pan around the origin and//note the following three lines of code execution order://matrix.setrotate (45f);//matrix.pretranslAte (-width,-height);//matrix.posttranslate (width, height);//Perform matrix.pretranslate (-width,-height);// After execution matrix.setrotate (45f);//Perform matrix.posttranslate (width, height);p rivate void Testrotateandtranslate () {Matrix Matrix = new Matrix (), int width = Mtestmatriximageview.getbitmap (). getwidth (); int height = Mtestmatriximageview.getbitmap (). GetHeight (); matrix.setrotate (45f); Matrix.pretranslate (-width,-height); Matrix.posttranslate (width, height); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Scale private void Testscale () {Matrix matrix = new Matrix (); Matrix.setscale (0.5f, 0.5f); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Horizontal tilt private void testskewx () {Matrix matrix = new Matrix (); Matrix.setskew (0.5f, 0); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Vertical tilt private void Testskewy () {Matrix matrix = new Matrix (); Matrix.setskew (0, 0.5f); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);}Horizontal and vertical tilt private void Testskewxy () {Matrix matrix = new Matrix (); Matrix.setskew (0.5f, 0.5f); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Horizontal symmetry--Pictures about x axisymmetric private void Testsymmetryx () {Matrix matrix = new Matrix (); int height = Mtestmatriximageview.getbitmap () . GetHeight (); float matrixvalues[] = {1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues);//If Matrix.post Translate (0, height);//indicates that the picture is upside down matrix.posttranslate (0, height*2); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Vertical symmetry--picture about y axisymmetric private void Testsymmetryy () {Matrix matrix = new Matrix (); int Width=mtestmatriximageview.getbitmap (). GetWidth (); float matrixvalues[] = { -1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues);//If Matrix.posttra Nslate (width,0);//show the picture left and right upside down Matrix.posttranslate (width*2, 0); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} About X=y symmetry--Pictures about x=y axisymmetric private void Testsymmetryxy () {Matrix matrix = new MatRix (); int width = Mtestmatriximageview.getbitmap (). getwidth (); int height = Mtestmatriximageview.getbitmap (). GetHeight (); float matrixvalues[] = {0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues); Matrix.posttran Slate (Width+height, width+height); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Gets the transform matrix for each value in matrix private void Showmatrixeveryvalue (Matrix matrix) {float matrixvalues []=new float[9]; Matrix.getvalues (matrixvalues); for (int i = 0; I <3; i++) {String valuestring= "", for (int j = 0; J < 3; J + +) {ValueS Tring=matrixvalues[3*i+j]+ ""; System.out.println ("+ (i+1) +" column with the value of "+ (j+1) +" = "+valuestring");}}}

Testmatriximageview as follows:

Package Cn.testmatrix;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.matrix;import android.widget.imageview;/** * Summary: * executed in mainactivity: * Mtestmatriximageview.setimagematrix (Matrix); * This custom view will call Setimagematrix (Matrix matrix) * and then call OnDraw (Canvas canvas) */public class Testmatriximageview extends    Imageview{private Matrix Mmatrix; Private Bitmap Mbitmap;public Testmatriximageview (context context) {super (context); mmatrix=new Matrix (); mbitmap= Bitmapfactory.decoderesource (Getresources (), R.drawable.icon);} @Overrideprotected void OnDraw (canvas canvas) {SYSTEM.OUT.PRINTLN ("---> onDraw");//Painting the original Canvas.drawbitmap (Mbitmap, 0, 0, NULL);//Draw after Matrix changed figure Canvas.drawbitmap (Mbitmap, Mmatrix, null); Super.ondraw (canvas);} @Overridepublic void Setimagematrix (Matrix matrix) {SYSTEM.OUT.PRINTLN ("---> Setimagematrix"); This.mMatrix.set ( Matrix); Super.setimagematrix (matrix);} Public Bitmap GetbiTMap () {System.out.println ("---> getbitmap"); return mbitmap;}} 

Main.xml as follows:
<relativelayout 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 "    >    <textview        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android:text= "@string/hello_world"/></relativelayout>


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.