我有問題需要爲我的快捷方式找到正確的啓動器圖標大小。Android快捷方式位圖啓動器圖標大小
在我的Nexus 7.2上,android.R.dimen.app_icon_size(請參閱代碼)的值爲96像素。 但是,如果我在主屏幕的屏幕截圖上測量其他應用程序的實際圖標大小,則它是120像素。創建短之後,它是比所有其他應用程序圖標(120px)更小(96像素)
在我的三星Galaxy SII android.R.dimen.app_icon_size是72.和這匹配我的屏幕截圖的措施。
這裏
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
三星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();
在這種情況下,我不能使用可繪製文件夾,因爲我創建佈局的xml位圖文件。上面的代碼不完整。在膨脹xml之後,我在佈局中編輯ImageView和TextView。我現在在代碼中添加了一條評論。 – almisoft