ListView items. Handle images with huge height - android

I have listview items with simple View inside, and i need to display image with huge height in it. I have cache system which can split large image into smaller ones.
Question:
What is the best way to handle displaying large images in one listview item?
Sure i can add some views to item at runtime(10 view 1000px height for example), but i think that i will get out of memory.
My point is to make my app display image like 9gag app.
9gag app
9gag view hierarchy

Do not load large bitmaps to memory consider loading a smaller version into memory, set inSampleSize to true in your BitmapFactory.Options object. For example, an image with resolution 2048x1536 that is decoded with an inSampleSize of 4 produces a bitmap of approximately 512x384.
Use these common guidelines for loading large bitmaps

Related

Android : Assigning a high resolution image to a smaller ImageView

I have this unanswered question of mine pertaining to images. I have an image of about 1 MB and I am assigning the images to an image view with width and height of 100dp and 80dp respectively. But when I assign the image to about 5 image views, the application crashes with Out of Memory Error
So, when I assign the 1 MB image to such a small image view, what happens? Doesn't android scale it down? Also, did the app crash because it couldn't take the load of 5MB in memory??
EDIT
I have 5 ImageViews on the screen. I am assigning the image to all if them this way
myImageView.setImageURI(item.getImageUri());
Android loads the whole image file to the memory and then scales it down. In order to avoid OOM you should load large bitmaps efficiently.

40+ ImageButtons on one screen?

For last 10+ hours I try to get a large (40+) amount of images (in ImageButton format) on a single Android screen without out of memory errors. The activity I work on is an image picker for a coloring book app. Images are of various sizes in the range of (500 to 1200)x(500 to 1200), PNGs (if that matters).
I have tried:
Horizontal Scroll View with images added from the code. The result is slow (I do it on UI thread) and consumes large memory space.
Horizontal Scroll View with images added from the code via AsyncThread. The result is fast but still consumes large memory space. I like the user experience of this one the most!
Grid View and List View - both are very choppy (testing on first Nexus 7). Memory usage is better.
What I am considering
View Pager - first results look better than Grid View from performance perspective (I have not completed it to the moment to assess the memory usage but it should be better from what I understand). Yet, I dislike the user experience and prefer a scrollable list of all images.
Conversion of my resources to JPG (will that get rid of Transparency byte?)
Downsizing the images to max 500x500px
None of the solutions seems as good as the Android Photo Gallery app available on all devices. This experience is what I would love to mirror. No idea how this is done though :(
Two questions.
What is the best way to get such thing (40+ Images scrollable on single screen) done? Is it really ViewPager? ScrollView with AsyncTask and well thought images resolution? Something I have not tried yet?
What is the memory limit I should try to keep below? Looking at Android heap size on different phones/devices and OS versions it seems to be 256 MB, is that fair assumption?
Thanks. Have a great weekend!
PS. On iOS all works like charm when I add all the buttons into a scroll view :(
Some basic calculations reveals your problem:
40+ images at 1200x1200 = approx 57MB, the comments are correct you need to subsample (i.e. thumbnail) the images or use a library such as the Universal Image Loader. Converting to JPG doesn't matter. That is a compressed storage format, the memory required to display the pixels remains the same.
There is a Android Developers article on Loading Large Bitmaps Efficiently with sample code. The following steps are covered in more detail in the article Android Bitmap Loading for Efficient Memory Usage.
The basic steps in loading a large bitmap are:
Determine the required size (from a known size or determining the
size of the target View).
Use the BitmapFactory class to get the bitmap’s size (set inJustDecodeBounds in BitmapFactory.Options to true).
Calculate the subsampling value and pass it to the BitmapFactory.Options setting inSampleSize.
Use the BitmapFactory to load a reduced size bitmap.

2 issues with android bitmaps

I have a ListView, with about 50 rows. Each item has an image. Sometimes, the images are downloaded, but sometimes, I just show a specific image from app resources.
Issues:
Sometimes I just show about 20 images from resources for my items
that all of the are the same image .it means I just show an image
for 20 rows that all the same. is it loading a bitmap an take the
amount of ram for each item ? or it just load an image and show it
for other items ?
The imageview's size for each item is 100*70 dp . so I want to do
something , like re sizing or reducing the image dp before I bitmap
them and show them in my app, so I can reduce the amount of ram they
takes . Is it possible ?
Thanks
Showing same image multiple times
If you set all the image as same Drawable (variable), you got from resources, Only 1 bitmap Drawable resides in RAM. Also, ListView recycles its rows, it does not really create 20 rows with all 20 Bitmaps loaded in RAM.
Setting DPI of images
BitmapDrawable class can automatically decide DPI for rendering, that's why it asks for Resources in its constructor. Or you can call setTargetDensity(int) later. NOTE: Changing DPI is not exactly same thing as Scaling an Image.
Loading scaled down images
This will definitely save RAM, Read Answers on how to load scaled down images.

Android ListView image resize

I have a lazy-loading ListView populated with images gotten over the network. The list rows are set to wrap_content, so once the image loads it resizes and the full scale image will be displayed. It looks great when scrolling down, but when scrolling up the rows resize and force the bottom rows off the screen. How can I prevent this jumpy scrolling while scrolling up?
----- EDIT:
The images are comics of varying sizes. Some are 2 or 3 frames where they aren't very tall. Others are single frame comics where they are much taller. The image needs to take up the full width and the height should not cut off any of the comic.
Assuming all images downloaded are expected to be around the same size, a good solution most developers use is to use a "dummy" image until the real image is loaded. This image will exist locally so it can be loaded almost instantaneously. In the getView method, show this dummy image until the real image is downloaded, and then simply replace it. This will prevent your rows from resizing.
you have to see this link It downloads images in the background thread. Images are being cached on SD card and in memory, It decode images with inSampleSize to reduce memory consumption and also try to handle recycled views correctly. Play a fake image when image are not completely downloaded. "sorry fo the music!!"
I figured out the solution. When I receive the image, I get the width from the parent then set the height of the image view to parentWidth * bitmapHeight / bitmapWidth. That way the resizing occurs as the row's view is created and the list doesn't jump around as much once I know the size of the bitmap.

Resizing Images in Android for use in a listview

I have a bunch of "large" 400x400 images that I am pulling from a source on the web and I need to display as 100x100 thumbnails in a ListView on Android. If I simply set the dimensions on the imageview element in xml it is very clear it is only scrunching the images down to 100x100 for 2 reasons:
1) The images don't look resampled
2) When given more than lets say 10 of these images in the list the scrolling becomes unbearably slow
I am not AS worried about #1 but the scrolling needs to stay smooth. So I am wondering, how should I go about resizing/resampling these images and caching them? I've done some googling but I just can't find a good resource for what would be a best practice in this situation.
Thanks in advance!
Take a look at BitmapFactory and the Options class. Options has an inSampleSize value that you can set to scale down an image when you read it in. You could try something like
Options op = new Options();
op.inSampleSize = 4;//You want to scale from 400 to 100
Bitmap thumbnail = BitmapFactory.decodeFile(fileName, op);
Just set android:scaleType="fitCenter" and your image will be scaled down to your desired size.

Categories

Resources