RemoteViews的layout无法设置xml类型的Drawable,会报错

这里写自定义目录标题

例如这样的:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <corners android:radius="10dp" />
            <size
                android:width="48dp"
                android:height="48dp" />
            <solid android:color="@color/color_red" />
        </shape>
    </item>

    <item
        android:bottom="6dp"
        android:left="6dp"
        android:right="6dp"
        android:top="6dp">
        <bitmap
            android:gravity="center"
            android:src="@drawable/lingxi_ic_hang_up" />
    </item>
</layer-list>

可在代码里这样使用:

java 复制代码
convertToBitmap(R.drawable.bg_lingxi_hang_up_view)?.let { setImageViewBitmap(R.id.image_hang_up, it)}
java 复制代码
    private fun convertToBitmap(@DrawableRes drawableRes: Int): Bitmap? {
        try {
            // 将 Drawable 转换为 Bitmap
            val drawable = TelApplication.instance?.let { ContextCompat.getDrawable(it, drawableRes) }
            AppLog.info(TAG, "convertToBitmap drawable: $drawable")
            if (drawable != null) {
                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)
                AppLog.info(TAG, "convertToBitmap bitmap: $bitmap")
                return bitmap
            }
        } catch (e: Exception) {
            e.printStackTrace()
            AppLog.error(TAG, "convertToBitmap", e)
        }

        AppLog.info(TAG, "convertToBitmap null")
        return null
    }
相关推荐
武子康3 小时前
Java-209 Spring AMQP 整合 RabbitMQ 实战:XML 配置直连交换机、RabbitAdmin 自动声明与收发闭环
xml·java·spring·rabbitmq·java-rabbitmq·java-activemq
利刃大大3 天前
【Mybatis】Mybatis入门 && 基础操作 && XML配置文件开发 && 多表查询 && 注入问题 && 数据库连接池
xml·数据库·mybatis
墨痕诉清风4 天前
java漏洞集合工具(Struts2、Fastjson、Weblogic(xml)、Shiro、Log4j、Jboss、SpringCloud)
xml·java·struts·安全·web安全·spring cloud·log4j
Lbwnb丶4 天前
failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try.
xml
拾忆,想起4 天前
设计模式三大分类完全解析:构建高质量软件的基石
xml·微服务·设计模式·性能优化·服务发现
前网易架构师-高司机4 天前
汽车充电插口识别数据集,可识别快充,慢充插口,支持yolo,coco json,pascal voc xml格式的标注数据集
xml·yolo·汽车·快充·充电·m慢充·插口
武藤一雄4 天前
彻底吃透.NET中序列化反序列化
xml·微软·c#·json·.net·.netcore
spencer_tseng4 天前
org.eclipse.wst.common.project.facet.core.xml could not be read.
xml·java·eclipse
小鸡吃米…5 天前
Python - XML 处理
xml·开发语言·python·开源