Pictures of Android Development (4)

Source: Internet
Author: User

Answer the previous article.
The main research is the use of bitmap and drawable, and the difference between the two.
Look at the test code first:

Packagecom. Example. Imagetext;Import Java. Util. ArrayList;Import Java. Util. List;Importcom. Example. Imagetext. R. drawable;Import Android. Annotation. Suppresslint;Import Android. App. Activity;Import Android. Content. Context;Import Android. Content. Intent;Import Android. Graphics. Bitmap;Import Android. Graphics. Bitmapfactory;Import Android. Graphics. drawable. Bitmapdrawable;Import Android. Graphics. drawable. Drawable;Import Android. OS. Build;Import Android. OS. Bundle;Import Android. OS. Handler;Import Android. Util. Displaymetrics;Import Android. Util. Log;Import Android. View. WindowManager;Import Android. Widgets. ImageView;public class Mainactivity extends Activity {private drawable Drawable1,drawable2,drawable4,drawable3;Private Bitmap BITMAP1,BITMAP2,BITMAP3,BITMAP4,BITMAP6,BITMAP5,BITMAP7;Private ImageView Iv1,iv2;@Override protected void OnCreate (Bundle savedinstancestate) {Super. OnCreate(savedinstancestate);Setcontentview (R. Layout. Activity_main);Drawable1 = Getresources (). Getdrawable(R. drawable. BG_1);Drawable2 = Getresources (). Getdrawable(R. drawable. BG_1);Bitmap1 = ((bitmapdrawable) drawable1). Getbitmap();BITMAP2 = ((bitmapdrawable) drawable2). Getbitmap();iv1= (ImageView) Findviewbyid (R. ID. IV1);Iv2= (ImageView) Findviewbyid (R. ID. IV2);Iv1. Setimageresource(R. drawable. BG_1); Iv2. Setimageresource(R. drawable. BG_1);Drawable3 = Iv1. Getdrawable();Drawable4 = Iv2. Getdrawable();BITMAP3 = ((bitmapdrawable) drawable3). Getbitmap();BITMAP4 = ((bitmapdrawable) drawable4). Getbitmap();BITMAP5 = Bitmapfactory. Decoderesource(Getresources (), R. drawable. BG_1);Bitmap6=bitmapfactory. Decoderesource(Getresources (), R. drawable. BG_1);BITMAP7 = Bitmapfactory. Decodestream(Getresources (). Openrawresource(R. drawable. BG_1));if (drawable1. Equals(Drawable2)) {Log. E("Tttext","Drawble1==drawable2");}else{Log. E("Tttext","Drawble1!=drawable2");} if (Drawable3. Equals(DRAWABLE4)) {Log. E("Tttext","Drawble3==drawable4");}else{Log. E("Tttext","Drawble3!=drawable4");} if (Bitmap1. Equals(BITMAP2)) {Log. E("Tttext","BITMAP1==BITMAP2");}else{Log. E("Tttext","BITMAP1!=BITMAP2");} if (bitmap3. Equals(BITMAP4)) {Log. E("Tttext","BITMAP3==BITMAP4");}else{Log. E("Tttext","BITMAP3!=BITMAP4");} if (bitmap3. Equals(BITMAP1)) {Log. E("Tttext","Bitmap3==bitmap1");}else{Log. E("Tttext","Bitmap3!=bitmap1");} if (BITMAP5. Equals(BITMAP6)) {Log. E("Tttext","BITMAP5==BITMAP6");}else{Log. E("Tttext","BITMAP5!=BITMAP6");} Log. E("Tttext","Bitmap1 occupies memory:"+getbitmapsize (BITMAP1)/1024x768/1024x768+"M");Log. E("Tttext","BITMAP3 occupies memory:"+getbitmapsize (BITMAP3)/1024x768/1024x768+"M");Log. E("Tttext","BITMAP5 occupies memory:"+getbitmapsize (BITMAP5)/1024x768/1024x768+"M");Log. E("Tttext","BITMAP7 occupies memory:"+getbitmapsize (BITMAP7)/1024x768/1024x768+"M");} @SuppressLint ("Newapi") Public long getbitmapsize (Bitmap Bitmap) {if (Build. VERSION. SDK_int >= Build. VERSION_codes. Honeycomb_MR1) {return bitmap. GetByteCount();}//Pre HC-MR1 return bitmap. Getrowbytes() * Bitmap. GetHeight();}}

The printing results are as follows:

OK, now let's analyze each of them.

1. drawable1 = getResources().getDrawable(R.drawable.bg_1);
drawable2 = getResources().getDrawable(R.drawable.bg_1);
bitmap1 = ((BitmapDrawable)drawable1).getBitmap();
bitmap2 = ((BitmapDrawable)drawable2).getBitmap();

As you can see from the print results, drawable1 and drawable2 are not the same objects, but they hold the same object for bitmap. As I have already said in the previous article, when drawable refers to a resource picture, it points to a bitmap object bitmapdrawable and it will go to the cache to find out if this bitmap is already present, or if there is a direct reuse, create a new one.

2. Iv1.setimageresource (r.drawable.bg_1);
Iv2.setimageresource (r.drawable.bg_1);

    drawable3 = iv1.getDrawable();    drawable4 = iv2.getDrawable();    bitmap3 = ((BitmapDrawable)drawable3).getBitmap();    bitmap4 = ((BitmapDrawable)drawable4).getBitmap();   

See here, Although we use the. Setimageresource method, the same results are printed, but, unlike the drawble and bitmap referenced by Setimageresource and setimagedrawable, the Setimagereso Urce only re-use Setimageresource bitmap, setimagedrawable will only reuse setimagedrawable bitmap, here to note.

3. BITMAP5 = Bitmapfactory.decoderesource (Getresources (), r.drawable.bg_1);
Bitmap6=bitmapfactory.decoderesource (Getresources (), r.drawable.bg_1);
BITMAP7 = Bitmapfactory.decodestream (Getresources (). Openrawresource (r.drawable.bg_1));

Here, I'm loading the bitmap directly from the resource folder.
, using two methods, from the printed results, we can know that all three are not the same bitmap object, and have not been reused.
But From the memory size: Bitmapfactory.decoderesource Get Bitmap is still magnified 4 times times, (why magnification is 4 times times, you can see what I said in the last article, the test machine is 320dpi, and this picture is placed under the mdpi file), and BITMAPFAC What Tory.decodestream get is the real picture without scaling, which is the difference between the two methods.

4.
Setimageresource (directly in XML SRC Reference is also called this method)
Setimagedrawable
Setimagebitmap
What is the difference between these methods?

First from the source can be seen, setimagedrawable and Setimagebitmap These two methods are the same, are called setimagedrawable, The reason to differentiate a setimagebitmap is to make it easier for us to convert the image of a web image or a local non-resource file into a bitmap scale. In addition, these two methods are asynchronous to load.

and Setimageresource or src this from the source can also be seen, they are executed in the main thread, may cause the activity of the start of the delay.

5. Is there any need to call Bitmap.recycle () This method to manually recycle pictures, here should be divided into scenes:
If you are loading a picture from a resource folder, it is best not to manually release it, because it will reuse the bitmap in the cache but will not be judged whether it is recycled or it is prone to anomalies.
If the picture is loaded from the Web or from a file such as a local photo album, because we use the stream to turn to bitmap, even if the recycling is not to reuse, this time can be manually recycled, and can be used in conjunction with soft references.
There may be people to ask, if I have to use the resources folder in the picture, and do not have to manually recycle, but also to ensure that no exception to do? For example a banner page, this time, I recommend not to use the Setimageresource (directly in the XML SRC reference is also called this method)
Setimagedrawable these two methods.

should use bitmap=. Bitmapfactory.decoderesource and Setimagebitmap combined, here is not reused, and the system will be based on the frequency screen density to scale, to avoid the waste of memory, at the same time you can also manually collect the bitmap without reporting abnormal, because I said above, with this side The law is that the system is not going to reuse bitmap.

The specific difference between 6.bitmap and drawble, I conclude that bitmap represents a bitmap that can be understood as an object, and drawble represents a picture, which can be a bitmap or a graphic.
Both bitmap and drawble can be scaled, but only bitmap can perform precise operations on pixels.
So when you want to compress the pixels accurately, you have to use bitmap. such as the transmission and loading of network images. But it's worth noting that we can get the bitmap information from the stream first, and calculate the width you want to zoom to. Then loaded into bitmap, you can load the entire large map into the bitmap in the beginning to cause memory overflow, in fact, many times because of this cause of oom, such as taking pictures.

Next, there is a final article, mainly for bitmap how to avoid memory overflow and increase efficiency in the case of scaling.

Pictures of Android Development (4)

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.