Easy Tutorial
❮ Builder Pattern Hadoop Skills ❯

8.2.1 Bitmap (Bitmap) Full Analysis Part 1

Classification Android Basic Tutorial

Introduction to This Section:

>

In the previous section, we discussed the 13 types of Drawables in Android. Have you applied them to your own projects? In this section, we will explore the use of Bitmap (Bitmap), and before we start the content of this section, let's distinguish the concepts of several terms:

All of the above are interfaces provided by Android's underlying graphics class: android.graphics! Well, without further ado, let's start this section!

PS: Official Documentation: Bitmap


1. Understanding Bitmap, BitmapFactory, BitmapFacotry.Options

>

As the title suggests, I could have directly talked about the relationship between these three things, but I just want to be arrogant and look at the code!

What I roughly want to say is: The constructor of Bitmap is private, and it cannot be instantiated from the outside, only through JNI instantiation! Of course, there will definitely be an interface provided for us to create Bitmaps, and this interface class is: BitmapFactory! Come on, open the BitmapFactory class, and we can see that BitmapFactory provides us with these methods, most of which are decodeXxx, creating Bitmaps in various forms!

Then we found that, for each method, there is a parameter of the Options type, let's take a look inside:

So we found out that this is a static inner class: BitmapFacotry.Options! And it is used to set the options during decoding!

We set some of the values of the parameters here, such as setting inJustDecodeBounds to true to avoid OOM (out of memory), what, don't know OOM, it's okay, I'll tell you a little later! Finally, back to our Bitmap! Well, there are many methods in Bitmap, so I won't explain them one by one, let's pick a few that are used more often to explain!

Chinese Document: Android Chinese API (136) —— Bitmap


2. Common Methods of Bitmap

>

Common Methods

Static Methods:

private void savePic(Bitmap b, String strFileName) { FileOutputStream fos = null; try { fos = new FileOutputStream(strFileName); if (null != fos) { boolean success = b.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); if (success) Toast.makeText(MainActivity.this, "Screen capture successful", Toast.LENGTH_SHORT).show(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }


Code Analysis :

>

The code is very simple, final View contentView = getWindow().getDecorView(); this line of code is to get the current XML root node View! Then set the size of the screenshot, call contentView.draw(new Canvas(bitmap)); OK, then convert the bitmap to a stream, and then write to the SD card, that's it ~ Of course, from the result we can also see that the screenshot is only the content of this APP! If you want to take a full screen screenshot, Google it yourself!


Summary of This Section:

>

This section explains Bitmap, BitmapFactory and its static inner class Options, as well as the basic usage of BitmapDrawable. In fact, we just need to know how to create Bitmap, its extension is generally achieved through Matrix and Canvas, Bitmap, we pay more attention to the OOM problem, the next section we will learn how to avoid Bitmap's OOM problem! Thank you~

WeChat Follow

❮ Builder Pattern Hadoop Skills ❯