Get picture width and height in Android

Source: Internet
Author: User

In Android, we want to get the width and height of the picture should do?
One, the normal load picture method to obtain the width and height

To give a simple example: Create a copy of a picture

//loading the originalBitmap BMSRC =Bitmapfactory.decoderesource (Getresources (), R.drawable.photo3); //Create a replica//1. Create the same size as the original image of the bitmap object, the object is currently no content, can be likened to creating the same size as the original//of white paperBitmap bmcopy =Bitmap.createbitmap (Bmsrc.getwidth (), Bmsrc.getheight (), Bmsrc.getconfig ()); //2. Create a Brush objectPaint paint =NewPaint (); //3. Create an artboard and lay the white Paper on the artboardCanvas Canvas =NewCanvas (bmcopy); //4. Start paintingCanvas.drawbitmap (BMSRC,NewMatrix (), paint); ImageView Iv1=(ImageView) Findviewbyid (R.ID.IV1); ImageView Iv2=(ImageView) Findviewbyid (R.ID.IV2);        Iv1.setimagebitmap (BMSRC); Iv2.setimagebitmap (bmcopy);

To get the width and height of the picture without loading the picture

You need to use the options object at this point, and give a simple example: Image zoom

Options opts =NewOptions (); //only request picture width height, do not parse picture pixel (Request picture attribute but not request memory, parse bitmap object, the object does not occupy memory)Opts.injustdecodebounds =true; //String Path = environment.getexternalstoragedirectory () + "/dog.jpg";Bitmapfactory.decodefile ("Sdcard/dog.jpg", opts); intImageWidth =Opts.outwidth; intImageHeight =Opts.outheight; //get the width and height of the screenDisplay DP =Getwindowmanager (). Getdefaultdisplay (); intScreenWidth =dp.getwidth (); intScreenHeight =dp.getheight (); //the width of the picture, divided by the width of the screen, calculates a wide and high scale, and takes a larger value as the scale of the picture.        intScale = 1; intScaleX = imagewidth/ScreenWidth; intScaleY = imageheight/ScreenHeight; if(ScaleX >= ScaleY && scaleX > 1) { scale=ScaleX; }Else if(ScaleX < ScaleY && ScaleY > 1) { scale=ScaleY;                } System.out.println (scale); //load pictures by zoom Scale//set the zoom ratioOpts.insamplesize =Scale ; //request memory for a pictureOpts.injustdecodebounds =false; Bitmap BM= Bitmapfactory.decodefile ("Sdcard/dog.jpg", opts); Iv.setimagebitmap (BM);

Get picture width and height in Android

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.