intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp)图像偏离中心

Joh*_*nyO 3 android

在我的Android桌面上创建快捷方式时,我遇到了一些问题.

首先,我有一个72x72图标,我从SD卡加载到Bitmap对象.

使用该位图对象,我将其设置为我的图标资源.

我遇到的问题是当我设置它时,快捷方式上的图像偏离中心并被切断.从屏幕指标我得到72x72的大小,不知道交易是什么.

码:

Bitmap theBitmap = BitmapFactory.decodeFile("/sdcard/icon.png");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, theBitmap)
Run Code Online (Sandbox Code Playgroud)

我已经尝试过调整它并使用画布,可绘制和另一个位图使其工作,但是当重新启动手机时它恢复到一个小尺寸.

使用与可绘制资源相同的图标使其看起来很完美,但它不是动态的:

Parcelable iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
Run Code Online (Sandbox Code Playgroud)

使用三星Epic 4g w/2.1

Lio*_*ior 8

我有类似的问题.我查看了Launcher源代码,我发现如果初始位图大小太小,会出现导致图标无法正确显示的错误.

首先将位图缩放到128x128:

Bitmap scaledBitmap = Bitmap.createScaledBitmap(theBitmap, 128, 128, true);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
Run Code Online (Sandbox Code Playgroud)

它会解决它.


Wei*_*ung 5

虽然很久以前它是一个线程,但我发现了一个属性解决方案:

int size = (int) getResources().getDimension(android.R.dimen.app_icon_size);
installer.putExtra(Intent.EXTRA_SHORTCUT_ICON, Bitmap.createScaledBitmap(b, size, size, false));
Run Code Online (Sandbox Code Playgroud)