[英]How to make a round icon for shortcut
我正在嘗試將 Android 快捷方式添加到應用程序中,包括將從位圖創建的動態快捷方式和圖標。 現在它看起來像這樣:
如您所見,動態快捷方式圖標在中心有一個方形圖像,但我需要它占據圖標的所有空間,因此不會有白色背景。 編碼:
Bitmap interlocutorAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_conference);
ShortcutInfo shortcutInfo = new ShortcutInfo.Builder(context, peer.getId())
.setLongLabel("Dynamic shortcut")
.setShortLabel("Dynamic")
.setIcon(Icon.createWithBitmap(interlocutorAvatar))
.setIntent(new Intent(Intent.ACTION_VIEW).setClass(context, VCEngine.appInfo().getActivity(ActivitySwitcher.ActivityType.CHAT))
.putExtra(CustomIntent.EXTRA_PEER_ID, peer.getId())
.putExtra(CustomIntent.EXTRA_CHAT_ID, peer.getId()))
.build();
添加到正在加載 xml 文件中的圖像的 imageView
android:scaleType="centerCrop"
我想我找到了一種可能的解決方案,那就是使用自適應圖標。 對我來說它看起來有點奇怪,但是嘿,只要它有效。 我使用了 AdaptiveIconDrawable,這是如何做到的:
將位圖轉換為自適應位圖的代碼:
@RequiresApi(api = Build.VERSION_CODES.O)
public static Bitmap convertBitmapToAdaptive(Bitmap bitmap, Context context) {
Drawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
AdaptiveIconDrawable drawableIcon = new AdaptiveIconDrawable(bitmapDrawable, bitmapDrawable);
Bitmap result = Bitmap.createBitmap(drawableIcon.getIntrinsicWidth(), drawableIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
drawableIcon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawableIcon.draw(canvas);
return result;
}
然后你可以像這樣設置快捷方式的圖標:
setIcon(Icon.createWithAdaptiveBitmap(convertBitmapToAdaptive(yourBitmap, context)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.