Image optimization method in Android development
1. Image loading method, user-friendly loading image
/***
* Load local image
* @param context: Main Run function instance
* @param bitadress: Image address, General point to the drawable directory under R
* @return
*/
Public final Bitmap Creatimage (context context, int bitadress) {
Bitmap bitmaptemp = null;
Bitmaptemp = Bitmapfactory.decoderesource (Context.getresources (),
bitadress);
return bitmaptemp;
}
2. Average image segmentation method, the large image is divided into n rows n columns, easy to use
/***
* Image Segmentation
*
* @param g
*: Canvas
* @param paint
*: Brushes
* @param imgbit
*: Picture
* @param x
*: X-axis start coordinates
* @param y
*: Y-axis start coordinates
* @param W
*: Width of single image
* @param h
*: Height of single image
* @param line
*: The first few columns
* @param row
*: The first few lines
*/
Public final void Cuteimage (Canvas g, paint paint, Bitmap imgbit, int x,
int y, int w, int h, int line, int row) {
G.cliprect (x, y, x + W, h + y);
G.drawbitmap (Imgbit, X-line * W, Y-row * H, paint);
G.restore ();
}
3. Zoom image to zoom in on the current image
/***
* How to zoom image
*
* @param bgimage
*: Source Picture Resource
* @param newwidth
*: Width after zooming
* @param newheight
*: Height after zooming
* @return
*/
Public Bitmap zoomimage (Bitmap bgimage, int newwidth, int newheight) {
Get the width and height of this picture
int width = bgimage.getwidth ();
int height = bgimage.getheight ();
Create a Matrix object for manipulating pictures
Matrix matrix = new Matrix ();
Calculate the zoom rate, the new size in addition to the original size
float ScaleWidth = ((float) newwidth)/width;
float ScaleHeight = ((float) newheight)/height;
Zoom picture Action
Matrix.postscale (ScaleWidth, ScaleHeight);
Bitmap Bitmap = Bitmap.createbitmap (bgimage, 0, 0, width, height,
Matrix, True);
return bitmap;
}
Picture optimization steps in Android development