Some things you don't know about Android bitmap _android

Source: Internet
Author: User

This article for everyone to share the Android bitmap use details for your reference, the specific content as follows

1. Several ways for computer to represent graphics

1 BMP: Almost no compression occupies a larger space
2 JPG: On the basis of BMP on the adjacent pixel compression, occupy more space than BMP small
3 PNG: On the basis of JPG further compressed space is relatively small

This is a simple introduction to three formats, know what is going on line, in Android generally use PNG format pictures, because he takes up space small

2, the size of the graphic

Size of graphic = total pixel of picture * size of each pixel
Picture Total pixel = Picture's long * picture's high

You can see a few options when saving a picture
Monochrome bitmap: Each pixel can represent up to 2 colors, only bits with a length of 1, so each pixel takes up 1/8byte
16-color bitmap: Each pixel can represent up to 16 colors, so it only requires a binary representation of a length of 4, so each pixel occupies 1/2byte
256-color bitmap: Each pixel can represent a maximum of 256 colors, so it only takes a two-level presentation on a length of 8, so each pixel accounts for 1byte
24-bit bitmap: The RGB three-color bitmap occupies 3 byte per pixel.

3, Android load big picture failure reason

Once you know the knowledge above, look at the bitmap information in Android,
The ARGB is used in Android to represent the color, so each pixel can account for 4byte.
When the original image is loaded with bitmapfactory, when the picture width and height is larger than the width and breadth of the phone will appear

E/androidruntime (2128): caused By:java.lang.OutOfMemoryError
e/dalvikvm-heap (2128): Out of the memory on a 30720012- byte allocation.

That is, the memory is out of bounds, but the size of the picture does not exceed the amount of memory allocated to each application by the system.
So you can determine when loading the original image is calculated by the formula above.

4, the method of loading large graphs

Knowing the reason for the failure of loading a large graph, the solution to the outofmemoryerror is obvious, and the right is scaling.
Let's look at the next step in scaling
1) Get the width and height of the mobile phone
2 Get the width and height of the picture
3) Calculate the scaling ratio
4 Display picture according to scaling ratio

    1 Obtain the width and height of the mobile phone and
    windowmanager wm = (WindowManager) getsystemservice (window_service);
    int screenwidth = Wm.getdefaultdisplay (). getwidth ();
    int screenheight = Wm.getdefaultdisplay (). GetHeight ();
    2 Get picture width and height
    bitmapfactory.options options = new options ();
    Returns a null without bitmap, does not really parse the bitmap, but can return some information about the picture.
    options.injustdecodebounds = true; 
    Bitmapfactory.decoderesource (Getresources (), r.drawable.dog,options);
    int imagwidth = options.outwidth;
    int imagheight = options.outheight;
    3) Compute the scaling ratio
    int scale =1;
    int scalex = imagwidth/screenwidth;
    int ScaleY = imagheight/screenheight;
    Scale = Math.max (ScaleX, ScaleY);
    4 Display Picture Options.insamplesize = scale according to scaling ratio
    ;
    Start the real parse bitmap 
    options.injustdecodebounds = false;
    Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.dog,options);

5, bitmap Modify the picture

Looking at a situation, when using bitmapfactory.decodexxx decode a picture, you need to modify the picture
Such as:

 Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);
    Bitmap.setpixel (M, color.red);

found that the push to the phone after the error, this is because the original image can not be modified.
So what happens when you need to change the picture?
Yes, you can manipulate the original copy of the original, the copy can be modified
The copy steps to create the original artwork are as follows:

1) Create template
2 Create a canvas with the original artwork as a template
3 Create a brush
4) Start copying original artwork

Bitmap Bitmap = Bitmapfactory.decoderesource (Getresources (),
        r.drawable.ic_launcher);
    Bitmap.setpixel (M, color.red);
    1 Create template
    Bitmap Copybitmap = Bitmap.createbitmap (Bitmap.getwidth (),
        bitmap.getheight (), Bitmap.getconfig () );
    2 Create a canvas with the original image as a template
    Canvas Canvas = new Canvas (copybitmap);
    3 Create a brush
    Paint Paint = new Paint ();
    4 Start copying original
    Canvas.drawbitmap (bitmap, New Matrix (), paint);

    Modify the picture
    Copybitmap.setpixel (color.red);

At this point I found some small details of the work, and other methods of operation of the bitmap should be known to everyone, no longer to swim the eggs.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.