abarisain / dmix

A modern MPD Client for Android.
Apache License 2.0
591 stars 198 forks source link

Suggestion for improving bitmap loading #837

Open struggggle opened 7 years ago

struggggle commented 7 years ago

Dear developers, I found an operation of bitmap displaying that can be optimized in MPDroid.

When loading an image, Google suggests us to resize the image before decoding them. it works like this:

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }

I noticed that in the following code, image resizing is correctly applied: com.namelessdev.mpdroid.tools.Tools.java decodeSampledBitmapFromBytes() (line 103 ) com.namelessdev.mpdroid.tools.Tools.java decodeSampledBitmapFromPath() (line 128) com.namelessdev.mpdroid.helpers.CoverManager.java run() (line 835)

However, in the code below, images are decoded directly without resizing: com.namelessdev.mpdroid.service.AlbumCoverHandler.java doInBackground() (line 280)

https://github.com/abarisain/dmix/blob/master/MPDroid/src/main/java/com/namelessdev/mpdroid/service/AlbumCoverHandler.java

`

        mAlbumCoverPath = params[0];

        mFullSizeAlbumCover = BitmapFactory.decodeFile(mAlbumCoverPath);

        if (mFullSizeAlbumCover == null) {
            /** TODO: Consider album cover reset here? */
            mNotificationCover = null;
        } else {
            /** This will always scale down, no filter needed. */
            mNotificationCover =
                    Bitmap.createScaledBitmap(mFullSizeAlbumCover, mIconWidth, mIconHeight,
                            false);
        }`

I am curious about the latter case. Why this method do not resize images?

In addition, I noticed that you call createScaledBitmap() to resize the loaded image in the above code, perhaps resizing the images before decoding them can help make app more smooth (reduce memory consumption).

Looking forward to your response and hope I can help improve MPDroid. Thanks.

References: https://developer.android.com/training/displaying-bitmaps/load-bitmap.html

abarisain commented 7 years ago

Hello!

Thanks for the suggestion.

From what I remember, bitmaps are not resized so they can be saved with their full size on disk. Covers are shown in many views, which have many different sizes, so we can't downscale them. I haven't touched the code in a while, so there's a huge change that what I'm saying is totally wrong.

I also see that it could be improved, but view sizes are not always known (more often than not, covers are requested before the first layout pass, so the ImageViews don't know their size yet).

That said, you're welcome to improve this how you want, but please know that MPDroid is no longer maintained, as I lack time and interest to do so :) You might want to collab with other people still working on it, as it apparently has gained some traction again these past months.