My Application is a Launcher, I have probleme to get the good size of icon, i tried to use the list of icon and to set the right size. but with devices that have an xxhdpi, my application carsh. the Class used to get Application information
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Formatter.BigDecimalLayoutForm;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Adapter used for providing the applications to show on the launcher's basic
* view.
*
*/
public class GridViewAppInfoAdapter extends BaseAdapter {
private Rect mOldBounds = new Rect();
private List<ApplicationInfo> apps;
private Context context;
private static final String TAG = "GridViewAppInfoAdapter";
private static int lowVal;
private static int medVal;
private static int highVal;
private static int xhighVal;
private static int xxhighVal;
public static ArrayList<String> lApplication = new ArrayList<String>();
public static ArrayList<String> AppList = new ArrayList<String>();
public GridViewAppInfoAdapter(Context context, List<ApplicationInfo> apps) {
super();
this.context = context;
this.apps = apps;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Log.e("GridView", "getView");
final ApplicationInfo info = apps.get(position);
if (convertView == null) {
final LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.grid_item, parent, false);
}
Drawable icon = info.icon;
ImageView imageView = new ImageView(context);
if (!info.filtered) {
final Resources resources = context.getResources();
DisplayMetrics highmetrics = new DisplayMetrics();
highmetrics = resources.getDisplayMetrics();
int width = (int) resources
.getDimension(android.R.dimen.app_icon_size);
int height = (int) resources
.getDimension(android.R.dimen.app_icon_size);
final int iconWidth = icon.getIntrinsicWidth();
final int iconHeight = icon.getIntrinsicHeight();
if (icon instanceof PaintDrawable) {
PaintDrawable painter = (PaintDrawable) icon;
painter.setIntrinsicWidth(width);
painter.setIntrinsicHeight(height);
}
// if (width > 0 && height > 0
// && (width < iconWidth || height < iconHeight))
if (width > 0 && height > 0) {
final float ratio = (float) iconWidth / iconHeight;
if (iconWidth > iconHeight) {
height = (int) (width / ratio);
} else if (iconHeight > iconWidth) {
width = (int) (height * ratio);
}
if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
lowVal = 65;
medVal = 90;
highaVal = 130;
xhighVal = 180;
xxhighVal = 280;
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
lowVal = 36;
medVal = 48;
highVal = 72;
xhighVal = 96;
xxhighVal = 144;
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
lowVal = 36;
medVal = 48;
highVal = 130;
xhighVal = 173;
xxhighVal = 600;
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
}
switch (highmetrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
imageView.setLayoutParams(new GridView.LayoutParams(lowVal,
lowVal));
height = lowVal;
width = lowVal;
break;
case DisplayMetrics.DENSITY_MEDIUM:
imageView.setLayoutParams(new GridView.LayoutParams(medVal,
medVal));
height = medVal;
width = medVal;
break;
case DisplayMetrics.DENSITY_HIGH:
imageView.setLayoutParams(new GridView.LayoutParams(
highVal, highVal));
height = highVal;
width = highVal;
break;
case DisplayMetrics.DENSITY_XHIGH:
imageView.setLayoutParams(new GridView.LayoutParams(
xhighVal, xhighVal));
height = xhighVal;
width = xhighVal;
break;
case DisplayMetrics.DENSITY_XXHIGH:
imageView.setLayoutParams(new GridView.LayoutParams(
xxhighVal, xxhighVal));
height = xxhighVal;
width = xxhighVal;
break;
default:
break;
}
// System.gc();
final Bitmap.Config c = icon.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
Bitmap thumb;
try {
thumb = Bitmap.createBitmap(width, height, c);
} catch (Error e) {
thumb = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_4444);
}
final Canvas canvas = new Canvas(thumb);
canvas.setDrawFilter(new PaintFlagsDrawFilter(
Paint.DITHER_FLAG, 0));
// Copy the old bounds to restore them later
// If we were to do oldBounds = icon.getBounds(),
// the call to setBounds() that follows would
// change the same instance and we would lose the
// old bounds
mOldBounds.set(icon.getBounds());
icon.setBounds(
context.getResources().getInteger(
R.integer.image_padding),
context.getResources().getInteger(
R.integer.image_padding), width, height);
icon.draw(canvas);
icon.setBounds(mOldBounds);
icon = info.icon = new BitmapDrawable(thumb);
info.filtered = true;
}
}
final TextView textView = (TextView) convertView
.findViewById(R.id.label);
textView.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
textView.setText(info.title);
textView.setLines(2);
lApplication.add((info.title).toString());
return convertView;
}
@Override
public int getCount() {
// supprime les répétitions
List apps2 = new ArrayList();
Set set = new HashSet();
for (Object item : apps) {
if (!set.contains(item)) {
apps2.add(item);
set.add(item);
}
}
// Log.e("apps", "/" + set.size());
return set.size();
}
@Override
public Object getItem(int position) {
return apps.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public void setApps(List<ApplicationInfo> apps) {
this.apps = apps;
for (int i = 0; i < apps.size(); i++) {
AppList.add(apps.get(i).title.toString());
}
notifyDataSetChanged();
}
public ArrayList<String> getApplicationList() {
ArrayList<String> AppList = new ArrayList<String>();
for (int i = 0; i < apps.size(); i++) {
AppList.add(apps.get(i).title.toString());
}
return AppList;
}
}
I have a probleme of memory with XXHDPI the log is given below
4
: E/AndroidRuntime(11857): FATAL EXCEPTION: main
07-15 18:32:40.124: E/AndroidRuntime(11857): java.lang.OutOfMemoryError
07-15 18:32:40.124: E/AndroidRuntime(11857): at android.graphics.Bitmap.nativeCreate(Native Method)
07-15 18:32:40.124: E/AndroidRuntime(11857): at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
07-15 18:32:40.124: E/AndroidRuntime(11857): a
I don't know what to do.