0

I have 4 images set as a background of screens in my application and displaying them cause lags in application. They have 1280x768px and about 70kb. How can I display them without loosing performance?

falsetto
  • 789
  • 2
  • 11
  • 35
  • Use image loader, like: https://github.com/square/picasso – questioner Apr 19 '15 at 16:14
  • So I guess I should use `new Target` in `into` method? but what next? use `setBackround` method from my view and convert received Bitmap into Drawable object? – falsetto Apr 19 '15 at 16:26
  • You can transform your main layout just a bit. Wrap your current main layout in `RelativeLayout` and add there and `ImageView` with `match_parent` as width and height and set Image Resources on this Image view. Second layout in RelativeLayout will be your current main view. This way your ImageView will act as background and second layout will be on top. – questioner Apr 19 '15 at 16:36
  • Ok, it works fine but it's 1sec of empty screen while Picasso is loading this image, how to avoid it? – falsetto Apr 19 '15 at 16:56
  • `Picasso` has possibility to load some temporary image (from resources for instance) that will be shown while real image is being loaded. You can use this solution. Otherwise - you can't do much more here. – questioner Apr 19 '15 at 17:01

2 Answers2

1

To use Picasso, simply add this to your gradle:

compile 'com.squareup.picasso:picasso:2.5.2'

And then, in case you have this image on our project folder:

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);

Instead, if you're downloading the image:

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Source: http://square.github.io/picasso/

Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • Yes it can work but what if I need to load it immediately? Should I load it while splash screen is displaying and then use this Bitmap in another screen? And as I asked earlier should I use method setBackground from my view and convert Bitmap to DrawableBitmap? – falsetto Apr 19 '15 at 16:36
  • If you want to load immediately then it's better to have your image saved in your project folder. If it's local, you shouldn't have a problem loading the image right when you start the screen containing it. I honestly don't know the details, I just know that Picasso loads the image in a very optimized way. Isn't your view an ImageView? – Teo Inke Apr 19 '15 at 16:56
  • My images are saved in my project and even though it's lagging. I used Picasso and it works fine but as I wrote in another comment it takes about 1 sec to load picture and it's a little bit annoying. – falsetto Apr 19 '15 at 16:59
  • Look for Picasso examples using fetch() to pre-load images then – Teo Inke Apr 19 '15 at 18:45
  • Can you explain how should I use it because I can't find any tutorial of using it? I'm guessing that I have to fetch image for example in my splash activity and then just load image using `into` in for example my fragment? – falsetto Apr 19 '15 at 19:45
  • I suppose you could do that. Tbh I haven't used fetch, but that's what I found after a quick search on pre loading images. If I find a good tutorial later I'll post it here – Teo Inke Apr 20 '15 at 14:05
  • Check this question: http://stackoverflow.com/questions/26282744/fetch-images-with-callback-in-picasso – Teo Inke Apr 21 '15 at 16:26
0

Check google's sample project here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59