如何使用IconCompat.createWithAdaptiveBitmap
How to use IconCompat.createWithAdaptiveBitmap
我想将自适应图标应用到由以下代码生成的快捷方式(代码仅供参考,不编译):
ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, "com.myapp.mainactivity")
.setShortLabel(context.getResources().getString("short label"))
.setLongLabel(context.getResources().getString("long label"))
.setIcon(IconCompat.createWithAdaptiveBitmap(bitmap)
.setIntent(targetActivityIntent)
.build();
public Bitmap getLauncherIcon(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return null;
} else {
Drawable backgroundDrawable = context.getDrawable(R.mipmap.ic_launcher_background);
Drawable vectorDrawable = context.getDrawable(R.drawable.ic_launcher_main_foreground);
AdaptiveIconDrawable adaptiveIconDrawable = new AdaptiveIconDrawable(backgroundDrawable, vectorDrawable);
Bitmap bitmap = drawableToBitmap(adaptiveIconDrawable);
return bitmap;
}
}
private static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
这是我的图标 (ic_main_white.xml)
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="45dp"
android:height="45dp"
android:viewportHeight="1024"
android:viewportWidth="1024">
<path
android:fillColor="#FFFFFF"
android:pathData="M511.5 74C269.87847 74 74 269.87847 74 511.5 74 753.13368 269.87847 949 511.5 949 753.12153 949 949 753.13368 949 511.5 949 269.87847 753.12153 74 511.5 74l0 0 0 0 0 0zm30.10243 764.20312l0 -140.08506 -57.72569 0 0 140.27951C325.65972 825.12674 199.05208 699.1875 184.78472 541.25l142.21181 0 0 -57.72569 -142.34549 0C198.12847 324.77257 325.07639 197.95833 483.87674 184.63889l0 142.00521 57.72569 0 0 -141.81077c157.64583 14.40104 283.32986 140.76563 296.74653 298.69098l-139.87847 0 0 57.72569 139.74479 0C824.02083 698.34896 698.66493 823.85069 541.60243 838.20312l0 0z"/>
</vector>
使用 Android 资产生成器我创建了一个图像资产,我在其中生成了所有需要的文件。这与您为应用程序的启动器图标创建自适应图标时要经历的过程相同。
但结果只显示了图标的一部分,看起来生成的图标对于预期的来说太大了,或者我在某处遗漏了一些东西。
还有 this 文档,其中说明如下:
For dynamic shortcuts, call the createWithAdaptiveBitmap() method when
you create them.
图标由 Android Studio 本身生成,这也适用于我的启动器图标,但为什么使用 createWithAdaptiveBitmap 无法正常工作?我是否遗漏了一些 java 代码,或者您是否知道使用带有快捷方式的自适应图标的示例,但我找不到任何适用于 GitHub 或其他地方的代码。
您需要的解决方案:
当您在 Android Studio 中使用资产管理器生成图标时,请确保图标位于 "secure area" 内,资产具有正确的大小非常重要(参见 https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive)
位图转换是关键:
private static Bitmap drawableToBitmap(final Drawable drawable, final Context context) {
final float screenDensity = context.getResources().getDisplayMetrics().density;
final int adaptiveIconSize = Math.round(ADAPTIVE_ICON_SIZE_DP * screenDensity);
final int adaptiveIconOuterSides = Math.round(ADAPTIVE_ICON_OUTER_SIDES_DP * screenDensity);
final Bitmap bitmap = Bitmap.createBitmap(adaptiveIconSize, adaptiveIconSize, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
drawable.setBounds(adaptiveIconOuterSides, adaptiveIconOuterSides, adaptiveIconSize - adaptiveIconOuterSides,
adaptiveIconSize - adaptiveIconOuterSides);
drawable.draw(canvas);
return bitmap;
}
这是我使用的:
fun convertAppIconDrawableToBitmap(context: Context, drawable: Drawable): Bitmap {
if (drawable is BitmapDrawable)
return drawable.bitmap
val appIconSize = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable is AdaptiveIconDrawable)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 108f, context.resources.displayMetrics).toInt()
else getAppIconSize(context)
return drawable.toBitmap(appIconSize, appIconSize, Bitmap.Config.ARGB_8888)
}
fun getAppIconSize(context: Context): Int {
val activityManager = ContextCompat.getSystemService(this, ActivityManager::class.java)!!
val appIconSize = try {
activityManager.launcherLargeIconSize
} catch (e: Exception) {
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48f, context.resources.displayMetrics).toInt()
}
return appIconSize
}
我想将自适应图标应用到由以下代码生成的快捷方式(代码仅供参考,不编译):
ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, "com.myapp.mainactivity")
.setShortLabel(context.getResources().getString("short label"))
.setLongLabel(context.getResources().getString("long label"))
.setIcon(IconCompat.createWithAdaptiveBitmap(bitmap)
.setIntent(targetActivityIntent)
.build();
public Bitmap getLauncherIcon(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
return null;
} else {
Drawable backgroundDrawable = context.getDrawable(R.mipmap.ic_launcher_background);
Drawable vectorDrawable = context.getDrawable(R.drawable.ic_launcher_main_foreground);
AdaptiveIconDrawable adaptiveIconDrawable = new AdaptiveIconDrawable(backgroundDrawable, vectorDrawable);
Bitmap bitmap = drawableToBitmap(adaptiveIconDrawable);
return bitmap;
}
}
private static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
这是我的图标 (ic_main_white.xml)
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="45dp"
android:height="45dp"
android:viewportHeight="1024"
android:viewportWidth="1024">
<path
android:fillColor="#FFFFFF"
android:pathData="M511.5 74C269.87847 74 74 269.87847 74 511.5 74 753.13368 269.87847 949 511.5 949 753.12153 949 949 753.13368 949 511.5 949 269.87847 753.12153 74 511.5 74l0 0 0 0 0 0zm30.10243 764.20312l0 -140.08506 -57.72569 0 0 140.27951C325.65972 825.12674 199.05208 699.1875 184.78472 541.25l142.21181 0 0 -57.72569 -142.34549 0C198.12847 324.77257 325.07639 197.95833 483.87674 184.63889l0 142.00521 57.72569 0 0 -141.81077c157.64583 14.40104 283.32986 140.76563 296.74653 298.69098l-139.87847 0 0 57.72569 139.74479 0C824.02083 698.34896 698.66493 823.85069 541.60243 838.20312l0 0z"/>
</vector>
使用 Android 资产生成器我创建了一个图像资产,我在其中生成了所有需要的文件。这与您为应用程序的启动器图标创建自适应图标时要经历的过程相同。
但结果只显示了图标的一部分,看起来生成的图标对于预期的来说太大了,或者我在某处遗漏了一些东西。
还有 this 文档,其中说明如下:
For dynamic shortcuts, call the createWithAdaptiveBitmap() method when you create them.
图标由 Android Studio 本身生成,这也适用于我的启动器图标,但为什么使用 createWithAdaptiveBitmap 无法正常工作?我是否遗漏了一些 java 代码,或者您是否知道使用带有快捷方式的自适应图标的示例,但我找不到任何适用于 GitHub 或其他地方的代码。
您需要的解决方案:
当您在 Android Studio 中使用资产管理器生成图标时,请确保图标位于 "secure area" 内,资产具有正确的大小非常重要(参见 https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive)
位图转换是关键:
private static Bitmap drawableToBitmap(final Drawable drawable, final Context context) { final float screenDensity = context.getResources().getDisplayMetrics().density; final int adaptiveIconSize = Math.round(ADAPTIVE_ICON_SIZE_DP * screenDensity); final int adaptiveIconOuterSides = Math.round(ADAPTIVE_ICON_OUTER_SIDES_DP * screenDensity); final Bitmap bitmap = Bitmap.createBitmap(adaptiveIconSize, adaptiveIconSize, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(bitmap); drawable.setBounds(adaptiveIconOuterSides, adaptiveIconOuterSides, adaptiveIconSize - adaptiveIconOuterSides, adaptiveIconSize - adaptiveIconOuterSides); drawable.draw(canvas); return bitmap; }
这是我使用的:
fun convertAppIconDrawableToBitmap(context: Context, drawable: Drawable): Bitmap {
if (drawable is BitmapDrawable)
return drawable.bitmap
val appIconSize = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable is AdaptiveIconDrawable)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 108f, context.resources.displayMetrics).toInt()
else getAppIconSize(context)
return drawable.toBitmap(appIconSize, appIconSize, Bitmap.Config.ARGB_8888)
}
fun getAppIconSize(context: Context): Int {
val activityManager = ContextCompat.getSystemService(this, ActivityManager::class.java)!!
val appIconSize = try {
activityManager.launcherLargeIconSize
} catch (e: Exception) {
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 48f, context.resources.displayMetrics).toInt()
}
return appIconSize
}