【发布时间】:2022-01-13 08:38:34
【问题描述】:
我正在开发一个 android 应用程序:
- Jetpack 生命周期 (ViewModel)
- 喷气背包导航
- 线圈(图像加载器)
我正在尝试自定义 BottomNavigationMenu。
但有一件事是很难的……
最后一个标签是带边框的用户个人资料图片。
如果用户的头像背景颜色是白色的,那么用户界面就很奇怪。 所以我应该显示边框。
class MainActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initBottomNav(binding.bottomNav)
vm.initProfileBottomIcon()
}
private fun initBottomNav(bottomNav: BottomNavigationView) {
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
bottomNav.setupWithNavController(navHostFragment.navController)
bottomNav.itemIconTintList = null
vm.profileImgUrl.observe(this) { url ->
bottomNav.menu.findItem(R.id.profileFragment).load(this, url) {
transformations(CircleCropTransformation())
}
}
}
}
此代码在 BottomNavigationMenu 上绘制个人资料图像。 但不画边框。
当我在谷歌上搜索时,不支持带边框的 CircleCrop(甚至 Glide)。
所以我尝试了下面的代码,但是效果不佳..
vm.profileImg.observe(this) { imgBitmap ->
val layerBorder = ResourcesCompat.getDrawable(resources, R.drawable.oval_trans_border1_red, null)
val layerIcon = BitmapDrawable(Resources.getSystem(), imgBitmap)
val layerDrawable = LayerDrawable(arrayOf(layerIcon, layerBorder))
val bottomNavProfile = bottomNav.menu.findItem(R.id.profileFragment)
val request = ImageRequest.Builder(this)
.data(layerDrawable)
.target {
bottomNavProfile.icon = it
}
.apply {
transformations(CircleCropTransformation())
}
.build()
imageLoader.enqueue(request)
}
有人帮帮我吗?
【问题讨论】:
-
当您加载
profileImg时,您可以使用转换来添加边框。它会给你一个 Drawable/Bitmap 。你试过这种方式吗?如果没有可用的转换,您可以创建一个 .在谈论 Glide 时,您只需修改Circular Transformation类应该不难。类似This.
标签: android navigation android-glide coil