Android rookie growth record--BITMAP

Source: Internet
Author: User

Bitmap Introduction

Bitmap is one of the most important classes of image processing in an Android system. It can get image file information, make image cut, rotate, zoom and so on, and can save image file in specified format. From the perspective of application, this paper emphatically introduces how to use bitmap to realize these functions.

Common Properties of bitmap

1. Bitmap class
public void Recycle ()--reclaims the memory space occupied by the bitmap and marks the bitmap as dead
Public Final Boolean isRecycled ()--Determines whether the bitmap memory has been disposed
Public final int getwidth ()--Gets the width of the bitmap
Public final int getheight ()--Gets the height of the bitmap
Public Final Boolean ismutable ()--whether the picture can be modified
public int getscaledwidth (canvas canvas)--Gets the width of the image after the specified density conversion
public int getscaledheight (canvas canvas)--Gets the height of the image after the specified density conversion
public boolean compress (compressformat format, int quality, OutputStream stream)-Converts a picture to an output stream in the specified picture format and quality.
Format:Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.JPEG
Quality: Picture quality, 0-100.0 represents the lowest picture quality compression, 100 with the highest picture quality compression. For images in lossless format such as PNG, this setting is ignored.

Common static methods:
public static Bitmap CreateBitmap (Bitmap src)--Create a new image with Src as the original
public static Bitmap Createscaledbitmap (Bitmap src, int dstwidth,
int Dstheight, Boolean filter)--Creates a new image with Src as the original, specifying the aspect of the new image and whether it is mutable.
public static Bitmap CreateBitmap (int width, int height, config config)--Creates a bitmap of the specified format and size
public static Bitmap CreateBitmap (Bitmap source, int x, int y, int width, int height) takes source as the original, creates a new picture, specifies the starting coordinates, and the height width of the new image.
2. Bitmapfactory Factory class:
Option parameter class:
Public Boolean injustdecodebounds--If set to true, does not get the picture, does not allocate memory, but returns the height width information of the picture.
public int insamplesize--A multiple of the picture scale. If set to 4, then the width and height are the original 1/4, then the figure is the original 1/16.
public int outwidth--Gets the width value of the picture
public int outheight--Gets the height value of the picture
public int indensity--pixel compression ratio for bitmaps
public int intargetdensity--The pixel compression ratio for the target bitmap (the bitmap to be generated)
The public boolean inscaled--is set to True when the picture is compressed, from indensity to intargetdensity.

Use Bitmapfactory to decode the generated bitmap object from the resources files, streams, and byte-arrays.
Read a file path to get a bitmap. If the specified file is empty or cannot be decoded into a file, NULL is returned.
public static Bitmap DecodeFile (String pathName, Options opts)
public static Bitmap DecodeFile (String pathName)
Read a resource file to get a bitmap. Null is returned if the bitmap data cannot be decoded, or if the OPTs parameter requests only the size information.
(That is, when options.injustdecodebounds=true, only the size information of the picture is requested.) )
public static Bitmap Decoderesource (Resources res, int id)
public static Bitmap Decoderesource (Resources res, int ID, Options opts)
Decode a bitmap from the input stream
public static Bitmap Decodestream (InputStream is)
To decode from a byte array to generate a immutable bitmap
public static Bitmap Decodebytearray (byte[] data, int offset, int length)

Bitmapdrawable Class: Inherited from Drawable, you can create from file paths, input streams, XML files, and bitmap.
Common constructors:
Resources res=getresources ();//Resource acquisition
Public bitmapdrawable (Resources Res)--Create an empty drawable. (Response used to specify the pixel density used at initial time) instead of the public bitmapdrawable () method (this method does not handle pixel density)
Public Bitmapdrawable (Resources res, Bitmap Bitmap)--create drawable from a Bitmap
Public Bitmapdrawable (Resources res, String filepath)--create a drawable by opening a given file path and decoding the bit Map.
Public Bitmapdrawable (Resources res, java.io.InputStream are)--create a drawable by decoding a bitmap from the given input Stream.

Bitmap get a picture example from a network connection:
1 Package Com.example.bitmaodemo;2 3 import java.io.IOException;4 import Java.io.InputStream;5 import java.net.HttpURLConnection;6 import java.net.MalformedURLException;7 import Java.net.URL;8 9 import android.app.Activity;Ten import Android.graphics.Bitmap; One import android.graphics.BitmapFactory; A import Android.os.Bundle; - import Android.os.Handler; - import android.os.Message; the import Android.util.Log; - import Android.view.View; - import Android.view.View.OnClickListener; - import Android.widget.Button; + import Android.widget.ImageView; - import Android.widget.Toast; +  A  Public classMainactivity extends Activity { at     PrivateThread tr; -     PrivateBitmap Bitmap; -     PrivateRunnable run; -  -     PrivateButton btn; -     PrivateImageView IV; in     PrivateHandler Handler; -      to     //the path of the picture, Baidu any one can +     Private StaticFinal String url_img ="http://p4.music.126.net/UVauLiOkCCNWR-XBMIDawg==/3437073360051219.jpg"; -  the @Override *     protected voidonCreate (Bundle savedinstancestate) { $ super.oncreate (savedinstancestate);Panax Notoginseng Setcontentview (r.layout.activity_main); -  theBTN =(Button) Findviewbyid (R.ID.BTN); +IV =(ImageView) Findviewbyid (r.id.imageview1); A  theBtn.setonclicklistener (NewOnclicklistener () { +  - @Override $              Public voidOnClick (View arg0) { $                 //get a new thread and download the diagram when you click the button -                 NewThread (NewRunnable () { -  the @Override -                      Public voidrun () {Wuyi                         Try { the                             //get the path to a picture -URL url =NewURL (url_img); Wu                             //Create a network connection -HttpURLConnection conn =(httpurlconnection) URL About . OpenConnection (); $ Conn.connect (); -                             //gets an input stream that reads information from the network connection above -InputStream is=Conn.getinputstream (); -                             if( is!=NULL) { ALOG.I ("Info","----------------"); +                                 /* the * Use Bitmapfactory to decode generated bitmap objects from resources files, streams, and byte-arrays.  - * Read a file path to get a bitmap. If the specified file is empty or cannot be decoded into a file, NULL is returned.  $                                  */ theBitmap = Bitmapfactory.decodestream ( is); the                             } the                              theMessage message =NewMessage (); -Message.obj =bitmap; inMessage.what =0; the                             //send a message to handler, you can follow the new UI the handler.sendmessage (message); About  the}Catch(malformedurlexception e) { the                             //TODO auto-generated Catch block the e.printstacktrace (); +}Catch(IOException e) { -                             //TODO auto-generated Catch block the e.printstacktrace ();Bayi                         } the  the                     } -  - }). Start (); the             } the         }); the  theHandler =NewHandler () { - @Override the              Public voidhandlemessage (Message msg) { the                 Switch(msg.what) { the                  Case 0:94LOG.I ("Info","SS"+msg.obj); the Iv.setimagebitmap ((Bitmap) msg.obj); the                      Break; the                 }98             } About         }; - 101     }102}

Since bitmap cannot be run in the main thread, we need to use other threads to perform a read of the picture.

Results:

After clicking the button:

Android rookie growth record--BITMAP

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.