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?