Picture optimization steps in Android development

Source: Internet
Author: User

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

Related Article

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.