简体   繁体   中英

Android shortcut bitmap launcher icon size

I have problems to find out the correct launcher icon size for my shortcut.

On my Nexus 7.2 the value of android.R.dimen.app_icon_size (see code) is 96 pixels. But if I measure the real icon size of other apps on a screenshot of my homescreen, it is 120 pixels. After creating a shortshut, it is smaller (96 px) than all other app icons (120px)

On my Samsung Galaxy SII android.R.dimen.app_icon_size is 72. And this match to my screenshot measure.

Here the result of

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

Nexus 7.2:

android.R.dimen.app_icon_size = 96
metrics.dip = 192
metrics.density = 2.0
metrics.densityDpi = 320
metrics.heightPixels = 1824
metrics.scaledDensity = 2.0
metrics.widthPixels = 1200
metrics.xdpi = 320.842
metrics.ydpi = 322.966

Samsung SII:

android.R.dimen.app_icon_size = 72
metrics.dip = 108
metrics.density = 1.5
metrics.densityDpi = 240
metrics.heightPixels = 800
metrics.scaledDensity = 1.5
metrics.widthPixels = 480
metrics.xdpi = 217.71428
metrics.ydpi = 218.49463

Here my code:

// Calculate launcher icon size
int size = (int) getResources().getDimension(android.R.dimen.app_icon_size);
int width = size;
int height = size;

// Create launcher icon bitmap      
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

// Inflate layout to bitmap
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.shortcut, null, false);
// here I edit layout, change ImageView and TextView etc...
layout.setDrawingCacheEnabled(true);
layout.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());

// Create SendFax intent
Intent shortcutIntent = new Intent();
shortcutIntent.setClass(context, SendFax.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

// Create shortcut intent               
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, description);

setResult(RESULT_OK, intent);
finish();

I have found out a working solution:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private int launcherLargeIconSize() {
    ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    return activityManager.getLauncherLargeIconSize();
}

int size1 = (int) getResources().getDimension(android.R.dimen.app_icon_size);
int size2 = Build.VERSION.SDK_INT >= 11 ? launcherLargeIconSize() : 0;
int size = size2 > size1 ? size2 : size1;

If you notice in the android res folder - there should be several drawable folders

drawable hdpi , drawable mdpi, drawable ldpi and so on each folder has designated images for each device, because each device shows images according to screen density, some devices have low density, and some have high density

which is why on one device your icon size is 96 px, and on another it is 72

the older and smaller devices are even 48 and 36

in order to fix the problem, you just have to populate the appropriate drawable folders, with the right size icon file, and then you wont have a problem

for more info, read this : Supporting multi screens in android

Getting the right size of a launcher icon seems to be not as straight forward as one might expect. I know that at least tablets go one folder up in the drawables folder to show larger icons then expected by the screen density.

Here is this a bit more discussed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM