日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android Drawable.mutate()的使用

發(fā)布時間:2024/4/15 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android Drawable.mutate()的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天,簡單講講android里如何使用Drawable的mutate函數(shù)。


最近,我看到一個自定義控件里用到了Drawable的mutate(),但是自己不知道是什么意思,于是在網(wǎng)上查找了資料,最終解決了問題。這里記錄一下。


摘錄android的官方注釋


/** * Make this drawable mutable. This operation cannot be reversed. A mutable * drawable is guaranteed to not share its state with any other drawable. * This is especially useful when you need to modify properties of drawables * loaded from resources. By default, all drawables instances loaded from * the same resource share a common state; if you modify the state of one * instance, all the other instances will receive the same modification. * * Calling this method on a mutable Drawable will have no effect. * * @return This drawable. * @see ConstantState * @see #getConstantState() */ public Drawable mutate() {return this; }


翻譯過來就是:
使這個drawable變得狀態(tài)不定。這個操作不能還原(變?yōu)椴欢ê缶筒荒茏優(yōu)樵瓉淼臓顟B(tài))。一個狀態(tài)不定的drawable可以保證它不與其他任何一個drawabe共享它的狀態(tài)。這對于你需要更改從同一資源加載來的drawable的屬性時非常有用。默認(rèn)情況下,所有的從同一資源(R.drawable.XXX)加載來的drawable實(shí)例都共享一個共用的狀態(tài),如果你更改一個實(shí)例的狀態(tài),其他所有的實(shí)例都會收到相同的通知。這個方法對于已經(jīng)是mutable的drawable沒有效果。


android中從同一個資源文件中加載出來的drawable會共享狀態(tài),如果你加載出來多個drawable,當(dāng)改變了其中一個的狀態(tài)時,其他drawable的狀態(tài)也會相應(yīng)改變。

如果把這個drawable變?yōu)閙utate drawable后,這個drawable就不會與其他drawable共享狀態(tài)。

特別注意,這個mutate操作是不可逆轉(zhuǎn)的。


簡單講講,其實(shí)就是如果多個控件使用同一個Drawable,如果其中一個控件的Drawable發(fā)生改變,其他所有的Drawable都會發(fā)生改變。如果使用Drawable.mutate(),就可以從Drawable里新建一個不可變的實(shí)例,那么當(dāng)這個Drawable發(fā)生改變時,不會導(dǎo)致其他的Drawable發(fā)生改變。


android Drawable.mutate()的使用就講完了。


就這么簡單。



總結(jié)

以上是生活随笔為你收集整理的android Drawable.mutate()的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。