0

Im trying to add some images to my android app. First of all, i want to add my background image, but when i do it app starts working much slower, animations not smoothly and app just lagging. First I added it this way:

android:background="@mipmap/background.png"

What i tryed after:

1) Make bg image in different resolutions, for different screen size and put it to corresponding folder in resourses. My bg image resolutins:

MDPI: 320x467 px
HDPI: 480x800 px
XDPI: 640x960 px
XXDPI: 960x1400 px
XXXDPI: 1280x1920 px

That didnt worked.

2) Remove all images from Android Studio to my backend and get them by request with AsyncTask: First, i choosing url for resolution by dpi:

URL url = null;
try {
    float density = getResources().getDisplayMetrics().density;
    url = new URL(PicturesApi.getUrlByDPI(density));
} catch (MalformedURLException e) {
    e.printStackTrace();
}
new SetImageBackground().execute(url);

getUrlByDPI method:

public static String getUrlByDPI(float density){
    if (density == 0.75f)
    {
        return "http://back_url/static/ldpi/background.png";
    }
    else if (density >= 1.0f && density < 1.5f)
    {
        return "http://back_url/static/mdpi/background.png";
    }
    else if (density == 1.5f)
    {
        return "http://back_url/static/hdpi/background.png";
    }
    else if (density > 1.5f && density <= 2.0f)
    {
        return "http://back_url/static/xhdpi/background.png";
    }
    else if (density > 2.0f && density <= 3.0f)
    {
        return "http://back_url/static/xxhdpi/background.png";
    }
    else
    {
        return "http://back_url/static/xxxhdpi/background.png";
    }
}

SetImageBackground class:

public class SetImageBackground extends AsyncTask<URL, Void, BitmapDrawable> {

    @Override
    protected BitmapDrawable doInBackground(URL... urls) {
        Bitmap bmp = null;
        try {
            bmp = BitmapFactory.decodeStream(urls[0].openConnection().getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        BitmapDrawable bitdraw = new BitmapDrawable(getResources(), bmp);
        return bitdraw;
    }

    @Override
    protected void onPostExecute(BitmapDrawable bitdraw){
        background = (CoordinatorLayout) findViewById(R.id.app_bar);
        background.setBackground(bitdraw);
    }
}

It works, but the problem with lags stays.

Why it could happends and what should I pay attention to in image (resolution, file extension) when i adding image to app, or how it do in correct way? May be i do it wrong?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Andrej Vilenskij
  • 487
  • 1
  • 7
  • 23
  • I am not sure if this could be a solution, but i think that you are looking for a way to lazy load it. Lazy loading means only loading the resources when it's really necessary (When the user is ready to see it). If you load images even when they are out of users view, you are wasting resources. [For this case, this question can help you](https://stackoverflow.com/questions/541966/how-to-lazy-load-of-images-in-listview-in-android) – Alexander Santos Nov 23 '19 at 16:27
  • Having a background image is usually not recommended because it slows down the app. An image of size 1280x1920px would take up approximately 76MB of memory. Maybe try using Glide or Picasso, but still having a background image would slow your app down. – Froyo Nov 23 '19 at 16:54
  • Maybe, try to use 9-patch. https://stackoverflow.com/a/43794864/891373 – Froyo Nov 23 '19 at 16:56
  • 1
    Note: `mipmap` is **ONLY** for the app icon, not for UI graphics. – Phantômaxx Nov 23 '19 at 18:42

0 Answers0