gpt4 book ai didi

android - 如何使用 IconCompat.createWithAdaptiveBitmap

转载 作者:行者123 更新时间:2023-11-30 05:08:22 24 4
gpt4 key购买 nike

我想将自适应图标应用于以下代码生成的快捷方式(代码仅供引用,不编译):

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 Asset Generator 创建了一个 Image Asset,我在其中生成了所有需要的文件。这与您为应用的启动器图标创建自适应图标时要经历的过程相同。

但结果只显示了图标的一部分,看起来生成的图标对于预期的来说太大了,或者我在某处遗漏了一些东西。

还有this说明以下内容的文档:

For dynamic shortcuts, call the createWithAdaptiveBitmap() method when you create them.

图标由 Android Studio 本身生成,这也适用于我的启动器图标,但为什么使用 createWithAdaptiveBitmap 无法正常工作?我是否遗漏了一些 Java 代码,或者您是否知道使用带有快捷方式的自适应图标的示例,但我在 GitHub 或其他地方找不到任何可用的东西。

最佳答案

这是我使用的:

    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
}

关于android - 如何使用 IconCompat.createWithAdaptiveBitmap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54150526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com