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:
Drawable: A general graphic object used to load images in common formats, which can be images like PNG, JPG, and also the visual objects of the 13 types of Drawables we learned before! We can think of it as a picture frame for holding paintings!
Bitmap (Bitmap): We can think of it as a painting easel, where we first place the painting, and then we can perform some processing, such as obtaining image file information, rotating cutting, zooming in and out, and other operations!
Canvas (Canvas): As its name suggests, a canvas, where we can draw (paint), you can use Paint (brush) to draw various shapes or write, and you can also use Path (path) to draw multiple points and connect them into various shapes!
Matrix (Matrix): Used for graphic effect processing, color matrix (ColorMatrix), and using Matrix for image translation, scaling, rotation, tilting, etc.!
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
public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream) Compress the bitmap to the specified OutputStream, which can be understood as saving the Bitmap to a file! format: format, PNG, JPG, etc.; quality: compression quality, 0-100, 0 indicates the lowest quality compression, 100 is the highest quality (PNG is lossless and will ignore the quality setting) stream: output stream The return value represents whether it is successfully compressed to the specified stream!
void recycle (): Recycle the memory space occupied by the bitmap and mark the bitmap as Dead
boolean isRecycled (): Determine whether the bitmap memory has been released
int getWidth (): Get the width of the bitmap
int getHeight (): Get the height of the bitmap
boolean isMutable (): Whether the image is modifiable
int getScaledWidth (Canvas canvas): Get the width of the image after density conversion
int getScaledHeight (Canvas canvas): Get the height of the image after density conversion
Static Methods:
Bitmap createBitmap (Bitmap src): Generate an immutable new image based on src
Bitmap createScaledBitmap (Bitmap src, int dstWidth,int dstHeight, boolean filter): Create a new image based on final View contentView = getWindow().getDecorView(); try { Log.e("HEHE", contentView.getHeight() + ":" + contentView.getWidth()); bitmap = Bitmap.createBitmap(contentView.getWidth(), contentView.getHeight(), Bitmap.Config.ARGB_4444); contentView.draw(new Canvas(bitmap)); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteOut); savePic(bitmap, "sdcard/short.png"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != byteOut) byteOut.close(); if (null != bitmap && !bitmap.isRecycled()) { // bitmap.recycle(); bitmap = null; } } catch (IOException e) { e.printStackTrace(); } } } }; try { action.run(); } catch (Exception e) { e.printStackTrace(); } }
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~
1.5.1 Git Tutorial on Basic Operations of Local Repositories
1.8 Project Related Analysis (Various Files, Resource Access)
2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
2.5.8 Detailed Explanation of Notification (Status Bar Notification)
2.6.4 Simple Usage of DrawerLayout (Official Side-Slide Menu)
3.3 A Brief Analysis of the Handler Message Passing Mechanism
3.6 Responding to System Setting Events (Configuration Class)
4.4.2 ContentProvider: Further Exploration - Document Provider
5.2.1 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 1)
5.2.2 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 2)
5.2.3 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 3)
5.2.4 Detailed Explanation of Fragment Example - Bottom Navigation Bar + ViewPager Page Switching
6.2 Data Storage and Access - SharedPreferences to Save User Preferences
6.3.1 Data Storage and Access - An Introduction to SQLite Database
6.3.2 Data Storage and Access - Another Look at SQLite Database
7.1.1 What to Learn in Android Network Programming and Learning the Http Protocol
[7.1.2 Learning about Android Http Request and Response Headers](android-
8.3.14 Paint API - Several Enum/Constant Values and ShadowLayer Shadow Effect
8.3.17 Detailed Explanation of Canvas API (Part 2) - Collection of Clipping Methods
8.3.18 Detailed Explanation of Canvas API (Part 3) - Matrix and drawBitmapMesh
8.4.3 Android Animation Collection - Property Animation - First Glance
8.4.4 Android Animation Collection - Property Animation - Another Look
11.0 "2015 Latest Android Basic Tutorial" Completion Celebration~
12.2 DrySister Girl Viewing App (First Edition) - 2. Parsing Backend Data
12.4 DrySister Girl Viewing App (First Edition) - 4. Adding Data Caching (Integrating SQLite)
12.5 DrySister Girl Viewing App (First Edition) - 5. Code Review, Adjustment, and Log Class Writing