我无法为我的快捷方式找出正确的启动器图标大小。
在我的 Nexus 7.2 上,android.R.dimen.app_icon_size(见代码)的值为 96 像素。但是如果我在我的主屏幕截图上测量其他应用程序的真实图标大小,它是 120 像素。创建快捷方式后,它比所有其他应用程序图标 (120 像素) 小 (96 像素)
在我的三星 Galaxy SII 上,android.R.dimen.app_icon_size 是 72。这与我的屏幕截图测量相匹配。
这里的结果
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
连结 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
三星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
这是我的代码:
// 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();