Created
September 3, 2013 09:18
-
-
Save Phonbopit/6421526 to your computer and use it in GitHub Desktop.
Android - ImageAdapter for avoid out of memory.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.Resources; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.GridView; | |
import android.widget.ImageView; | |
public class ImageAdapter extends BaseAdapter { | |
private Context mContext; | |
// Keep all Images in array | |
public Integer[] mThumbIds = { | |
R.drawable.pic1, R.drawable.pic2, | |
R.drawable.pic3, R.drawable.pic4, | |
R.drawable.pic5, R.drawable.pic6, | |
R.drawable.pic7, R.drawable.pic8, | |
R.drawable.pic9, R.drawable.pic10, | |
R.drawable.pic11, R.drawable.pic12, | |
R.drawable.pic13, R.drawable.pic14, | |
R.drawable.pic15, R.drawable.pic16, | |
R.drawable.pic17, R.drawable.pic18, | |
R.drawable.pic19 | |
}; | |
// Constructor | |
public ImageAdapter(Context c){ | |
mContext = c; | |
} | |
@Override | |
public int getCount() { | |
return mThumbIds.length; | |
} | |
@Override | |
public Object getItem(int position) { | |
return mThumbIds[position]; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ImageView imageView = new ImageView(mContext); | |
// imageView.setImageResource(mThumbIds[position]); | |
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); | |
imageView.setImageBitmap(decodeSampledBitmapFromResource(mContext.getResources(), mThumbIds[position], 100, 100)); | |
imageView.setLayoutParams(new GridView.LayoutParams(70, 70)); | |
return imageView; | |
} | |
private Bitmap decodeSampledBitmapFromResource(Resources res, int resId, | |
int reqWidth, int reqHeight) { | |
// First decode with inJustDecodeBounds=true to check dimensions | |
final BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inJustDecodeBounds = true; | |
BitmapFactory.decodeResource(res, resId, options); | |
// Calculate inSampleSize | |
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); | |
// Decode bitmap with inSampleSize set | |
options.inJustDecodeBounds = false; | |
return BitmapFactory.decodeResource(res, resId, options); | |
} | |
private int calculateInSampleSize( | |
BitmapFactory.Options options, int reqWidth, int reqHeight) { | |
// Raw height and width of image | |
final int height = options.outHeight; | |
final int width = options.outWidth; | |
int inSampleSize = 1; | |
if (height > reqHeight || width > reqWidth) { | |
// Calculate ratios of height and width to requested height and width | |
final int heightRatio = Math.round((float) height / (float) reqHeight); | |
final int widthRatio = Math.round((float) width / (float) reqWidth); | |
// Choose the smallest ratio as inSampleSize value, this will guarantee | |
// a final image with both dimensions larger than or equal to the | |
// requested height and width. | |
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; | |
} | |
return inSampleSize; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment