Astered Astered - 4 months ago 11
Android Question

android - scaling ImageView

I'm trying to do a launcher and I'm finding problems with the ListView that shows applications, because some apps have bigger icons and some others smaller ones.
So, I need your help to adjust size of icons in the ListView's activity.
There's the code:

private PackageManager manager;
private List<AppDetail> apps;
public void loadApps(){
manager = getPackageManager();
apps = new ArrayList<AppDetail>();

Intent i = new Intent (Intent.ACTION_MAIN, null);
i.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> aviableActivities = manager.queryIntentActivities(i, 0);
for(ResolveInfo ri:aviableActivities) {
AppDetail app = new AppDetail();
app.label = ri.loadLabel(manager);
app.name = ri.activityInfo.packageName;
app.icon = ri.activityInfo.loadIcon(manager);
apps.add(app);
}
}


It's not a XML problem, everything I have to change is in the icon receiving phase.

Answer

This is from a project I was working on sometime back. You can load the image as a Bitmap, resize it and then set it to the ImageView

public static Bitmap resizeBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // CREATE A MATRIX FOR THE MANIPULATION
    Matrix matrix = new Matrix();
    // RESIZE THE BIT MAP
    matrix.postScale(scaleWidth, scaleHeight);

    // "RECREATE" THE NEW BITMAP
    Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix, false);
    bm.recycle();
    return resizedBitmap;
    }

But before you do this, I would strongly recommend you scale down the sampling of the input images (as you probably can do with a lower image resolution) or you may be dealing with images using a lot of memory and quickly run into the OutOfMemoryError. The code for this is provided by Google.

Comments