Android picture rotation, scaling, displacement, tilt, Symmetry complete demo sample (i)--imageview.setimagematrix (matrix) and matrix

Source: Internet
Author: User

Mainactivity such as the following:
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: * using Mimageview.setimagematrix (Matrix) to achieve * image translation, scaling, rotation, tilt and symmetry * * Reference information: * 0 http:/ /blog.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 surround Image Center point rotate//testrotate ();//3 Test the rotation of the original point after the translation//testrotateandtranslate (),//4 zoom//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 symmetric//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);} Around the center point of the picture rotates the private void testrotate () {Matrix matrix=new matrix (); int Width=mtestmatriximageview.getbitmap (). getwidth (); int Height=mtestmatriximageview.getbitmap (). GethEight (); Matrix.postrotate (45f, WIDTH/2, HEIGHT/2); matrix.posttranslate (width, height); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Pan after rotation of the origin point//Note the following three lines of code run in order://matrix.setrotate (45f);//matrix.pretranslate (-width,-height);// Matrix.posttranslate (width, height);//First run matrix.pretranslate (-width,-height);//After running Matrix.setrotate (45f);// Run matrix.posttranslate (width, height);p rivate void Testrotateandtranslate () {Matrix matrix = new Matrix (); int width = MT Estmatriximageview.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.posttranslate (width,0),//means the picture is inverted 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.getbit Map (). getwidth (); int height = Mtestmatriximageview.getbitmap (). GetHeight (); float matrixvalues[] = {0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues), Matrix.posttranslate (Width+height, width+height); Mtestmatriximageview.setimagematrix (matrix); Showmatrixeveryvalue (matrix);} Gets the transform matrix each value in the 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 such as the following:

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: * run in mainactivity: * Mtestmatriximageview.setimagematrix (Matrix); * This own definition 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 such as the following:
<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>


Android picture rotation, scaling, displacement, tilt, Symmetry complete demo sample (i)--imageview.setimagematrix (matrix) and matrix

Related Article

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.