Android picture rotation, scaling, displacement, tilt, symmetry complete example (ii)--bitmap.createbitmap () and matrix

Source: Internet
Author: User

Mainactivity as follows:
Package Cc.c;import Android.app.activity;import Android.graphics.bitmap;import android.graphics.matrix;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import android.widget.imageview;/** * Demo Description: * Use Bitmap.createbitmap () and matrix to achieve the rotation, scaling, displacement, and tilt of the image *. * The role of the method and Mimageview.setimagematrix (matrix); * It's the same * * NOTE: * The use of Bitmap.createbitmap () and matrix method does not implement the image * about the x-axis, y-axis and xy symmetry. * Understand how to achieve the coder please advise, thank you. * */public class Mainactivity extends Activity {private Bitmap mbitmap;private Bitmap mnewbitmap;private ImageView Mimagev    Iew;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main);    Init ();    } private void Init () {mimageview= (ImageView) Findviewbyid (R.id.imageview);         Mimageview.setscaletype (ImageView.ScaleType.MATRIX);    Bitmapdrawable bitmapdrawable= (bitmapdrawable) mimageview.getdrawable ();        Mbitmap=bitmapdrawable.getbitmap (); The two ways of panning, the effect is consistent//tEstTranslate1 (MBITMAP);    TestTranslate2 (MBITMAP);    Around the center point of the picture rotation and displacement of two ways, the effect is consistent//testrotate1 (MBITMAP);        TestRotate2 (MBITMAP);    The two ways of translation around the origin point are consistent//testrotateandtranslate1 (MBITMAP);        TestRotateAndTranslate2 (MBITMAP);    Test the scaling of two ways, the effect of consistent//testscale1 ();        TestScale2 (MBITMAP);    Test tilt Each of the two ways, the effect is consistent//testskewx1 ();    TestSkewX2 (MBITMAP);    TestSkewY1 ();    TestSkewY2 (MBITMAP);    TestSkewXY1 ();        TestSkewXY2 (MBITMAP);    Test symmetrical//testsymmetryx (MBITMAP);    Testsymmetryy (MBITMAP);    Testsymmetryxy (MBITMAP); }//Panning way one private void testTranslate1 (Bitmap Bitmap) {matrix matrix=new matrix (); int width=bitmap.getwidth ()    ; int height=bitmap.getheight (); matrix.posttranslate (width, height); Mimageview.setimagematrix (matrix);} Panning way Two private void TestTranslate2 (Bitmap Bitmap) {Matrix matrix=mimageview.getimagematrix (); int width= Bitmap.getwidth (); int height=bitmap.getheight (); matrix.posttranslate (width, height); mnewbitmaP=bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, false); Mimageview.setimagebitmap (MNEWBITMAP);} Rotation and displacement around the center point of the picture a private void testRotate1 (Bitmap Bitmap) {matrix matrix=new matrix (); int width=bitmap.getwidth (); int Height=bitmap.getheight (); Matrix.postrotate (45f, WIDTH/2, HEIGHT/2); matrix.posttranslate (width, height); Mimageview.setimagematrix (matrix);} How to rotate and shift around the center point of the picture two//NOTE://In the manner of a rotational 45° using Matrix.postrotate (45f, WIDTH/2, HEIGHT/2); But in mode two just rotate 22.5 degrees matrix.postrotate (45/2f, WIDTH/2, HEIGHT/2);p rivate void TestRotate2 (Bitmap Bitmap) {Matrix matrix= Mimageview.getimagematrix (); int width=bitmap.getwidth (); int height=bitmap.getheight (); Matrix.postrotate (45/2f, WIDTH/2, HEIGHT/2); matrix.posttranslate (width, height); Mnewbitmap=bitmap.createbitmap (Bitmap, 0, 0, width, height, Matrix, False); Mimageview.setimagebitmap (Mnewbitmap);} How to pan around the origin a private void testRotateAndTranslate1 (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width = Bitmap.getwidth (); int height = bitmap.getheight (); Matrix.setrotate (45f); matrix.posttranslate (width, height); Mimageview.setimagematrix (matrix);} How to pan around the origin two//NOTE://Ibid. private void TestRotateAndTranslate2 (Bitmap Bitmap) {Matrix Matrix = Mimageview.getimagematrix (); int width =bitmap.getwidth (); int height = bitmap.getheight (); matrix.setrotate (45/2f);    Matrix.posttranslate (width, height); Mnewbitmap=bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, false); Mimageview.setimagebitmap (MNEWBITMAP);} Scaled way one private void TestScale1 () {Matrix matrix = new Matrix (); Matrix.setscale (0.5f, 0.5f); Mimageview.setimagematrix ( Matrix);} Scaled way two private void TestScale2 (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width =bitmap.getwidth (); int height = bit Map.getheight (); Matrix.setscale (0.5f, 0.5f); Mnewbitmap=bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, False    ); Mimageview.setimagebitmap (MNEWBITMAP);} Horizontally inclined way one private void testSkewX1 () {Matrix matrix = new Matrix (); Matrix.setskew (0.5f, 0); Mimageview.setimagematrix ( Matrix);} Horizontally inclined SideType two private void testSkewX2 (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width =bitmap.getwidth (); int height = bitmap.ge    Theight (); Matrix.setskew (0.5f, 0); Mnewbitmap=bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, false); Mimageview.setimagebitmap (MNEWBITMAP);} Vertically sloping way one private void testSkewY1 () {Matrix matrix = new Matrix (); Matrix.setskew (0, 0.5f); Mimageview.setimagematrix ( Matrix);} Vertically sloping way two private void testSkewY2 (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width =bitmap.getwidth (); int height = Bitmap.getheight (); Matrix.setskew (0, 0.5f); Mnewbitmap=bitmap.createbitmap (bitmap, 0, 0, width, height, matrix, false)    ; Mimageview.setimagebitmap (MNEWBITMAP);} Horizontal and vertical tilt way one private void testSkewXY1 () {Matrix matrix = new Matrix (); Matrix.setskew (0.5f, 0.5f); Mimageview.setimagematrix (matrix);} Horizontal and vertical tilt mode two private void testSkewXY2 (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width =bitmap.getwidth (); int Heigh t = Bitmap.getheight (); Matrix.setskew (0.5f, 0.5f); mnewbitmap=bItmap.createbitmap (bitmap, 0, 0, width, height, matrix, false); Mimageview.setimagebitmap (MNEWBITMAP);} Horizontal symmetry--Pictures about x axisymmetric private void Testsymmetryx (Bitmap Bitmap) {Matrix matrix = new Matrix (); int height =bitmap.getheight (); f Loat matrixvalues[] = {1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues);//If Matrix.posttranslate (0, H eight);//indicates that the picture is upside down matrix.posttranslate (0, height*2); Mimageview.setimagematrix (matrix);} Vertical symmetry--picture about y-axis to private void Testsymmetryy (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width=bitmap.getwidth (); Float matrixvalues[] = { -1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues);//If Matrix.posttranslate (Widt    h,0);//Indicates the picture is inverted matrix.posttranslate (width*2, 0); Mimageview.setimagematrix (matrix);} About x=y symmetric private void Testsymmetryxy (Bitmap Bitmap) {Matrix matrix = new Matrix (); int width = bitmap.getwidth (); int Heigh t = bitmap.getheight (); float matrixvalues[] = {0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f};matrix.setvalues (matrixvalues); Matrix.posttranslate (Width+height, width+height); Mimageview.setimagematrix (matrix);} }

Main.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    >    < ImageView        android:id= "@+id/imageview"        android:layout_width= "fill_parent"        android:layout_height= " Fill_parent "        android:src=" @drawable/A "     />      </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.