展会信息港展会大全

handler.obtainMessage().sendToTarget()分析 android开发教程
来源:互联网   发布日期:2016-01-14 09:14:00   浏览:3760次  

导读:在使用message进行handler的数据交互的时候不可避免的会使用到message作为数据的载体,但是在使用message的时候有人会直接 new一个message,有人会使用handler obtainMessage(),那么这两种方式那种更好一些呢, ...

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

赞助本站

人工智能实验室

相关热词: handler sendToTarget android

AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港