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