Drawable VS Bitmap in a specific case - android

I'm developing a music Player, and since i have started I've a question: Should i use Drawable or Bitmap?
Considered that:
I have to load many images, one for every song (The image is loaded only when it is the turn of that song).
The image loaded has to be visualized on the Widget and Notification too.
The same image is displayed on more activities.
The image loading has to be as fast as possible.
The memory usage have to be as low as possible, even after have loaded several images.
I was thinking to temporarly save images of actual playlist, so I don't have to reload it every time is played the same song.
I am using Glide library to have better performance in this moment, and I am working with bitmap. Any suggestion?

Both graphics objects works almost everywhere.
If you loading from file system (downloads) you need to load Bitmaps to create BitmapDrawables so you may apply bitmap directly in this case.
If you are using from the res folder you can load as Drawables.

Drawable is just a wrapper for things that can be drown (colors, vector images, layer-lists, selectors and bitmaps).
Whereas bitmaps are actually representation of images in Android.
So, your question is invalid, we can not equate bitmap and Drawable.

Related

What's the best way to place multiple images onto the screen simultaneously?

So my app needs to have a maximum of 50 images on the screen at the same time in a FrameView, they need to be placed on top of a base image. If I put these images into drawable, the app crashes after a few images are placed with an OutOfMemoryError. But it works when I put the images in mipmap for some reason without crashing. So I placed them all into the mipmap folders. The images are VERY small too, the largest among them is 2.2 KB and the smallest is 398 bytes. The app works as intended, but performance is horrible after you place a few images onto the screen. The first few images will load quickly, but as you continue to place images onto the screen it gets progressively slower to the point where it may take multiple seconds to place the image onto the screen. I'm just using an ArrayList to put all the Drawables in, and then another ArrayList to put all the ImageViews and I load them into the view within my onTouchListener. Here's an idea. The index is used to decide which image is to be inserted:
drawableOverlays.set(index, getResources().getDrawable(R.mipmap.exampleImage, null));
imageOverlays.get(index).setImageDrawable(drawableOverlays.get(index));
frame.addView(imageOverlays.get(index));
I've also tried using Glide and Picasso and those loaded in even slower. Am I approaching this wrong? Is there a more efficient way to accomplish this?
The best way is create a RecyclerView with ImageViews as items, and then load the images with Glide or Picasso, any of those libraries will give you the best performance

Large images as background in Android

I would like to create fragment with image as background. What I do is saving image in .png format, putting it to drawable folder and than using it as background in layout. First thing I noticed is that the view stutters, so I created different images for different densities. But I'm still afraid that in older devices with worser CPU view would statter.
Question
Do I have to use libraries like Picasso for adding images as background ? What is the best approach ?
I think that base approach of using libraries like Picasso, Glide, UniversalImageLoader is for downloading images from network, caching, decoding, smooth loading in runtime.
If you want just set background for imageView from your local resources the only thing you should consider is to do this efficiently. This is nice described by google here and should be enough for your case.

Why are my images in the layout load slower than texts in Android?

I mean, that the TextViews loads almost at once, however the ImageViews loads a bit slower, and this looks very ugly, and disturbing.
What can I do against it?
I load the images of the ImageView on the fly, and set the images of the ImageViews with setImageBitmap() in a background process.
setImageBitmap is time-consuming - it requires loading bitmap data from storage, memory allocation, decoding, etc, etc. A TextView obviously needs to do no such thing.
You can improve the situation somewhat by using placeholder images of the same size as the image that you're about to load, using transition animations, or (whenever possible) using resource drawables.

Loading tall images from url in slivers preventing whole image from loading into ram

I am looking for a library or some idea on how I can load an image by parts from the disk or a url straight to the disk then in parts again to the ram. So the two ways I see that this can be done is loading the whole image onto the disk by reading and writing it from the url directly using the ram only for the buffer then when the image is on the disk some how creating bitmaps of only parts of the image, that way I DO NOT load it all and putting those in a ListView.
The issue is that I am dealing with extremely long images (10K + pixels long w/ a width of 4-600) and they hog up lots of ram if loaded all in one bitmap. I can not just scale them down like the Google android tutorial does in the handling large bitmaps section as that results in a width too small to deal with. So if I can somehow generate small bitmap slivers on the disk I can use them by loading them in a ListView preventing loading the image as a whole into ram.
Right now I am breaking the long images into slivers from a bitmap and I realized that that isn't really accomplishing what I am trying to do as the whole image is loaded into a bitmap in memory and is then broken up, then GC (So I am using up the full abmount of ram anyways). I am testing on a new top of the line android phone and the app works fine, but the heap size reaches 80mb+ with the larger images temporarily in ram as it breaks down the bitmap and this will be an issue with devices that have lower heap limits
You can try using this class, support from 2.3 http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html
If you are using Java you can work with InputStreamReader and OutputStreamWriter. Method read() accepts the buffer as one of the parameters, its length can be anything suitable. Of course you can create a new file for each buffer being written.
Is it what you're looking for?
edit
well its not. have you seen this Strange out of memory issue while loading an image to a Bitmap object ?
If you have control over the server from where you are fetching data, throw in another field in your response, such that it returns a thumbnail/smaller image. The server can then generate the required thumbnails for you, without you bothering about it.
Decoding bitmaps on the fly might be expensive, most of the times. If you can't change anything on the server, download and save the images, and after saving, generate their corresponding thumbnails and save them as well. In your list, use the thumbnails. Also, save the information about which images have been cached, and whose thumbnails you already have. This might look like a lot of work, but depending on the use case, this can be a better approach dealing with large images.
Problem with downsizing?
Well, you can come up with some kind of logic as to generate thumbnails, based on the original size of the image. For longer(vertically long images), you could use BitmpapRegionDecoder (http://developer.android.com/reference/android/graphics/BitmapRegionDecoder.html) as #Binh Tran has suggested.
Try maybe encoding the image to make the size small.

How to implement a lazy loaded list of images in a listview

What i am trying to do is toscroll through a list of images. But when i scroll down or up quickly the images are misplaced in order. A diff pic comes in the place where it was supposed to be. I have tried setting the imageview to null drawable from my imageloader library to clear any rpevious images if any. But the issue persists. What should i do
You can try to use Universal image loader
This is for listView optimization and improving performance if your listView contain images from web.
Features:
Multithread image loading Possibility of wide tuning ImageLoader's
configuration (thread executors, downlaoder, decoder, memory and disc
cache, display image options, and others) Possibility of image
caching in memory and/or on device's file sysytem (or SD card)
Possibility to "listen" loading process Possibility to customize
every display image call with separated options Widget support
Finally discovered the culprit. I was using the imageview.bringtofront() function which was causing the problem for some unknown reason. I wanted to put a part of the imageview infront of the other and was using the relative layout & bring to front. Now i removed the bringtofront() and rearranged the imageview in relativelayout so that its already in the front. Resolved. Thank you all

Categories

Resources