CoderAlee/PaintedSkin

ImageView根据条件设置不同的图片资源,这种情况应该如何实现换肤??

Closed this issue · 1 comments

@DrawableRes val resId = when (type) {
        Source.Bluetooth -> R.mipmap.icon_source_bt
        Source.USB -> R.mipmap.icon_source_local
        Source.Fm -> R.mipmap.icon_source_fm
        else -> R.mipmap.icon_source_online
}
imageView.setImageResource(resId)

推荐动态添加换肤属性,有换肤库来管理图片资源的黑白天切换

// 像换肤库动态添加换肤属性,addEnabledThemeSkinView会立即执行对应的SkinElement
inline fun View.addSkin(block: () -> SkinElement) {
    WindowManager.getInstance().getWindowProxy(context)?.addEnabledThemeSkinView(this, block())
}

fun View.skinSrc(@DrawableRes id: Int) {
    addSkin { SkinElement(DefaultExecutorBuilder.ATTRIBUTE_SRC, id) }
}

@DrawableRes val resId = when (type) {
        Source.Bluetooth -> R.mipmap.icon_source_bt
        Source.USB -> R.mipmap.icon_source_local
        Source.Fm -> R.mipmap.icon_source_fm
        else -> R.mipmap.icon_source_online
}
imageView.skinSrc(resId)

或在View层监听主题切换事件主动切换资源

fun setSrc(){
@DrawableRes val resId = when (type) {
        Source.Bluetooth -> R.mipmap.icon_source_bt
        Source.USB -> R.mipmap.icon_source_local
        Source.Fm -> R.mipmap.icon_source_fm
        else -> R.mipmap.icon_source_online
}
imageView.setImageDrawable(ThemeSkinService.getInstance().currentThemeSkinPack.getMipmap(resId))
}

ThemeSkinService.getInstance().subscribeSwitchThemeSkin(observer)

// 当切换主题时会回调此函数,在此函数内处理需要动态设置资源的View
fun onThemeSkinSwitchRunOnUiThread(){
    setSrc()
}