【问题标题】:Android shortcut bitmap launcher icon sizeAndroid 快捷位图启动器图标大小
【发布时间】:2013-09-09 05:13:16
【问题描述】:

我无法为我的快捷方式找出正确的启动器图标大小。

在我的 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);

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

【问题讨论】:

    标签: android bitmap icons shortcut launcher


    【解决方案1】:

    我找到了一个可行的解决方案:

    @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;
    

    【讨论】:

    • 这真是很酷的东西。对于我对旧机器人的测试,我实际上不得不稍微改变你的代码。无论如何都会以您的方式调用getLauncherLargeIconSize() 并引发异常。我将所有这些都放在了一个额外的方法中,并在 SDK
    • 你确定吗?在我看来,如果 Build.VERSION.SDK_INT
    • 是的,当然。我用模拟器测试过。让我们看看我是否可以编辑您的答案并添加我的代码。
    • 我在上面编辑了我的代码并将 API-level-11 放在一个额外的方法中
    【解决方案2】:

    如果您注意到在 android res 文件夹中 - 应该有几个可绘制文件夹

    drawable hdpi、drawable mdpi、drawable ldpi等 每个文件夹对每个设备都有指定的图片,因为每个设备都是按照屏幕密度来显示图片的,有的设备密度低,有的设备密度高

    这就是为什么在一台设备上你的图标大小是 96 像素,而在另一台设备上是 72

    较旧和较小的设备甚至是 48 和 36

    为了解决这个问题,你只需要用合适大小的图标文件填充适当的可绘制文件夹,然后你就不会遇到问题了

    欲了解更多信息,请阅读:Supporting multi screens in android

    【讨论】:

    • 在这种情况下,我不能使用可绘制文件夹,因为我从布局 xml 文件创建了位图。上面的代码不完整。膨胀 xml 后,我在布局内编辑 ImageView 和 TextView。我现在在代码中添加了注释。
    【解决方案3】:

    获得正确大小的启动器图标似乎并不像人们想象的那么简单。我知道平板电脑至少会在 drawables 文件夹中上一个文件夹来显示比屏幕密度预期的更大的图标。

    这是更多discussed

    【讨论】:

      猜你喜欢
      • 2012-09-27
      • 2015-07-08
      • 1970-01-01
      • 2014-12-04
      • 2018-05-25
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      相关资源
      最近更新 更多