2013-09-05 50 views
2

我有问题需要为我的快捷方式找到正确的启动器图标大小。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(); 

回答

0

如果您在Android res文件夹注意 - 应该有几个绘制文件夹

绘制华电国际,绘制MDPI,绘制LDPI并为每个设备等等 每个文件夹已指定的图像,因为每个设备展示根据屏幕密度的图像,一些设备具有低的密度,和一些具有高密度

这就是为什么一个设备上的图标的大小为96像素,并在另一个则是72

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

为了解决这个问题,你只需要填写相应的绘制文件夹,用大小合适的图标文件,然后你不会有问题

更多的信息,请阅读本:Supporting multi screens in android

+0

在这种情况下,我不能使用可绘制文件夹,因为我创建布局的xml位图文件。上面的代码不完整。在膨胀xml之后,我在布局中编辑ImageView和TextView。我现在在代码中添加了一条评论。 – almisoft

0

获得正确尺寸的发射器图标似乎并不像人们所期望的那样直截了当。我知道,至少平板电脑会将一个文件夹放在drawables文件夹中,以显示屏幕密度所期望的较大图标。

下面是该多一点discussed

6

我已经找到了一个有效的解决方案:

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

这真的很酷的东西。对于我用旧的机器人测试,我实际上不得不改变你的代码。在你的方式'getLauncherLargeIconSize()'将被调用,并抛出异常。我已经把所有这些放在了一个额外的方法中,并在SDK <11时返回。 – jboi

+0

你确定吗?在我看来,如果Build.VERSION.SDK_INT <11,getLauncherLargeIconSize()不会被调用。我的代码适用于我的Samsung Galaxy SII,Android 2.3.6。但是,如果您的最小API级别<11,请将@SuppressLint(“NewApi”)添加到该方法。当然,耶普, – almisoft

+0

。我用模拟器测试了它。让我们看看我是否可以编辑你的答案并添加我的代码。 – jboi