Android implements bitmap clipping

Source: Internet
Author: User

We can not always rely on bitmapfactory below to tell you how to intercept a part from BITMAQP to create a new bitmap
The system will have a default PNG image: Icon.png But the outermost part of this picture is a white one. Now, for example, how to intercept it because the outer layer is white, so I used *.9.png as its boundary.


Create BITMAOP and point to Icon.png

<span style= "FONT-SIZE:12PX;" >bitmap ori = Bitmapfactory.decoderesource (This.getresources (), R.drawable.icon);  </span>


Create a layout file There are 2 ImageView one for the original image to display a split post
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout  xmlns:android= "http://schemas.android.com/apk/ Res/android "      android:orientation=" vertical "      android:layout_width=" fill_parent "      android:layout_ height= "Fill_parent"      android:id= "@+id/layout"      >  <imageview android:layout_width=        "Wrap_ Content "       android:layout_height=" wrap_content "       android:id=" @+id/image1 "      android:layout_gravity=" Center_horizontal "      />  <imageview        android:layout_width=" wrap_content "       android:layout_ height= "Wrap_content"       android:id= "@+id/image2"      android:layout_gravity= "center"      />  </ Linearlayout>  


Initialize Variables

Llauout = (linearlayout) Findviewbyid (r.id.layout);            Iv1 = (ImageView) Findviewbyid (r.id.image1);  Iv2 = (ImageView) Findviewbyid (r.id.image2);  

get the width and height of the original image for later use
<span style= "FONT-SIZE:12PX;" >width = Ori.getwidth ();  Height = ori.getheight ();  </span>



define the variable flag split position and initialize it

<span style= "FONT-SIZE:12PX;" ><span class= "keyword" style= "Font-family:monaco, ' Dejavu Sans mono ', ' bitstream Vera Sans Mono ', Consolas, ' Courie R New ', monospace; line-height:18px; Color:rgb (127, 0, 85); Font-weight:bold; " >int</span><span style= "Font-family:monaco, ' Dejavu Sans mono ', ' bitstream Vera Sans Mono ', Consolas, ' Couri Er New ', monospace; line-height:18px; " > startx,starty,lengthx,lengthy; </span></span>

<span style= "Font-family:monaco, ' Dejavu Sans mono ', ' bitstream Vera Sans Mono ', Consolas, ' Courier New ', monospace; line-height:18px; " ></span>

<span style= "FONT-SIZE:12PX;" >startx = 0;  Starty = 0;    Lengthx = width;  lengthy = height;  </span>


How to choose a picture locationfunction Prototypes: Bitmap.createbitmap (Bitmap source, int x, int y, int width, int height)

Method 1: Continuously adjust the number of references: X,y,width,heighy
Method 2: Use the navigation key to control the above 4 variables respectively
Navigation key left: x
navigation key right: Width
on the navigation key: Y
under Navigation key: Height
bring it closer to the center of the picture and press the move fixed distance

public boolean OnKeyDown (Int. keycode, KeyEvent msg) {                    switch (keycode) {case          keyevent.keycode_dpad_left:              Updateleft ();              break;                        Case Keyevent.keycode_dpad_right:              updateright ();              break;          Case KEYEVENT.KEYCODE_DPAD_UP:              updateup ();              break;          Case Keyevent.keycode_dpad_down:              updatedown ();              break;          Case Keyevent.keycode_dpad_center:              showresult ();              break;          }                    return false;      }  

There is also a need to infer whether the movement

The following scenarios are unreasonable:
1. When the left side of the graph is larger than the maximum width
2. When the top of the graph is larger than the maximum height
3. Graphic width or height less than 0

public Boolean Isupdateok () {          if (StartX > lengthx) | | (Starty > lengthy) | | (Lengthx > 0) | | (lengthy > 0)) {              return false;          }          else {              return true;          }      }  


Detailed Move Method
public void Updateleft () {StartX + = step;          Lengthx = Width-startx;                    lengthy = Height-starty;                            if (Isupdateok ()) {target1.recycle ();                            Target1 = Bitmap.createbitmap (Ori,startx, Starty, Lengthx, lengthy);                            Iv2.setimagebitmap (TARGET1);          Setcontentview (llauout);          }} public void Updateup () {starty + = step;          Lengthx = Width-startx;                    lengthy = Height-starty;                            if (Isupdateok ()) {target1.recycle ();                            Target1 = Bitmap.createbitmap (Ori,startx, Starty, Lengthx, lengthy);                            Iv2.setimagebitmap (TARGET1);          Setcontentview (llauout);                    }} public void Updateright () {lengthx-= step;                            if (Isupdateok ()) {target1.recycle (); Target1 = Bitmap.createbiTMap (Ori,startx, Starty, Lengthx, lengthy);                            Iv2.setimagebitmap (TARGET1);          Setcontentview (llauout);                    }} public void Updatedown () {lengthy-= step;                            if (Isupdateok ()) {target1.recycle ();                            Target1 = Bitmap.createbitmap (Ori,startx, Starty, Lengthx, lengthy);                            Iv2.setimagebitmap (TARGET1);          Setcontentview (llauout);          }} public void Showresult () {Alertdialog.builder ab = new Alertdialog.builder (this);                    Alertdialog Adialog; Ab.setmessage ("StartX:" +startx+ "\ n" + "Starty:" +starty+ "\ n" + "LENGTHX:" +lengthx+ "\ n" + "lengthy:" +lengthy). Settitle          ("Show result"). Show ();;            Adialog = Ab.create ();                Adialog.show ();   }


Emulator implementation Status:

Android implements bitmap clipping

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.