Solution to the problem of network load point nine graph resource deformation

Time:2021-6-3

Dot nine image is a unique image format in Android system, with suffix. 9. PNG. The main principle is to stretch the part of the picture, such as chat bubble box. You can choose to stretch only the middle area and not the corners, so that the bubble box will not deform with the change of the content. How to choose which area to stretch can be edited in Android studio. The principle is to add pure black lines to the border of the picture, and the line area is the stretchable area.

There is no black border in the figure below. The whole picture is stretched, which results in the distortion of the picture.
Solution to the problem of network load point nine graph resource deformation

In the figure below, the black border is added and the stretch area is selected, so the picture will not be deformed.
Solution to the problem of network load point nine graph resource deformation

But, it’s not that simple!!!

In the development process, we meet a requirement that chat bubbles need to dynamically download from the network, and then read the point nine pictures from the cache, but the loaded point nine pictures are still deformed.

However, there are still some solutions, that is, the compilation and conversion steps in the red circle in the figure below. Before uploading the picture, you can make a compilation and conversion for the nine point picture to display it normally. AAPT is required for compilation transformation. Where is AAPT installed? Android studio click file, select project structure, select SDK location, and get the Android SDK location, then the path of AAPT is Android SDK location / build tools / * * / AAPT.

As shown below, after AAPT conversion of bubble.9.png, the generated bubble 1.9.png is the compiled image, which will not be deformed when downloaded from the network.

aapt s -i /xxx/xxx/bubble.9.png -o /xxx/xxx/bubble1.9.png

Solution to the problem of network load point nine graph resource deformation

How to load a. 9 image from the cache?

Bitmap bitmap = BitmapFactory.decodeFile(resPath);
byte[]chunk = bitmap.getNinePatchChunk();
if (NinePatch.isNinePatchChunk(chunk)) {
    NinePatchDrawable drawable = new NinePatchDrawable(bitmap, chunk, new Rect(), null);
    mFlowAsrTxAnimation.setBackground(drawable);
    Log.d(TAG, "isNinePatchChunk");
} else {
    BitmapDrawable drawable = new BitmapDrawable(bitmap);
    mFlowAsrTxAnimation.setBackground(drawable);
    Log.d(TAG, "isNotNinePatchChunk");
}

be careful:Execute AAPT command on MAC and Bash: AAPT: command not found appears. The solution can copy AAPT in Android SDK location / build tools / * * / AAPT to / usr / local / bin directory.