Reputation: 723
Recently, while trying to load a image in imageView I ran into "Out of Heap Memory" error. So I looked on internet and found the way to optimise the images.
I am looking forward to finding the best way to optimise a bitmap.
Currently, I have an ImageView which has 200dp as hight and "match_parent" for width. Basically it fills the screen horizontally and takes 200dp vertically.
I am trying to achieve something like in this image.
Here is my master plan to do so.
1) Create a layout for 1 row (ImageView, TextView, Black Translucent bar behind text)
2) Use RecyclerView and inflate the data
Currently I am stuck on Step 1 (not satisfied with code).
What I did is create an XML layout for the row.
<RelativeLayout
android:id="@+id/layout_adventure_fragment"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/imageView_adventure_home_screen_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:background="@drawable/textview_background_gradient"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:id="@+id/textView_adventure_name_home_screen_fragment"
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="Demo Text"/>
</RelativeLayout>
I'll adjust TextView as per Typography guidelines later.
Ok, so basically ImageView is 200dp tall. To compress my images I used the guide at Android training page which uses inSampleSize and loads image in an AsyncTask.
To calculate the required height I used something I am not sure if it is correct way or not. Since I care to compress image only for height I passed only height to calculate inSampleSize.
DisplayMetrics disp = getApplicationContext().getResources().getDisplayMetrics();
int density = disp.densityDpi;
width = disp.widthPixels;
height = (int)((float)(200.0/160.0) * (float)density);
Bitmap b =decodeSampledBitmapFromResource(getResources(),R.drawable.shake,height);
loadBitmap(R.drawable.shake,iV);
Finally I extracted thumbnail from the final bitmap for the required width and height to show user an image which fills the imageView instead of being just in the centre with margins.
imageView.setImageBitmap(ThumbnailUtils.extractThumbnail(bitmap,width,height));
Full code can be found here : http://pastebin.com/mhKi1sKd
What is the best way to optimise the imageView so that in case multiple images are shown in a RecyclerView, Heap memory does not get full and also if there are unnecessary codes in my program that simply take up processing and valuable time?
This is what I have achieved till now.
Upvotes: 0
Views: 134
Reputation: 25028
Have a look at Picasso
library.
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching.
Upvotes: 1