iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 4
0
自我挑戰組

跟Kotlin一起來聊Android元件 或許還有應用,或許還有一些資訊雜談系列 第 4

利用Glide製作圓形圖片 前景加背景一次滿足 LayerDrawable

先上程式碼

fun buttonShowPlay() {
        val green = ColorDrawable(resources.getColor(R.color.green)) as Drawable
        val play = resources.getDrawable(R.drawable.ic_play_arrow_black_24dp)
        val playWithBackground = LayerDrawable(arrayOf(green, play))
        Glide
            .with(this)
            .load(playWithBackground)
            .apply(RequestOptions.circleCropTransform())
            .into(img_play)
    }

來講解流程 :
先定義兩層Drawable,一層當背景,一層當前景使用。
val green = ColorDrawable(resources.getColor(R.color.green)) as Drawable
val play = resources.getDrawable(R.drawable.ic_play_arrow_black_24dp)

然後來一個
val playWithBackground = LayerDrawable(arrayOf(green, play))

LayerDrawable是可以接受Drawable列表對象並管理的Drawable

先來看LayerDrawable的用法

我們在這邊宣告了一個array,裡面放了兩個Drawable物件。並指定給一個LayerDrawable生成一個Drawable物件playWithBackground

然後交給Glide來生成圓形圖片,是不是很簡單?

這邊有一個要注意的重點,LayerDrawable的顯示順序是由列表的第一項,依序疊到上層。

playWithBackground.setLayerInset(0, 0, 0, 0, 0)
playWithBackground.setLayerInset(1, 100, 100, 0, 0)

這邊再來談談setLayerInset的用法

setLayerInset是用來定義LayerDrawable裡每層元素的位置。

setLayerInset(index,left,top,right,bottom)


上一篇
Android Back倒退鍵 事件攔截
下一篇
RecycleView ListView布局
系列文
跟Kotlin一起來聊Android元件 或許還有應用,或許還有一些資訊雜談30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言