Android sendToTarget

在使用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,那么这两种形式有什么不一样的呢:前者是不能拉伸的,而后者是能够随意拉伸的,而且两者可同一时候存在的。

原文地址:https://www.cnblogs.com/gcczhongduan/p/4516894.html