You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 14(Pixel 7a)启动器应用如何获取方形应用图标?

解决Android 14启动器获取方形应用图标问题

在Google Pixel 7a(Android 14)上开发启动器时,系统默认会对通过loadIcon()获取的应用图标应用圆角裁剪,哪怕原始图标是方形。要获取完整的方形图标,需要绕过系统的图标美化流程,直接从应用资源包中读取原始图标。

修改代码方案

替换你当前获取图标的逻辑,直接从应用的资源中提取原始图标:

var pm = context.getPackageManager()

var mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)

var apps = pm.queryIntentActivities(mainIntent, 0)
for (resolveInfo in apps) { 
    val appInfo = resolveInfo.activityInfo.applicationInfo
    // 尝试获取原始图标,失败则 fallback 到系统处理后的图标
    val originalIcon = try {
        pm.getDrawable(appInfo.packageName, appInfo.icon, appInfo)
    } catch (e: Exception) {
        resolveInfo.activityInfo.loadIcon(pm)
    }
            
    var img = ImageView(context)
    img.setImageDrawable(originalIcon)

    appGrid.addView(img)
}

处理自适应图标(可选)

如果目标应用使用了自适应图标(AdaptiveIconDrawable),可以将其转换为Bitmap,确保显示完整的方形:

// 工具方法:Drawable转Bitmap
fun drawableToBitmap(drawable: Drawable): Bitmap {
    if (drawable is BitmapDrawable) {
        return drawable.bitmap
    }
    val bitmap = Bitmap.createBitmap(
        drawable.intrinsicWidth, 
        drawable.intrinsicHeight, 
        Bitmap.Config.ARGB_8888
    )
    val canvas = Canvas(bitmap)
    drawable.setBounds(0, 0, canvas.width, canvas.height)
    drawable.draw(canvas)
    return bitmap
}

// 在循环中使用:
val bitmap = drawableToBitmap(originalIcon)
img.setImageBitmap(bitmap)

原理说明

Android 12及以上系统会对启动器获取的图标强制应用系统设置的图标形状(如Pixel 7a默认的圆角/圆形),loadIcon()方法返回的是经过系统加工后的Drawable。而pm.getDrawable()直接从应用的APK资源中读取原始图标,不会经过系统的圆角裁剪流程,因此能保留图标原本的方形样式。

内容的提问来源于stack exchange,提问作者Priff13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 07:16:11