Android shortcut bitmap launcher icon size

天大地大妈咪最大 提交于 2019-12-02 05:26:35

问题


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();

回答1:


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;



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/18635135/android-shortcut-bitmap-launcher-icon-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!