在使用message進行handler的數據交互的時候不可避免的會使用到message作為數據的載體,但是在使用message的時候有人會直接new一個message,有人會使用handler.obtainMessage(),那麼這兩種方式那種更好一些呢,或者說應用場景分別是什麼呢。下面是我的拙見:
/**
* Returns a new {@link android.os.Message
Message} from the global message pool. More efficient than
* creating and allocating new instances. The retrieved message has its handler set to this instance (Message.target == this).
* If you don’t want that facility, just call Message.obtain() instead.
*/
public final Message obtainMessage()
{
return Message.obtain(this);
}
這是android的源碼從註釋上咱們可以看出這種形式得到的message是先從 global message
pool中獲取的,大致可類比成1.6之後的String的變量的String pool,而new Message是直接new的,也就是說這兩種方式隻是在生成對象的形式上不同。
在分享個imageView在設置圖片的時候通常會有兩種形式一種是設置src,另一種是設置background,那麼這兩種形式有什麼不一樣的呢:前者是不能拉伸的,而後者是可以任意拉伸的,並且兩者可同時存在的。