{"id":72028,"date":"2024-02-23T01:22:13","date_gmt":"2024-02-22T17:22:13","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=72028"},"modified":"2024-02-23T01:22:13","modified_gmt":"2024-02-22T17:22:13","slug":"picasso-basic-use","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/picasso-basic-use.html","title":{"rendered":"Picasso \u57fa\u672c\u4f7f\u7528\u548c\u6e90\u7801\u5b8c\u5168\u89e3\u6790"},"content":{"rendered":"
\u8bf4\u5230Picasso<\/a>\uff0c\u76f8\u4fe1Android\u5f00\u53d1\u4eba\u5458\u7edd\u4e0d\u964c\u751f\uff0c\u5b83\u662fSquare\u516c\u53f8\u5f00\u53d1\u7684\u4e00\u6b3e\u56fe\u7247\u52a0\u8f7d\u795e\u5668\u3002\u4f7f\u7528\u8fc7\u5b83\u7684coder\u7edd\u5bf9\u662f\u7231\u4e0d\u91ca\u624b\uff1a\u5bf9\u5b83\u672c\u8eab\u800c\u8a00\uff0c\u8f7b\u91cf\u5b89\u5168\uff0c\u6709\u6548\u52a0\u8f7d\u56fe\u7247\u5e76\u9632\u6b62OOM\uff1b\u5bf9\u6211\u4eec\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7b80\u5355\u65b9\u4fbf\uff0c\u4e00\u884c\u4ee3\u7801\u641e\u5b9a\u56fe\u7247\u52a0\u8f7d\u3002\u56e0\u6b64\u5b83\u5907\u53d7Android\u5f00\u53d1\u4eba\u5458\u7684\u949f\u7231\u3002<\/p>\n \u5173\u4e8e\u5b83\u7684\u66f4\u591a\u597d\u5904\u4f18\u70b9\uff0c\u76f8\u4fe1\u4e0d\u7528\u6211\u4ecb\u7ecd\u4f60\u4e5f\u975e\u5e38\u7684\u6e05\u695a\uff0c\u5728\u8fd9\u91cc\u6211\u4eec\u5e9f\u8bdd\u4e0d\u591a\u8bf4\uff0c\u76f4\u63a5\u7684\u8fdb\u5165\u4e3b\u9898\u6765\u8bb2\u89e3\u5b83\u7684\u4f7f\u7528\u548c\u4ece\u6e90\u7801\u7684\u89d2\u5ea6\u89e3\u6790\u5b83\u7684\u52a0\u8f7d\u539f\u7406\u3002<\/p>\n \u4e00 \u5de5\u7a0b\u5f15\u5165<\/strong><\/p>\n \u5728\u6211\u4eec\u7684\u9879\u76eebuild.gradle\u4e2d\u6dfb\u52a0\u5bf9Picasso\u6846\u67b6\u7684\u4f9d\u8d56\uff1a<\/p>\n \u5728\u8fd9\u91cc\u6211\u4f7f\u7528\u7684\u662f2.5.2\u7684\u6700\u65b0\u7248\u672c\u3002<\/p>\n \u4e8c \u6dfb\u52a0\u6743\u9650<\/strong><\/p>\n \u56e0\u4e3a\u52a0\u8f7d\u56fe\u7247\u9700\u8981\u8bbf\u95ee\u7f51\u7edc\uff0c\u7531\u6b64\u6211\u4eec\u5728Manifest\u4e2d\u6dfb\u52a0\u8bbf\u95ee\u7f51\u7edc\u7684\u6743\u9650\uff1a<\/p>\n \u4e09 \u521b\u5efa\u52a0\u8f7d\u56fe\u7247\u5e03\u5c40\u6587\u4ef6<\/strong><\/p>\n \u5728\u6211\u4eec\u7684MainActivity\u7684\u5e03\u5c40\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2aButton\u548c\u4e00\u4e2aImageView\uff1a<\/p>\n <\/p>\n \u56db MainActivity\u4e2d\u70b9\u51fb\u52a0\u8f7d\u56fe\u7247<\/strong><\/p>\n \u5728\u8fd9\u91cc\u6211\u4eec\u70b9\u51fbButton\u8ba9\u5b83\u52a0\u8f7d\u4e00\u5f20\u767e\u5ea6Logo\u7684\u56fe\u7247\u5e76\u663e\u793a\u5728ImageView\u63a7\u4ef6\u4e2d\u3002<\/p>\n <\/p>\n \u4e94 \u52a0\u8f7d\u56fe\u7247<\/strong><\/p>\n \u70b9\u51fbButton\u52a0\u8f7d\u56fe\u7247\uff0c\u6765\u770b\u7ed3\u679c\uff1a<\/p>\n <\/p>\n ok\uff0c\u5df2\u52a0\u8f7d\u51fa\u6765\u4e86\uff0c\u4f46\u662f\u6211\u4eec\u5230\u5e95\u505a\u4e86\u4ec0\u4e48\u5462\uff1f<\/p>\n \u5176\u5b9e\u6211\u4eec\u6240\u505a\u7684\u4e8b\u60c5\u975e\u5e38\u7684\u7b80\u5355\uff0c\u90a3\u5c31\u662f\u5728Button\u7684\u70b9\u51fb\u4e8b\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u884c\u4ee3\u7801\uff1a<\/p>\n \u6ca1\u9519\u5c31\u662f\u4e00\u884c\u4ee3\u7801\u641e\u5b9a\u56fe\u7247\u52a0\u8f7d\u3002\u81f3\u4e8e\u5b83\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u7684\u6e90\u7801\u5206\u6790\u4e2d\u8be6\u7ec6\u7684\u89e3\u6790\u5b83\uff0c\u73b0\u5728\u6765\u770b\u770b\u5b83\u7684\u5176\u4ed6\u4f7f\u7528\u3002<\/p>\n \u4e0d\u5149\u52a0\u8f7d\u56fe\u7247\u662f\u975e\u5e38\u7b80\u5355\u7684\u4e8b\u60c5\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u5728\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u5360\u4f4d\u56fe\uff0c\u53ef\u4ee5\u5f88\u53cb\u597d\u7684\u544a\u8bc9\u7528\u6237\u5b83\u6b63\u5728\u52a0\u8f7d\uff0c\u800c\u4e0d\u662f\u52a0\u8f7d\u4e2d\u663e\u793a\u7a7a\u767d\u7684\u754c\u9762\u3002<\/p>\n \u4f7f\u7528placeholder\u4e3a\u5c1a\u672a\u52a0\u8f7d\u5230\u56fe\u7247\u7684ImageView\u8bbe\u7f6e\u5360\u4f4d\u56fe\uff0c\u8fd9\u662f\u4e00\u79cd\u53cb\u597d\u7684\u663e\u793a\u65b9\u5f0f\u3002<\/p>\n <\/p>\n \u4e0d\u5149\u53ef\u4ee5\u8bbe\u7f6e\u5360\u4f4d\u56fe\uff0c\u800c\u4e14\u8fd8\u53ef\u4ee5\u5728\u56fe\u7247\u52a0\u8f7d\u4e0d\u51fa\u6765\uff0c\u6216\u662f\u627e\u4e0d\u5230\u8981\u52a0\u8f7d\u7684\u56fe\u7247\u540e\uff0c\u53ef\u4ee5\u4e3aImageView\u8bbe\u7f6e\u5f02\u5e38\u56fe\uff1a<\/p>\n \u4f7f\u7528error\u53ef\u4ee5\u4e3a\u52a0\u8f7d\u5f02\u5e38\u7684ImageView\u8bbe\u7f6e\u5f02\u5e38\u56fe\uff0c\u4fee\u6539\u6211\u4eec\u7684\u56fe\u7247URL\uff0c\u4f7f\u5b83\u65e0\u6cd5\u83b7\u53d6\u5230\u6b63\u786e\u7684\u56fe\u7247\u5730\u5740\uff0c\u7136\u540e\u6765\u770b\u7ed3\u679c\uff1a<\/p>\n <\/p>\n \u4e0d\u4ec5\u5982\u6b64\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5bf9\u52a0\u8f7d\u5230\u7684\u56fe\u7247\u8fdb\u884c\u91cd\u65b0\u8c03\u6574\uff0c\u6bd4\u5982\u6539\u53d8\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u663e\u793a\u5f62\u72b6\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528transform\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a<\/p>\n \u9996\u5148\u6211\u4eec\u5148\u81ea\u5b9a\u4e49\u4e00\u4e2aTransformation\uff1a<\/p>\n \u7136\u540e\u5728transform\u65b9\u6cd5\u4e2d\u8fdb\u884c\u5904\u7406\uff1a<\/p>\n \u8fd9\u6837\u7684\u8bdd\u5c31\u53ef\u4ee5\u5bf9\u56fe\u7247\u8fdb\u884c\u4e00\u5b9a\u610f\u4e49\u4e0a\u7684\u63a7\u5236\u548c\u9009\u62e9\uff0c\u4f7f\u5b83\u66f4\u52a0\u7b26\u5408\u6211\u4eec\u7684\u9700\u6c42\u3002<\/p>\n \u5f53\u7136Picasso\u4e2d\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u7684\u5e94\u7528\uff0c\u6bd4\u5982\u53ef\u4ee5\u8bbe\u7f6e\u52a0\u8f7d\u5927\u5c0f\uff0c\u4f7f\u7528resizeDimen\u65b9\u6cd5\uff0c\u586b\u5145\u65b9\u5f0f\u4f7f\u7528centerCrop\uff0cfit\u7b49\u7b49\uff0c\u5927\u5bb6\u5982\u679c\u6709\u9700\u8981\u7684\u8bdd\u53ef\u4ee5\u81ea\u5df1\u5c1d\u8bd5\u4f7f\u7528\u3002\u8fd9\u91cc\u5c31\u4e0d\u8981\u4e00\u4e00\u7684\u4ecb\u7ecd\u4e86\u3002<\/p>\n ok\uff0c\u4e0a\u9762\u4ecb\u7ecd\u4e86Picasso\u7684\u90e8\u5206\u57fa\u7840\u4f7f\u7528\uff0c\u975e\u5e38\u7684\u7b80\u5355\uff0c\u4e00\u884c\u4ee3\u7801\u641e\u5b9a\u4f60\u7684\u6240\u9700\uff0c\u90a3\u4e48\u4e0b\u9762\u6211\u4eec\u4ece\u6e90\u7801\u7684\u89d2\u5ea6\u6765\u89e3\u6790\u4e0b\u5b83\u5230\u5e95\u662f\u600e\u4e48\u5b9e\u73b0\u6211\u4eec\u7684\u56fe\u7247\u52a0\u8f7d\u548c\u4f7f\u7528\u7684\u3002<\/p>\n \u4ee5\u4e0b\u9762\u6700\u7b80\u6d01\u7684\u52a0\u8f7d\u4e3a\u793a\u4f8b\uff1a<\/p>\n \u9996\u5148Picasso\u4f1a\u8c03\u7528\u9759\u6001with\u65b9\u6cd5\uff0c\u90a3\u4e48\u6211\u4eec\u6765\u770b\u770bwith\u65b9\u6cd5\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff1a<\/p>\n <\/p>\n \u7531\u4e0a\u9762\u7684\u6e90\u7801\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5728with\u65b9\u6cd5\u4e2d\u4e3b\u8981\u505a\u4e86\u4e00\u4ef6\u4e8b\uff0c\u90a3\u5c31\u662f\u8fd4\u56de\u4e00\u4e2aPicasso\u5b9e\u4f8b\uff0c\u5f53\u7136\u8fd9\u4e2a\u5b9e\u4f8b\u4e5f\u4e0d\u662f\u90a3\u4e48\u7b80\u5355\u7684\u521b\u5efa\u7684\uff0c\u4e3a\u4e86\u9632\u6b62Picasso\u7684\u591a\u6b21\u521b\u5efa\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u53cc\u91cd\u52a0\u9501\u7684\u5355\u4f8b\u6a21\u5f0f\u6765\u521b\u5efa\u7684\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u4e86\u4fdd\u8bc1\u7ebf\u7a0b\u7684\u5b89\u5168\u6027\u3002\u4f46\u662f\u5b83\u53c8\u4e0d\u662f\u76f4\u63a5\u7684\u4f7f\u7528\u5355\u4f8b\u6a21\u5f0f\u521b\u5efa\u7684\uff0c\u5728\u521b\u5efa\u5b9e\u4f8b\u7684\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u4e86Builder\u6a21\u5f0f\uff0c\u5b83\u53ef\u4ee5\u4f7fPicasso\u5728\u521b\u5efa\u65f6\u521d\u59cb\u5316\u5f88\u591a\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u540e\u671f\u4f7f\u7528\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u6765\u770b\u770b\u8fd9\u4e2aBuilder\u662f\u600e\u4e48\u64cd\u4f5c\u7684\uff1a<\/p>\n <\/p>\n \u5728Builder\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5c31\u53ea\u662f\u83b7\u53d6\u5230\u5f53\u524d\u5e94\u7528\u7ea7\u522b\u7684\u4e0a\u4e0b\u6587\uff0c\u4e5f\u5c31\u8bf4\u660e\u4e86Picasso\u662f\u9488\u5bf9\u5e94\u7528\u7ea7\u522b\u7684\u4f7f\u7528\uff0c\u4e0d\u4f1a\u662f\u968f\u7740Activity\u6216\u662fFragment\u7684\u751f\u547d\u5468\u671f\u800c\u4ea7\u751f\u53d8\u5316\uff0c\u53ea\u6709\u5f53\u5f53\u524d\u7684\u5e94\u7528\u9000\u51fa\u6216\u662f\u9500\u6bc1\u65f6Picasso\u624d\u4f1a\u505c\u6b62\u5b83\u7684\u884c\u4e3a\u3002<\/p>\n \u90a3\u4e48\u63a5\u4e0b\u6765\u770b\u4e0bbuild\u65b9\u6cd5\u4e2d\u505a\u4e86\u54ea\u4e9b\u64cd\u4f5c\u5462\uff1a<\/p>\n <\/p>\n \u8fd9\u91cc\u4ee3\u7801\u4e5f\u662f\u5f88\u7b80\u5355\uff0c\u4e3b\u8981\u662f\u521d\u59cb\u5316\u4e86downloader\uff0ccache\uff0cservice\uff0cdispatcher\u7b49\u51e0\u4e2a\u5b9e\u4f8b\u53d8\u91cf\uff0c\u800c\u8fd9\u51e0\u4e2a\u53d8\u91cf\u503c\u4e5f\u662f\u5df2\u8bbe\u7f6e\u7684\uff0c\u5982\u6e90\u7801\uff1a<\/p>\n \u8fd9\u4e9b\u8bbe\u7f6e\u5c31\u50cf\u6211\u4eec\u5e73\u5e38\u4f7f\u7528AlertDialog\u4e00\u6837\uff0c\u8d27\u5230\u5230Builder\u4e4b\u540e\u5206\u522b\u8fdb\u884c\u8bbe\u7f6e\u5c31\u884c\u3002<\/p>\n ok\uff0c\u6211\u4eec\u73b0\u5728\u6765\u770b\u770b\u521d\u59cb\u5316\u4e2d\u51e0\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u53d8\u91cf\uff1a \u6765\u770b\u4e0bUtils\u4e2d\u662f\u600e\u4e48\u5b9e\u73b0downloader \u7684\u521d\u59cb\u5316\u7684\uff1a<\/p>\n <\/p>\n createDefaultDownloader\u65b9\u6cd5\u4e2d\u9996\u5148\u4f7f\u7528Java\u53cd\u5c04\u673a\u5236\u6765\u67e5\u627e\u9879\u76ee\u4e2d\u662f\u5426\u4f7f\u7528\u4e86OKHttp<\/a>\u7f51\u7edc\u52a0\u8f7d\u6846\u67b6\uff0c\u5982\u679c\u4f7f\u7528\u4e86\u5219\u4f1a\u4f7f\u7528okhttp\u4f5c\u4e3a\u56fe\u7247\u7684\u52a0\u8f7d\u65b9\u5f0f\uff0c\u5982\u679c\u6ca1\u6709\u4f7f\u7528\uff0c\u5219\u4f1a\u4f7f\u7528\u5185\u7f6e\u7684\u5c01\u88c5\u52a0\u8f7d\u5668UrlConnectionDownloader\u3002<\/p>\n \u6ce8\uff1a\u7531\u4e8eokhttp3\u7684\u5305\u540d\u5df2\u66f4\u6362\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u90fd\u662f\u4f7f\u7528\u5185\u7f6e\u7684\u5c01\u88c5\u4e0b\u8f7d\u5668\uff0c\u8fd9\u4e2a\u662f\u4e00\u4e2a\u5c0fbug\u7b49\u5f85\u5b8c\u5584\u3002\u5f53\u4fee\u590d\u4e4b\u540ePicasso+okhttp3\u5219\u662f\u6700\u7406\u60f3\u7684\u52a0\u8f7d\u65b9\u5f0f\u3002 \u6211\u4eec\u5728\u6765\u770b\u770bPicassoExecutor<\/a>Service\u7684\u6e90\u7801\uff1a<\/p>\n <\/p>\n PicassoExecutorService\u76f4\u63a5\u7ee7\u627f\u4e0eThreadPoolExecutor\u7ebf\u7a0b\u6c60\uff0c\u5728\u6784\u9020\u65b9\u6cd5\u4e2d\u521d\u59cb\u5316\u4e86\u4e3b\u7ebf\u7a0b\u5927\u5c0f\uff0c\u6700\u5927\u7ebf\u7a0b\u7b49\uff0c\u5982\uff1a<\/p>\n \u5f53\u7136\uff0c\u5728Picasso\u7ebf\u7a0b\u6c60\u4e2d\u4e3b\u7ebf\u7a0b\u548c\u6700\u5927\u7ebf\u7a0b\u6570\u662f\u53ef\u53d8\u7684\uff0c\u6839\u636e\u7528\u6237\u4f7f\u7528\u7684\u7f51\u7edc\u7c7b\u578b\u6765\u8bbe\u7f6e\u7ebf\u7a0b\u6570\u91cf\uff0c\u540e\u9762\u4f1a\u8be6\u7ec6\u8bf4\u660e\u5b83\u7684\u5e94\u7528\u3002 \u9996\u5148\uff0c\u6765\u770b\u770b\u5b83\u7684\u767b\u573a\u4eae\u76f8\uff1a<\/p>\n \u5b83\u7684\u767b\u573a\u5c31\u662f\u521b\u5efa\u4e00\u4e2aDispatcher \u5bf9\u8c61\u5b9e\u4f8b\uff0c\u5f53\u7136\uff0c\u5b83\u4f20\u9012\u4e86\u5728Builder\u4e2d\u6240\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u6bd4\u5982downloader\u4e0b\u8f7d\u5668\uff0c\u7528\u4e8e\u56fe\u7247\u7684\u4e0b\u8f7d\uff0c\u5f53\u7136\u4e0b\u8f7d\u662f\u4e0d\u80fd\u518d\u4e3b\u7ebf\u7a0b\u8fdb\u884c\u7684\uff0c\u6240\u4ee5\u8fd9\u91cc\u4e5f\u4f20\u9012\u4e86service\u7ebf\u7a0b\u6c60\uff0c\u800c\u4e0b\u8f7d\u597d\u7684\u8d44\u6e90\u4e5f\u662f\u4e0d\u80fd\u76f4\u63a5\u5728\u5b50\u7ebf\u7a0b\u4e2d\u8fdb\u884c\u66f4\u65b0UI\u7684\uff0c\u6240\u4ee5\u540c\u65f6\u4e5f\u628a\u4e3b\u7ebf\u7a0b\u4e2d\u7684HANDLER\u4f20\u9012\u8fdb\u53bb\uff0c\u540c\u65f6\u5e94\u7528\u7ea7\u522b\u7684\u4e0a\u4e0b\u6587context\uff0c\u548c\u7f13\u5b58cache\uff0c\u72b6\u6001\u53d8\u5316stats\u7b49\u4e5f\u4f20\u9012\u8fdb\u53bb\u8fdb\u884c\u76f8\u5bf9\u5e94\u7684\u4e1a\u52a1\u64cd\u4f5c\u3002<\/strong><\/p>\n \u7ecf\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u5f88\u6e05\u695a\u7684\u770b\u51fa\uff0c\u5c31\u8fd9\u4e48\u7b80\u5355\u7684\u4e00\u4e2a\u521b\u5efa\u5b9e\u4f8b\u5df2\u7ecf\u5f88\u660e\u786e\u7684\u8868\u8fbe\u51fa\u4e86Dispatcher\u5b58\u5728\u7684\u610f\u4e49\uff0c\u800c\u4e14\u6211\u4eec\u4e5f\u660e\u786e\u4e86\u5b83\u5927\u6982\u7684\u804c\u8d23\u3002<\/p>\n \u90a3\u4e48\u6211\u4eec\u63a5\u7740\u770b\u770bDispatcher\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5177\u4f53\u7684\u505a\u4e86\u54ea\u4e9b\u64cd\u4f5c\uff1a<\/p>\n <\/p>\n \u5728Dispatcher\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u6211\u628a\u5b83\u5206\u4e3a\u4e865\u4e2a\u90e8\u5206\uff0c\u4e0b\u9762\u6765\u8be6\u7ec6\u7684\u89e3\u6790\u4e0b\uff1a<\/p>\n \u2460\uff1adispatcherThread<\/strong>\uff0c\u5b83\u662f\u4e00\u4e2aHandlerThread\u7ebf\u7a0b\uff0c\u5982\uff1a<\/p>\n \u5b83\u7684\u521b\u5efa\u4e3b\u8981\u662f\u5f00\u542f\u4e00\u4e2a\u5b50\u7ebf\u7a0b\u4f9bDispatcher\u8c03\u7528\uff0c\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u5728\u5b50\u7ebf\u7a0b\u4e2d\u53bb\u6267\u884c\u8017\u65f6\u7684\u56fe\u7247\u4e0b\u8f7d\u64cd\u4f5c\u3002<\/p>\n \u2461\uff1a\u5bf9\u8c61\u5b9e\u4f8b\u7684\u63a5\u53d7<\/strong>\uff0c\u4e3b\u8981\u662f\u63a5\u53d7\u5728Picasso\u4e2d\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u6ca1\u6709\u4ec0\u4e48\u597d\u8bf4\u7684\u3002<\/p>\n \u2462\uff1a\u521b\u5efa\u7528\u4e8e\u4fdd\u5b58\u6570\u636e\u3001\u5bf9\u8c61\u7684\u96c6\u5408<\/strong>\uff0c\u4e5f\u5c31\u662f\u4e3a\u4e86\u4fdd\u5b58\u5bf9\u8c61\u6216\u72b6\u6001\u7528\u7684\u3002<\/p>\n \u2463\uff1aDispatcherHandler\uff0c\u8fd9\u662f\u4e00\u4e2aHandler\uff0c\u5e76\u4e14\u662f\u4f5c\u7528\u5728dispatcherThread\u7ebf\u7a0b\u4e2d\u7684Handler\uff0c\u5b83\u7528\u4e8e\u628a\u5728dispatcherThread\u5b50\u7ebf\u7a0b\u7684\u64cd\u4f5c\u8f6c\u5230\u5230Dispatcher\u4e2d\u53bb<\/strong>\uff0c\u5b83\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u63a5\u53d7\u4e86Dispatcher\u5bf9\u8c61\uff1a<\/p>\n <\/p>\n \u6211\u4eec\u5728\u6765\u770b\u770b\u4ed6\u7684handleMessage\u65b9\u6cd5\u662f\u600e\u4e48\u5904\u7406\u6d88\u606f\u7684\uff1a<\/p>\n \u800c\u4ece\u5b83\u7684\u5904\u7406\u6d88\u606f\u7684handleMessage\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u6240\u6709\u7684\u6d88\u606f\u5e76\u6ca1\u6709\u88ab\u76f4\u63a5\u8fdb\u884c\u5904\u7406\u800c\u662f\u8f6c\u79fb\u5230\u4e86dispatcher\u4e2d\uff0c\u5728dispatcher\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n \u2464\uff1a\u76d1\u542c\u7f51\u7edc\u53d8\u5316\u64cd\u4f5c<\/strong>\uff0c\u5b83\u662f\u7528\u4e8e\u76d1\u542c\u7528\u6237\u624b\u673a\u7f51\u7edc\u53d8\u5316\u800c\u5b58\u5728\u7684\u3002\u6211\u4eec\u4e3b\u8981\u6765\u770b\u770bNetworkBroadcastReceiver\u8fd9\u4e2a\u7c7b\uff1a<\/p>\n <\/p>\n \u5728\u6784\u9020\u53c2\u6570\u4e2d\u4e5f\u63a5\u6536\u5230Dispatcher\u5bf9\u8c61\uff0c\u5e76\u6709\u6ce8\u518c\u5e7f\u64ad\u548c\u9500\u6bc1\u5e7f\u64ad\u7684\u65b9\u6cd5\uff0c\u5f53\u7136\u5b83\u4e5f\u6ca1\u6709\u76f4\u63a5\u7684\u5904\u7406\uff0c\u4e5f\u662f\u4f20\u9012\u5230Dispatcher\u4e2d\u8fdb\u884c\u6d88\u5316\u7684\u3002<\/p>\n \u6211\u4eec\u5728\u6765\u770b\u770b\u5f53\u7528\u6237\u7684\u7f51\u7edc\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u5b83\u4f1a\u505a\u54ea\u4e9b\u64cd\u4f5c\uff1a<\/p>\n <\/p>\n \u6211\u4eec\u53ef\u4ee5\u770b\u5230\u4e3b\u8981\u5206\u4e3a\u4e24\u4e2a\uff0c\u4e00\u4e2a\u662f\u822a\u73ed\u6a21\u5f0f\uff0c\u4e00\u4e2a\u662f\u6b63\u5e38\u7684\u7f51\u7edc\u72b6\u6001\u53d8\u5316\uff0c\u822a\u73ed\u6a21\u5f0f\u6211\u4eec\u5148\u4e0d\u7528\u7406\u4f1a\uff0c\u4e3b\u8981\u770b\u4e0b\u7f51\u7edc\u53d8\u5316\u7684\u64cd\u4f5c\uff1a<\/p>\n \u8fd9\u91cc\u7684handler\u5c31\u662fDispatcherHandler\uff0c\u90a3\u4e48\u6211\u4eec\u770b\u770b\u5b83\u53c8\u662f\u600e\u4e48\u505a\u7684\uff1a<\/p>\n \u8c03\u7528dispatcher\u7684performNetworkStateChange\u65b9\u6cd5\u6765\u5904\u7406\uff1a<\/p>\n <\/p>\n \u5f53\u7f51\u7edc\u53d8\u5316\u65f6\uff0c\u5b83\u4f1a\u4f20\u9012\u7ed9\u6211\u4eec\u7684PicassoExecutorService\u7ebf\u7a0b\u6c60\uff0c\u5728adjustThreadCount\u65b9\u6cd5\u4e2d\u5224\u65ad\u7528\u6237\u662f\u4f7f\u7528\u7684\u90a3\u7c7b\u578b\u7f51\u7edc\uff0c\u5982wifi\uff0c4G\u7b49\uff0c\u7136\u540e\u4e3a\u7ebf\u7a0b\u6c60\u8bbe\u7f6e\u76f8\u5e94\u7684\u7ebf\u7a0b\u6570\u3002\u6765\u770b\uff1a<\/p>\n \u5c31\u5982\u4e0a\u9762\u6240\u8bf4\uff0c\u6839\u636e\u7528\u6237\u4f7f\u7528\u4e0d\u540c\u7684\u7f51\u7edc\u7c7b\u578b\u5206\u522b\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u6570\u91cf\uff0c\u6bd4\u5982\u5f53\u7528\u6237\u4f7f\u7528\u7684\u662fwifi\uff0c\u7ebf\u7a0b\u6570\u91cf\u5c06\u4f1a\u8bbe\u7f6e\u4e3a4\u4e2a\uff0c4G\u7684\u8bdd\u8bbe\u4e3a3\u4e2a\u7b49\uff0c\u8fd9\u6837\u6839\u636e\u7528\u6237\u7684\u5177\u4f53\u60c5\u51b5\u6765\u8bbe\u8ba1\u7ebf\u7a0b\u6570\u91cf\u662f\u975e\u5e38\u4eba\u6027\u5316\u7684\uff0c\u4e5f\u662f\u503c\u5f97\u6211\u4eec\u6548\u4eff\u7684\u3002<\/p>\n ok\uff0c\u5230\u6b64\u6211\u4eec\u91cd\u8981\u7684Dispatcher\u5bf9\u8c61\u7684\u6784\u9020\u65b9\u6cd5\u5df2\u5b8c\u5168\u7684\u89e3\u6790\u5b8c\u6210\u4e86\uff0c\u4ece\u4e0a\u9762\u7684\u89e3\u6790\u4e2d\u6211\u4eec\u5f88\u6e05\u695a\u7684\u770b\u5230\u4e86Dispatcher\u4f5c\u4e3a\u4e2d\u8f6c\u7ad9\u5b58\u5728\u7684\u610f\u4e49\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u7ebf\u7a0b\u8f6c\u6362\u64cd\u4f5c\u90fd\u662f\u7531Dispatcher\u6765\u64cd\u63a7\u7684\uff0c\u5f53\u7136\u53ef\u80fd\u8fd8\u6709\u5c0f\u4f19\u4f34\u4eec\u5e76\u4e0d\u6e05\u695a\u5b83\u662f\u600e\u4e48\u8fd0\u4f5c\u7684\uff0c\u600e\u4e48\u8fdb\u5165\u5b50\u7ebf\u7a0b\u7684\uff0c\u90a3\u662f\u56e0\u4e3a\u6211\u4eec\u7684\u8bb2\u89e3\u8fd8\u6ca1\u6709\u8fdb\u884c\u5230\u8fdb\u5165\u5b50\u7ebf\u7a0b\u7684\u6b65\u9aa4\u800c\u5df2\uff0c\u4e0b\u9762\u5c06\u4f1a\u8fdb\u4e00\u6b65\u7684\u8bb2\u89e3\u3002<\/p>\n \u603b\u7ed3\u4e0bDispatcher\u4e2d\u6240\u5305\u542b\u7684\u91cd\u8981\u5bf9\u8c61\u5b9e\u4f8b\uff1a<\/strong><\/p>\n \u2460\uff1aPicassoExecutorService\u7ebf\u7a0b\u6c60<\/strong><\/p>\n \u2461\uff1adownloader \u4e0b\u8f7d\u5668<\/strong><\/p>\n \u2462\uff1a\u9488\u5bf9DispatcherThread\u7ebf\u7a0b\u7684DispatcherHandler\u5904\u7406\u5668<\/strong><\/p>\n \u2463\uff1aNetworkBroadcastReceiver\u7f51\u7edc\u76d1\u542c\u5668<\/strong><\/p>\n \u2464\uff1amainThreadHandler\u4e3b\u7ebf\u7a0b\u7684Handler<\/strong><\/p>\n \u2465\uff1a\u4fdd\u5b58\u6570\u636e\u7684\u96c6\u5408\uff1ahunterMap\uff0cpausedActions\uff0cbatch\u7b49<\/strong><\/p>\n \u7406\u89e3\u597d\u4e86\u4e0a\u9762\u8fd9\u4e9b\u5bf9\u8c61\u5b58\u5728\u7684\u610f\u4e49\u5c06\u5bf9\u4e0b\u9762\u7684\u7406\u89e3\u6709\u7740\u5de8\u5927\u7684\u597d\u5904\uff0c\u8bf7\u6ca1\u6709\u5b8c\u5168\u7406\u89e3\u7684\u5728\u4ed4\u7ec6\u7684\u9605\u8bfb\u4e00\u904d\uff08\u975e\u5e38\u91cd\u8981\uff09\u3002<\/p>\n ok\uff0c\u77e5\u9053\u4e86Dispatcher\u5305\u542b\u54ea\u4e9b\u91cd\u8981\u7684\u5bf9\u8c61\u5b9e\u4f8b\u4e4b\u540e\uff0c\u8ba9\u6211\u4eec\u518d\u56de\u5230Picasso\u7684Builder\u4e2d\uff0c\u5728build\u65b9\u6cd5\u4e2d\u6700\u7ec8\u8fd4\u56de\u7684\u662f\u4e00\u4e2aPicasso\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff1a<\/p>\n \u5728Picasso\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u503c\u5f97\u6211\u4eec\u53ea\u8981\u6ce8\u610f\u7684\u662f\u9488\u5bf9requestHandlers\u7684\u5e94\u7528\uff1a<\/p>\n <\/p>\n \u8fd9\u91cc\u628aPicasso\u80fd\u90fd\u5e94\u7528\u7684RequestHandler\u90fd\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\uff0c\u7136\u540e\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u8fd0\u7528\u76f8\u5bf9\u5e94\u7684Handler\u8fdb\u884c\u4e1a\u52a1\u7684\u5904\u7406\u3002<\/p>\n ok\uff0c\u5230\u6b64Picasso\u4e2dwith\u65b9\u6cd5\u4e2d\u6240\u505a\u7684\u4e8b\u60c5\u5df2\u7ecf\u5b8c\u5b8c\u5168\u5168\u7684\u5c55\u793a\u5728\u60a8\u7684\u9762\u524d\u4e86\uff0c\u76f8\u4fe1\u60a8\u5bf9\u8fd9\u4e00\u6b65\u5e94\u7528\u7406\u89e3\u7684\u5f88\u900f\u5f7b\u4e86\u3002<\/p>\n \u90a3\u4e48\u6765\u603b\u7ed3\u4e0b\uff0cPicasso\u73b0\u5728\u62e5\u6709\u4e86\u90a3\u4e9b\u91cd\u8981\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff1a<\/strong><\/p>\n \u2460\uff1adispatcher\uff0c\u91cd\u8981\u6027\u4e0d\u8a00\u800c\u55bb\uff0c\u4e0a\u9762\u5df2\u5145\u5206\u7684\u5c55\u793a\u4e86\u5b83\u7684\u91cd\u8981\u6027\uff0c\u4f5c\u4e3a\u4e2d\u8f6c\u7ad9\uff0cPicasso\u662f\u5fc5\u987b\u8981\u62e5\u6709\u4e86\u7684\u3002\u6700\u7ec8\u76ee\u7684\u662f\u8ba9\u6211\u4eec\u7684\u4e3b\u7ebf\u7a0b\u8fdb\u5165\u5b50\u7ebf\u7a0b\u4e2d\u53bb\u8fdb\u884c\u8017\u65f6\u7684\u4e0b\u8f7d\u64cd\u4f5c\u3002<\/strong><\/p>\n \u2461\uff1arequestHandlers\uff0c\u5b83\u7684\u5b58\u5728\u662f\u4e3a\u4e86\u9009\u62e9\u4e00\u79cd\u8bf7\u6c42\u5904\u7406\u65b9\u5f0f\uff0c\u6bd4\u5982\u8bf4\uff0c\u4e0b\u8f7d\u7f51\u7edc\u56fe\u7247\u9700\u8981\u4f7f\u7528NetworkRequestHandler\u8fd9\u4e2a\u8bf7\u6c42\u5668\u3002<\/strong><\/p>\n \u2462\uff1aHANDLER\uff0c\u8fd9\u662f\u4e3b\u7ebf\u7a0b\u7684Handler\uff0c\u7528\u6765\u5904\u7406\u8fd4\u56de\u7ed3\u679c\u7684\uff0c\u6bd4\u5982\u8bf4\u56fe\u7247\u4e0b\u8f7d\u6210\u529f\u540e\u8981\u66f4\u65b0UI\u5c31\u662f\u901a\u8fc7\u5b83\u6765\u5b8c\u6210\u7684\uff0c\u8fd9\u4f1a\u5728\u540e\u9762\u56fe\u7247\u4e0b\u8f7d\u5b8c\u6210\u540e\u66f4\u65b0UI\u65f6\u8be6\u7ec6\u8bb2\u89e3<\/strong><\/p>\n \u2463\uff1a\u4e00\u4e9b\u914d\u7f6e\u5bf9\u8c61\u5b9e\u4f8b\u7b49\uff0c\u5982\uff1a\u7f13\u5b58cache\uff0c\u56fe\u7247\u52a0\u8f7d\u9ed8\u8ba4\u914d\u7f6edefaultBitmapConfig\uff0c\u72b6\u6001\u5b58\u50a8stats\u7b49\u7b49\u3002<\/strong><\/p>\n with\u65b9\u6cd5\u4e2d\u4e3b\u8981\u662f\u505a\u4e86\u4e00\u4e2a\u57fa\u7840\u7684\u914d\u7f6e\u5de5\u4f5c\uff0c\u6bd4\u5982Picasso\u7684\u914d\u7f6e\uff0cDispatcher\u7684\u914d\u7f6e\uff0c\u8fd9\u4e9b\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u524d\u63d0\u5de5\u4f5c\uff0c\u53ea\u6709\u505a\u597d\u4e86\u8fd9\u4e9b\u914d\u7f6e\u6211\u4eec\u4f7f\u7528\u8d77\u6765\u624d\u80fd\u663e\u5f97\u6beb\u4e0d\u8d39\u52b2\u3002<\/p>\n \u4e0b\u9762\u6211\u4eec\u5c31\u6765\u770b\u770b\u5b83\u7684\u5e94\u7528\u5427\u3002\u5728load\u65b9\u6cd5\u4e2d\u9700\u8981\u6211\u4eec\u4f20\u9012\u4e00\u4e2a\u53c2\u6570\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ef\u4ee5\u662fUrl\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2apath\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u6587\u4ef6\uff0c\u4e00\u4e2a\u8d44\u6e90\u5e03\u5c40\u7b49\uff1a<\/p>\n \u4e0d\u7ba1\u4f20\u9012\u7684\u662f\u4e00\u4e2a\u4ec0\u4e48\u53c2\u6570\uff0c\u5b83\u90fd\u662f\u8fd4\u56de\u4e00\u4e2a\u8bf7\u6c42\u6784\u9020\u5668RequestCreator\uff0c\u6211\u4eec\u6765\u770b\u770b\u5b83\u7684\u6784\u9020\u65b9\u6cd5\u505a\u4e86\u54ea\u4e9b\u4e8b\u60c5\uff1a<\/p>\n <\/p>\n \u4e3b\u8981\u662f\u83b7\u53d6\u5230Picasso\u5bf9\u8c61\uff0c\u5e76Builder\u6a21\u5f0f\u6784\u5efa\u4e00\u4e2aRequest\u4e2d\u7684Builder\u5bf9\u8c61\uff1a<\/p>\n \u8fd9\u4e2aBuilder\u5bf9\u8c61\u4e3b\u8981\u63a5\u53d7\u4e86\u4e00\u4e9b\u53c2\u6570\u4fe1\u606f\uff0c\u5305\u62ecurl\uff0c\u8d44\u6e90\u5e03\u5c40\uff0c\u548c\u9ed8\u8ba4\u7684\u56fe\u7247\u914d\u7f6e\u3002<\/p>\n \u7531\u4e8eload\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2aRequestCreator\u5bf9\u8c61\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65b9\u6cd5\u94fe\u7684\u5f62\u5f0f\u8fdb\u884c\u8c03\u7528\u5176\u4ed6\u7684\u65b9\u6cd5\uff0c\u6bd4\u5982\u7ed9\u8bf7\u6c42\u6dfb\u52a0\u5360\u4f4d\u56fe\uff0c\u5f02\u5e38\u56fe\uff0c\u8f6c\u6362\u5668\u7b49\u7b49\uff0c\u5177\u4f53\u7684\u53ef\u4ee5\u770b\u6e90\u7801\u3002<\/p>\n ok\uff0c\u603b\u4f53\u6765\u8bf4\u8fd9load\u65b9\u6cd5\u4e2d\u4e3b\u8981\u662f\u521b\u5efa\u4e86\u4e00\u4e2aRequestCreator\u5bf9\u8c61\uff0c\u5e76\u4e14RequestCreator\u4e2d\u6784\u9020\u4e86\u4e00\u4e2aRequest.Builder\u5bf9\u8c61\u3002<\/p>\n \u90a3\u4e48\u6765\u770b\u770bRequestCreator\u62e5\u6709\u54ea\u4e9b\u91cd\u8981\u7684\u5bf9\u8c61\u5b9e\u4f8b\uff1a<\/strong><\/p>\n \u2460\uff1apicasso\u5bf9\u8c61<\/strong><\/p>\n \u2461\uff1aRequest.Builder\u5bf9\u8c61\u5b9e\u4f8bdata\uff0c\u5b83\u91cc\u9762\u5305\u62ec\u6211\u4eec\u8bf7\u6c42\u7684URL\u5730\u5740\uff0c\u8d44\u6e90\u6587\u4ef6\u4ee5\u53ca\u56fe\u7247\u9ed8\u8ba4\u914d\u7f6e\u3002<\/strong><\/p>\n \u5728load\u65b9\u6cd5\u4e2d\u4e3b\u8981\u521b\u5efa\u4e86\u4e00\u4e2aRequestCreator\u5bf9\u8c61\uff0c\u5e76\u83b7\u53d6\u5230\u4e86\u8981\u52a0\u8f7d\u7684url\/path\/file\/resourceId\u8d44\u6e90\u5730\u5740\u8def\u5f84\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u8981\u505a\u7684\u5c31\u662f\u5728into\u65b9\u6cd5\u4e2d\u6765\u52a0\u8f7d\u56fe\u7247\u4e86\u3002\u5148\u6765\u770b\u770binto\u65b9\u6cd5\u4e2d\u505a\u4e86\u54ea\u4e9b\u4e8b\u60c5\uff1a<\/p>\n \u5982\u4e0a\u6e90\u7801\u53ef\u4ee5\u77e5\u9053into\u8fd8\u662f\u505a\u4e86\u5f88\u591a\u7684\u4e8b\u60c5\uff0c\u4e0b\u9762\u4e00\u4e00\u89e3\u6790\uff1a<\/p>\n \u2460\uff1acheckMain()\uff1a<\/strong>\u9996\u5148\u68c0\u67e5\u4e3b\u5426\u5728\u4e3b\u7ebf\u7a0b\u8fd0\u884c\uff0c\u5982\u679c\u4e0d\u662f\u5728\u4e3b\u7ebf\u7a0b\u5c31\u4f1a\u629b\u51fa\u4e00\u4e2a\u5e94\u8be5\u5728\u4e3b\u7ebf\u7a0b\u8fd0\u884c\u7684\u5f02\u5e38\uff1athrow new IllegalStateException(\u201cMethod call should happen from the main thread.\u201d);\u8fd9\u8bf4\u660e\u5230\u8fd9\u4e00\u6b65\u8fd8\u662f\u5728\u4e3b\u7ebf\u7a0b\u8fd0\u884c\u7684\u3002<\/p>\n \u2461\uff1adata.hasImage()\uff1a<\/strong>\u8fd9\u91cc\u7684data\u662f\u5728\u4e4b\u524d\u521d\u59cb\u5316\u7684Request.Builder\u5bf9\u8c61\uff0c\u5b83\u91cc\u9762\u5305\u542burl\u5730\u5740\uff0cresourceId\u548c\u9ed8\u8ba4\u914d\u7f6e\uff0c\u8fd9\u91cc\u662f\u5224\u65aduri\u6216resourceId\u662f\u5426\u4e3a\u7a7a\u4e3a0\uff0c\u5982\u679c\u662f\u7684\u8bdd\u5c31\u53d6\u6d88imageview\u7684\u8bf7\u6c42\uff1apicasso.cancelRequest(target);<\/p>\n \u2462\uff1adeferred\uff1a<\/strong>\u5ef6\u8fdf\uff0c\u5982\u679c\u9700\u8981\u5ef6\u8fdf\u7684\u8bdd\u5c31\u4f1a\u5f97\u5230\u6267\u884c\uff0c\u7136\u540e\u4f1a\u53bb\u83b7\u53d6data\u4e2d\u56fe\u7247\u7684\u5927\u5c0f\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\uff0c\u5c31\u5f97\u5230target\u7684\u5bbd\u9ad8\u6765\u91cd\u65b0\u8bbe\u7f6e\u8981\u52a0\u8f7d\u7684\u56fe\u7247\u7684\u5c3a\u5bf8\uff1adata.resize(width, height);<\/p>\n \u2463\uff1acreateRequest\uff1a<\/strong>\u5728data.build()\u65b9\u6cd5\u4e2d\u521b\u5efaRequest\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c06\u4f1a\u5305\u542buri\u6216resourceId\u4ee5\u53ca\u9ed8\u8ba4\u56fe\u7247config\u3002\u7136\u540e\u5728\u5f97\u5230\u7684Request\u5bf9\u8c61\u540e\u8fdb\u884c\u8f6c\u6362\uff0c\u8be5\u8f6c\u6362\u4e3b\u8981\u662f\u4e0e\u6211\u4eec\u5728Picasso\u6784\u5efa\u65f6\u662f\u5426\u81ea\u5b9a\u4e49\u4e86RequestTransformer\u6709\u5173\u3002<\/p>\n \u2464\uff1acreateKey\uff1a<\/strong>\u5b83\u4e3b\u8981\u7684\u662f\u8fd4\u56de\u5df2String\u7c7b\u578bkey\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u5efa\u7acbImageView\u548ckey\u7684\u5173\u8054\uff0c\u53ef\u4ee5\u901a\u8fc7key\u6765\u83b7\u53d6\u5230Imageview\u7684\u72b6\u6001\uff0c\u6bd4\u5982\u8bf4\u662f\u5426\u5df2\u7f13\u5b58<\/p>\n \u2465\uff1ashouldReadFromMemoryCache\uff1a<\/strong>\u770b\u65b9\u6cd5\u540d\u4e5f\u80fd\u77e5\u9053\uff0c\u5b83\u7684\u4f5c\u7528\u662f\u662f\u5426\u4ece\u5185\u5b58\u7f13\u5b58\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5c31\u4ece\u7f13\u5b58\u4e2d\u8bfb\u53d6\u6570\u636e\uff0c\u5047\u5982\u83b7\u53d6\u5230\u6570\u636e\u5219\u4f1a\u53d6\u6d88\u5f53\u524dImageView\u7684\u8bf7\u6c42\u5e76\u76f4\u63a5\u7ed9\u5b83\u8bbe\u7f6eBitmap\uff0c\u800c\u4e14\u5982\u679c\u6709\u56de\u8c03\u7684\u8bdd\u5c06\u4f1a\u56de\u8c03\u6210\u529f\u7684\u65b9\u6cd5\u3002<\/p>\n \u2466\uff1aImageViewAction\uff1a<\/strong>\u6784\u5efa\u4e00\u4e2aImageViewAction\u5bf9\u8c61\uff0c\u503c\u5f97\u6ce8\u610f\u7684\u662f\u5728\u6784\u5efa\u5bf9\u8c61\u65f6\uff0c\u4f1a\u628aImageView\u6dfb\u52a0\u5230RequestWeakReference\u8fdb\u884c\u5b58\u50a8\uff0c\u4ee5\u4fbf\u4e8e\u4f7f\u7528\u65f6\u67e5\u627e\uff0cRequestWeakReference\u662f\u4e00\u4e2aWeakReference\u7c7b\u578b\u7684\u5f31\u5f15\u7528\u3002\u540c\u65f6ImageViewAction\u4e5f\u662f\u5728\u5b8c\u6210\u56fe\u7247\u52a0\u8f7d\u65f6\u771f\u6b63\u66f4\u65b0UI\u7684\u5173\u952e\u7c7b\uff0c\u8fd9\u5728\u540e\u9762\u4f1a\u8fdb\u884c\u8be6\u7ec6\u8bb2\u89e3\u3002<\/p>\n \u2467\uff1aenqueueAndSubmit\uff1a<\/strong>\u8c03\u7528picasso\u7684enqueueAndSubmit\u65b9\u6cd5\u8fdb\u884c\u63d0\u4ea4\u4efb\u52a1\u3002<\/p>\n <\/p>\n \u5728\u6765\u770bsubmit\u65b9\u6cd5\u7684\u64cd\u4f5c\uff1a<\/p>\n \u5728\u8fd9\u91cc\u518d\u6b21\u4f7f\u7528\u5230\u4e86dispatcher\u4efb\u52a1\u5206\u53d1\u5668\uff0c\u628a\u6211\u4eec\u7684\u4efb\u52a1action\u63d0\u4ea4\u5230Dispatcher\u4e2d\u8fdb\u884c\u5904\u7406\u3002\u7136\u540e\u5728\u6765\u770b\u4e0bdispatchSubmit\u65b9\u6cd5\uff1a<\/p>\n \u7531Dispatcher\u7684\u6784\u9020\u65b9\u6cd5\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u6b64\u65f6\u7684handler\u662fDispatcherHandler\uff0c\u90a3\u4e48\u6211\u4eec\u770b\u4e0b\u5b83\u662f\u600e\u4e48\u5904\u7406\u6211\u4eec\u7684\u4efb\u52a1\u884c\u4e3a\u7684\uff1a<\/p>\n \u5728handleMessage\u4e2d\u76f4\u63a5\u83b7\u53d6\u5230\u6211\u4eec\u7684\u4efb\u52a1\u884c\u4e3aAction\uff0c\u7136\u540e\u8c03\u7528performSubmit\u65b9\u6cd5\uff1a<\/p>\n performSubmit\u65b9\u6cd5\u662f\u771f\u6b63\u610f\u4e49\u4e0a\u7684\u4efb\u52a1\u63d0\u4ea4\u7684\u5177\u4f53\u5730\u65b9\uff0c\u6211\u4eec\u6765\u89e3\u8bfb\u4e0b\u5b83\u7684\u6e90\u7801\uff1a<\/p>\n \u2460\uff1a\u9996\u5148\u6839\u636eaction\u7684\u6807\u5fd7\u6765\u67e5\u8be2\u662f\u5426\u5df2\u5b58\u5728\u4e8e\u6682\u505c\u5217\u8868\u4e2d\uff0c\u5982\u679c\u5b58\u5728\u5c06\u4f1a\u628aaction\u5b58\u653e\u5230pausedActions\u7684\u96c6\u5408\u5217\u8868\u4e2d\uff0c\u4ee5\u4fbf\u7b49\u5f85\u5524\u9192\u8bf7\u6c42\u3002<\/strong><\/p>\n \u2461\uff1a\u7136\u540e\u901a\u8fc7action\u7684key\u4ecehunterMap\u96c6\u5408\u4e2d\u67e5\u8be2\u662f\u5426\u5df2\u5b58\u5728\u8be5hunterMap\u7684\u8bf7\u6c42\u9879\uff0c\u5982\u679c\u5b58\u5728\u5c06\u4f1a\u8c03\u7528hunter\u7684attach\u65b9\u6cd5\uff0c\u8fdb\u884c\u5408\u5e76\u8bf7\u6c42\uff0c\u907f\u514d\u4e00\u4e2aImageView\u8fdb\u884c\u591a\u6b21\u7684\u91cd\u590d\u8bf7\u6c42\uff1a<\/strong><\/p>\n <\/p>\n \u628aaction\u5b58\u653e\u5230ArrayList\u5f53\u4e2d\uff0c\u5982\u679c\u6709\u76f8\u540c\u7684action\u6839\u636eArrayList\u7684\u4e0d\u91cd\u590d\u6027\u5c06\u4f1a\u4fdd\u5b58\u4e00\u4e2aaction\uff0c\u5e76\u4e14\u66f4\u65b0\u65b0\u7684\u5c5e\u6027\u503cpriority\u3002<\/p>\n \u2462\uff1a\u5f53\u7ebf\u7a0b\u6c60service\u6ca1\u6709\u5173\u95ed\u7684\u65f6\u5019\uff0c\u901a\u8fc7forRequest\u65b9\u6cd5\u83b7\u53d6\u4e00\u4e2aRunnable\u7c7b\u578b\u7684BitmapHunter\u7ebf\u7a0b\uff0c\u6765\u770b\u4e0bforRequest\u7684\u6e90\u7801\uff1a<\/strong><\/p>\n <\/p>\n \u5206\u522b\u4eceaction\u548cpicasso\u83b7\u53d6\u5230Request\u8bf7\u6c42\u548c\u6240\u6709\u7684requestHandlers\u8bf7\u6c42\u5904\u7406\u5668\uff0c\u7136\u540e\u904d\u5386\u6240\u6709\u7684\u8bf7\u6c42\u5668\u83b7\u53d6\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u5904\u7406\u5668\uff0c\u8c03\u7528canHandleRequest\u5c1d\u8bd5\u770b\u662f\u5426\u8be5\u5904\u7406\u5668\u80fd\u591f\u5904\u7406\uff0c\u6765\u770b\u4e0bcanHandleRequest\u65b9\u6cd5\uff1a<\/p>\n \u5b83\u662f\u4e00\u4e2a\u62bd\u8c61\u65b9\u6cd5\uff0c\u9700\u8981\u5230\u5b50\u7c7b\u53bb\u67e5\u627e\uff0c\u800c\u5b9e\u73b0\u5b83\u7684\u5b50\u7c7b\u90fd\u662f\u6839\u636e\u4ee5\u4e0b\u7684\u7ea6\u675f\u6761\u4ef6\u6765\u5224\u65ad\u662f\u5426\u53ef\u4ee5\u5904\u7406\u8be5\u8bf7\u6c42\u7684\uff1a<\/p>\n \u4e5f\u5c31\u662f\u8bf4\u901a\u8fc7url\u5730\u5740\u7684Scheme\u7ea6\u675f\u6761\u4ef6\u8fdb\u884c\u5224\u65ad\u7684\uff0c\u800c\u4ee5\u6211\u4eec\u73b0\u5728\u7684action\u4e2d\u7684Request\u83b7\u53d6\u5230url\uff0c\u662f\u4ee5http\/https\u5f00\u5934\u7684\uff0c\u90a3\u4e48\u6765\u770b\u4e0bNetworkRequestHandler\u4e2d\u7684canHandleRequest\u6e90\u7801\uff1a<\/p>\n \u53ef\u4ee5\u770b\u51fa\u6b63\u597d\u7684\u5339\u914d\uff0c\u7531\u6b64\u5f97\u51fa\u7ed3\u8bba\uff0c\u5c06\u8981\u5904\u7406\u6211\u4eec\u8bf7\u6c42\u6570\u636e\u7684\u5c06\u662fNetworkRequestHandler\u5904\u7406\u5668\u3002<\/p>\n \u5339\u914d\u597d\u4e86\u8bf7\u6c42\u5904\u7406\u5668\uff0c\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2aBitmapHunter\u7684\u7ebf\u7a0b\uff0c\u83b7\u53d6\u5230\u7ebf\u7a0b\u4e4b\u540e\u4f1a\u5728\u7ebf\u7a0b\u6c60\u8fdb\u884c\u5f00\u542f\u7ebf\u7a0b\uff0c\u5e76\u628a\u8be5\u7ebf\u7a0b\u5b58\u653e\u5230hunterMap\u7ebf\u7a0b\u96c6\u5408\u4e2d\u4ee5\u4fbf\u591a\u6b21\u8bf7\u6c42\u53ef\u4ee5\u5408\u5e76\u76f8\u540c\u7684\u7ebf\u7a0b\uff1a<\/p>\n ok\uff0c\u73b0\u5728\u53ef\u4ee5\u5230\u6211\u4eec\u7684\u7ebf\u7a0b\u6c60PicassoExecutorService\u4e2d\u770b\u770b\u5b83\u5230\u5e95\u662f\u600e\u4e48\u6267\u884c\u7684\uff0csubmit\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n \u7528\u628a\u6211\u4eec\u7684\u7ebf\u7a0btask\u5c01\u88c5\u5230PicassoFutureTask \u4e2d\uff0cPicassoFutureTask \u662f\u4e00\u4e2a\u66f4\u4fbf\u4e8e\u6211\u4eec\u63a7\u4f4f\u5904\u7406\u7684\u7ebf\u7a0b\uff0c\u7136\u540e\u8c03\u7528execute\u5f00\u542f\u7ebf\u7a0b\uff0c\u4e4b\u540e\u4f1a\u628a\u6211\u4eec\u7684\u7ebf\u7a0b\u8f6c\u79fb\u5230addWorker\u65b9\u6cd5\u4e2d\uff0c\u5728addWorker\u4e2d\u5f00\u542f\u7ebf\u7a0bstart\uff1a<\/p>\n \u4e0a\u8ff0\u6e90\u7801\u662f\u6267\u884c\u5728ThreadPoolExecutor\u7ebf\u7a0b\u6c60\u4e2d\u7684\uff0c\u53ef\u4ee5\u770b\u4e0b\u6e90\u7801\u3002<\/p>\n \u5f53\u7ebf\u7a0b\u5f00\u542f\u540e\uff0c\u5728\u54ea\u6267\u884c\u5462\uff1f<\/p>\n \u6211\u4eec\u77e5\u9053\u6211\u4eec\u7684\u5c01\u88c5\u7684\u7ebf\u7a0b\u6700\u521d\u59cb\u7684\u662fBitmapHunter\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u5230\u5b83\u91cc\u9762\u6765\u770b\u770b\u662f\u600e\u4e48\u6267\u884c\u7684\uff1a<\/p>\n <\/p>\n \u5728BitmapHunter\u7684run\u65b9\u6cd5\u4e2d\uff0c\u5148\u4fee\u6539\u7ebf\u7a0b\u540d\u79f0\uff0c\u7136\u540e\u6267\u884chunt\u65b9\u6cd5\uff0c\u628a\u6267\u884c\u7ed3\u679c\u5b58\u653e\u5230result\u4e2d\uff0c\u90a3\u6211\u4eec\u5148\u770b\u770bhunt\u65b9\u6cd5\u662f\u600e\u4e48\u6267\u884c\u7684\uff1a<\/p>\n \u89e3\u6790\u4e0b\u6e90\u7801\uff1a<\/p>\n \u2460\uff1a\u8fd8\u662f\u9996\u5148\u83b7\u53d6\u5230action\u884c\u4e3a\u5bf9\u5e94key\uff0c\u901a\u8fc7key\u4ece\u7f13\u5b58cache\u4e2d\u67e5\u627ebitmap\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\u4fee\u6539stats\u72b6\u6001\u5e76\u76f4\u63a5\u7684\u628abitmap\u8fd4\u56de\u3002<\/strong><\/p>\n \u2461\uff1a\u5982\u679c\u7f13\u5b58\u4e2d\u4e0d\u5b58\u5728\u5219\u662f\u8981\u53bb\u7f51\u7edc\u52a0\u8f7drequestHandler.load();\u6211\u4eec\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\u77e5\u9053\u80fd\u5904\u7406\u5f53\u524drequesr\u7684requestHandler\u662fNetworkRequestHandler\uff0c\u90a3\u4e48\u6211\u4eec\u53bbNetworkRequestHandler\u7684load\u65b9\u6cd5\u4e2d\u67e5\u770b\uff1a<\/strong><\/p>\n <\/p>\n \u8fd9\u91cc\u5f88\u6e05\u6670\u7684\u53ef\u4ee5\u770b\u5230\u662f\u76f4\u63a5\u8c03\u7528downloader\u7684load\u65b9\u6cd5\uff0c\u800c\u6211\u4eec\u7684downloader\u5728Picasso\u6784\u5efaBuilder\u7684\u65f6\u5019\u4e5f\u5f88\u6e05\u6670\u7684\u8bf4\u660e\u662fUrlConnectionDownloader\uff0c\u90a3\u4e48\u5728\u53bbUrlConnectionDownloader\u7684load\u65b9\u6cd5\u770b\u770b\uff1a<\/p>\n \u8fd9\u91cc\u7684\u4ee3\u7801\u76f8\u4fe1\u6211\u4eec\u90fd\u5f88\u719f\u6089\uff0c\u5c31\u662f\u6784\u5efa\u4e00\u4e2aHttpURLConnection<\/a> \u53bb\u8fdb\u884c\u7f51\u7edc\u8bf7\u6c42\uff0c\u5f53\u7136\u8fd8\u6709\u5c31\u662f\u6839\u636enetworkPolicy\u8fdb\u884c\u4e00\u4e9b\u7f51\u7edc\u7f13\u5b58\u7684\u7b56\u7565\u3002\u6700\u540e\u628a\u7ed3\u679c\u5b58\u653e\u5230Response\u5bf9\u8c61\u4e2d\u3002<\/p>\n \u7136\u540eNetworkRequestHandler\u7684load\u65b9\u6cd5\u53c8\u4f1a\u4eceResponse\u5bf9\u8c61\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u5e76\u628a\u5b83\u5b58\u653e\u5230Result\u5bf9\u8c61\u4e2d\u3002\u7136\u540e\u8fd4\u56de\u7ed9BitmapHunter\u4e2dhunt\u65b9\u6cd5\u7684RequestHandler.Result result\u4e2d\uff0c\u4eceresult\u4e2d\u83b7\u53d6\u8f93\u5165\u6d41\uff0c\u89e3\u6790\u8f93\u5165\u6d41\u8f6c\u5316\u4e3aBitmap\u5e76\u8fd4\u56de\u3002<\/p>\n \u5230\u6b64\u6211\u4eec\u5df2\u4ece\u7f51\u7edc\u4e2d\u4e0b\u8f7d\u4e86\u6570\u636e\uff0c\u5e76\u8f6c\u5316\u4e3abitmap\u4e86\uff0c\u7136\u540e\u5728\u8fd4\u56de\u6211\u4eec\u7684BitmapHunter\u4e2d\u7684run\u65b9\u6cd5\u4e2d\uff0c\u5728run\u65b9\u6cd5\u4e2d\u6211\u4eec\u83b7\u53d6\u5230\u4e86bitmap\uff0c\u7136\u540e\u8c03\u7528dispatcher\u8fdb\u884c\u4e8b\u7269\u5206\u53d1\uff0c\u6210\u529f\u83b7\u53d6\u5219\u8c03\u7528dispatchComplete\uff0c\u5426\u5219\u8c03\u7528dispatchFailed\u3002<\/p>\n \u4e0b\u9762\u6211\u4eec\u770b\u770bdispatcher\u4e2d\u7684dispatchComplete\u65b9\u6cd5\uff1a<\/p>\n \u540c\u6837\u7684\u9053\u7406\uff0c\u8fd9\u91cc\u7684handler\u8fd8\u662fDispatcherHandler\uff0c\u90a3\u4e48\u6765\u770b\u770b\u5b83\u7684\u5904\u7406\uff1a<\/p>\n \u8f6c\u5230dispatcher\u7684performComplete\u65b9\u6cd5\u4e2d\uff1a<\/p>\n <\/p>\n \u8fd9\u91cc\u9996\u5148\u628a\u6211\u4eec\u7684\u7ed3\u679c\u4fdd\u5b58\u5728cache\u7f13\u5b58\u4e2d\uff0c\u7136\u540e\u4ecehunterMap\u96c6\u5408\u4e2d\u79fb\u9664BitmapHunter\u5bf9\u5e94\u7684key\uff0c\u539f\u56e0\u662f\u8bf7\u6c42\u5df2\u5b8c\u6210\u3002\u7136\u540e\u8c03\u7528 batch(hunter);\u65b9\u6cd5\uff1a<\/p>\n <\/p>\n \u9996\u5148\u5224\u65adBitmapHunter\u662f\u5426\u5df2\u53d6\u6d88\uff0c\u7136\u540e\u628aBitmapHunter\u5b58\u653e\u5728\u4e00\u4e2aList< BitmapHunter > batch\u96c6\u5408\u4e2d\uff0c\u6700\u540e\u901a\u8fc7DispatcherHandler\u53d1\u9001\u4e00\u4e2a\u7a7a\u7684\u5ef6\u8fdf\u6d88\u606f\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u5ef6\u8fdf\u4e0b\u4e0b\u4e00\u4e2a\u7f51\u7edc\u52a0\u8f7d\u4ee5\u4fbf\u5904\u7406\u5f53\u524d\u7684bitmap\u5de5\u4f5c\uff0c\u6765\u770b\u4e0b\u5b83\u662f\u600e\u4e48\u5904\u7406\u7684\uff1a<\/p>\n \u8fdb\u5165performBatchComplete\u4e2d\u67e5\u770b\uff1a<\/p>\n performBatchComplete\u4e2d\u9996\u5148\u83b7\u53d6\u5b58\u653eBitmapHunter\u7684\u96c6\u5408\uff0c\u7136\u540e\u8c03\u7528mainThreadHandler\u53d1\u9001\u6d88\u606f\u3002<\/p>\n \u8fd8\u8bb0\u5f97mainThreadHandler\u662f\u600e\u4e48\u5417\uff1f<\/p>\n \u5728Picasso\u7c7b\u4e2d\u6784\u5efaBuilder\u7684build\u65b9\u6cd5\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2aDispatcher\u5bf9\u8c61\uff0c\u91cc\u9762\u4f20\u8fdb\u4e00\u4e2aHANDLER\u7684\u53c2\u6570\uff0c\u8bf7\u770b\uff1a<\/p>\n \u8fd9\u4e2aHANDLER\u5c31\u662fmainThreadHandler\u3002<\/p>\n \u90a3\u4e48\u5b83\u53d1\u9001\u7684\u6d88\u606f\u662f\u600e\u4e48\u5904\u7406\u7684\u5462\uff1f<\/p>\n <\/p>\n \u83b7\u53d6\u5230BitmapHunter\u96c6\u5408\u8fdb\u884c\u904d\u5386\uff0c\u7136\u540e\u76f4\u63a5\u8c03\u7528Picasso\u4e2d\u7684complete\u65b9\u6cd5\uff1a<\/p>\n \u5728complete\u65b9\u6cd5\u4e2d\uff0c\u76f4\u63a5\u4eceBitmapHunter\u4e2d\u83b7\u53d6\u539f\u5df2\u5c01\u88c5\u7684Action\uff0cUri\uff0cBitmap\u7b49\u7b49\u6570\u636e\u4fe1\u606f\uff0c\u7136\u540e\u8c03\u7528deliverAction\u65b9\u6cd5\uff1a<\/p>\n <\/p>\n \u8fd9\u91cc\u8fdb\u884c\u4e00\u7cfb\u5217\u7684\u5224\u65ad\uff0c\u5f53action\u6ca1\u6709\u53d6\u6d88\uff0c\u5e76\u4e14Bitmap\u4e0d\u4e3a\u7a7a\u65f6\uff0c\u5c06\u4f1a\u8c03\u7528action.complete(result, from);\u6765\u5b8c\u6210\u64cd\u4f5c\u3002<\/p>\n \u90a3\u8fd8\u8bb0\u5f97\u6211\u4eec\u5728\u521b\u5efaaction\u65f6\u7684\u64cd\u4f5c\u5417\uff1f<\/p>\n \u90a3\u4e48\u5f88\u6e05\u6670\u7684\u5c31\u77e5\u9053\u6211\u4eec\u7684Action \u5176\u5b9e\u5c31\u662fImageViewAction\u5462<\/p>\n \u90a3\u4e48\u6211\u4eec\u6765\u770b\u4e0bImageViewAction\u7684complete\u662f\u600e\u4e48\u64cd\u4f5c\u7684\u5462\uff1f<\/p>\n <\/p>\n target\u5c31\u662f\u6211\u4eec\u5728\u521b\u5efaImageViewAction\u65f6\u4f20\u9012\u7684ImageView\uff0c\u73b0\u5728\u83b7\u53d6\u5230\u5b83\uff0c\u7136\u540e\u8c03\u7528PicassoDrawable.setBitmap\u65b9\u6cd5\u6765\u5b8c\u6210\u8bbe\u7f6e\u56fe\u7247\uff1a<\/p>\n <\/p>\n \u5f53\u6211\u4eec\u770b\u5230target.setImageDrawable(drawable);\u65f6\uff0c\u662f\u4e0d\u662f\u7ec8\u4e8e\u677e\u4e86\u4e00\u53e3\u6c14\u5462\uff0c\u7ec8\u4e8e\u770b\u5230\u4e86\u4e3aImageView\u8bbe\u7f6e\u56fe\u7247\u7684\u4fe1\u606f\u4e86\u3002<\/p>\n \u597d\u4e86\uff0c\u5230\u4e86\u8fd9\u4e00\u6b65\u6574\u4e2aPicasso\u52a0\u8f7d\u56fe\u7247\u7684\u6e90\u7801\u6267\u884c\u6d41\u7a0b\u5c31\u5df2\u5b8c\u5168\u7684\u89e3\u6790\u5b8c\u6bd5\u4e86\uff0c\u76f8\u4fe1\u60a8\u53ef\u4ee5\u975e\u5e38\u6e05\u695a\u7684\u4e86\u89e3\u4e86\u6574\u4e2a\u6846\u67b6\u7684\u52a0\u8f7d\u8fc7\u7a0b\uff0c\u540c\u65f6\u6211\u4e5f\u76f8\u4fe1\u53ef\u80fd\u5f88\u5c11\u4eba\u80fd\u505a\u5230\u6211\u8fd9\u4e48\u8be6\u7ec6\u7684\u5b8c\u5b8c\u5168\u5168\u7684\u5206\u6790\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\uff0c\u800c\u4e14\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u5e76\u6ca1\u6709\u7ed9\u5927\u5bb6\u7559\u4e0b\u4ec0\u4e48\u76f2\u70b9\uff0c\u662f\u771f\u771f\u6b63\u6b63\u7684\u6e90\u7801\u5927\u89e3\u6790\u3002<\/p>\n \u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.codeceo.com\/article\/android-image-loading-frame-picasso.html<\/a><\/p>\ncompile 'com.squareup.picasso:picasso:2.5.2'<\/pre>\n
<uses-permission android:name=\"android.permission.INTERNET\"\/><\/pre>\n
Picasso.with(MainActivity.this).load(url).into(headerImage);<\/pre>\n
Picasso.with(MainActivity.this)\r\n.load(url)\r\n.placeholder(R.mipmap.ic_launcher)\r\n.into(headerImage);<\/pre>\n
Picasso.with(MainActivity.this)\r\n .load(url)\r\n .placeholder(R.mipmap.ic_launcher)\r\n .error(R.drawable.error)\r\n .into(headerImage);<\/pre>\n
private class customTransformer implements Transformation{\r\n\r\n @Override\r\n public Bitmap transform(Bitmap source) {\r\n \/\/\u5728\u8fd9\u91cc\u53ef\u4ee5\u5bf9Bitmap\u8fdb\u884c\u64cd\u4f5c\uff0c\u6bd4\u5982\u6539\u53d8\u5927\u5c0f\uff0c\u5f62\u72b6\u7b49\r\n\r\n return source;\r\n }\r\n @Override\r\n public String key() {\r\n return null;\r\n }\r\n }<\/pre>\n
Picasso.with(MainActivity.this)\r\n .load(url)\r\n .placeholder(R.mipmap.ic_launcher)\r\n .transform(new customTransformer())\r\n .error(R.drawable.error)\r\n .into(headerImage);<\/pre>\n
Picasso.with(MainActivity.this).load(url).into(headerImage);<\/pre>\n
public Builder downloader(Downloader downloader) {\r\n if (downloader == null) {\r\n throw new IllegalArgumentException(\"Downloader must not be null.\");\r\n }\r\n if (this.downloader != null) {\r\n throw new IllegalStateException(\"Downloader already set.\");\r\n }\r\n this.downloader = downloader;\r\n return this;\r\n }\r\n\r\n public Builder executor(ExecutorService executorService) {\r\n if (executorService == null) {\r\n throw new IllegalArgumentException(\"Executor service must not be null.\");\r\n }\r\n if (this.service != null) {\r\n throw new IllegalStateException(\"Executor service already set.\");\r\n }\r\n this.service = executorService;\r\n return this;\r\n }\r\n\r\n public Builder memoryCache(Cache memoryCache) {\r\n if (memoryCache == null) {\r\n throw new IllegalArgumentException(\"Memory cache must not be null.\");\r\n }\r\n if (this.cache != null) {\r\n throw new IllegalStateException(\"Memory cache already set.\");\r\n }\r\n this.cache = memoryCache;\r\n return this;\r\n }\r\n\r\n ...<\/pre>\n
\n<\/a>downloader \u4e0b\u8f7d\u5668<\/strong>
\n\u9996\u5148\u770b\u4e0bdownloader\uff0cbuilder\u4e2d\u9996\u5148\u5224\u65addownloader\u662f\u5426\u4e3a\u7a7a\u503c\uff0c\u5f53\u4e3a\u7a7a\u503c\u65f6\u5c31\u4e3a\u5b83\u521d\u59cb\u5316\u9ed8\u8ba4\u503c\uff0c\u5982\uff1a<\/p>\nif (downloader == null) {\r\n downloader = Utils.createDefaultDownloader(context);\r\n}<\/pre>\n
\n<\/a>service \u7ebf\u7a0b\u6c60<\/strong>
\n\u540c\u6837\u7684\u4f7f\u7528\uff0c\u9996\u5148\u5224\u65ad\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u521d\u59cb\u5316\u9ed8\u8ba4\u5bf9\u8c61\uff1a<\/p>\nif (service == null) {\r\n service = new PicassoExecutorService();\r\n}<\/pre>\n
public ThreadPoolExecutor(int corePoolSize,\r\n int maximumPoolSize,\r\n long keepAliveTime,\r\n TimeUnit unit,\r\n BlockingQueue<Runnable> workQueue,\r\n ThreadFactory threadFactory) {\r\n if (corePoolSize < 0 ||\r\n maximumPoolSize <= 0 ||\r\n maximumPoolSize < corePoolSize ||\r\n keepAliveTime < 0)\r\n throw new IllegalArgumentException();\r\n if (workQueue == null || threadFactory == null || handler == null)\r\n throw new NullPointerException();\r\n this.corePoolSize = corePoolSize;\r\n this.maximumPoolSize = maximumPoolSize;\r\n this.workQueue = workQueue;\r\n this.keepAliveTime = unit.toNanos(keepAliveTime);\r\n this.threadFactory = threadFactory;\r\n this.handler = handler;<\/pre>\n
\n<\/a>dispatcher \u4e8b\u52a1\u5206\u53d1\u5668<\/strong>
\ndispatcher \u5728Picasso\u4e2d\u626e\u6f14\u7740\u5341\u5206\u91cd\u8981\u7684\u89d2\u8272\uff0c\u53ef\u4ee5\u8bf4\u5b83\u662f\u6574\u4e2a\u56fe\u7247\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\u7684\u4e2d\u8f6c\u7ad9\uff0c\u4e3b\u7ebf\u7a0b\u548c\u5b50\u7ebf\u7a0b\u6765\u56de\u7684\u5207\u6362\u4e3b\u8981\u90fd\u662f\u4f9d\u8d56\u5b83\u7684\u5b58\u5728\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u6765\u4ed4\u7ec6\u7684\u7814\u7a76\u4e0b\u5b83\u7684\u8bbe\u8ba1\uff0c\u7406\u89e3\u597d\u5b83\u7684\u5b58\u5728\u5bf9\u6574\u4e2aPicasso\u6846\u67b6\u7684\u7406\u89e3\u4e5f\u57fa\u672c\u660e\u6717\u3002<\/p>\nDispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats);<\/pre>\n
static class DispatcherThread extends HandlerThread {\r\n DispatcherThread() {\r\n super(Utils.THREAD_PREFIX + DISPATCHER_THREAD_NAME, THREAD_PRIORITY_BACKGROUND);\r\n }\r\n }<\/pre>\n
@Override public void handleMessage(final Message msg) {\r\n switch (msg.what) {\r\n case REQUEST_SUBMIT: {\r\n Action action = (Action) msg.obj;\r\n dispatcher.performSubmit(action);\r\n break;\r\n }\r\n case REQUEST_CANCEL: {\r\n Action action = (Action) msg.obj;\r\n dispatcher.performCancel(action);\r\n break;\r\n }\r\n case TAG_PAUSE: {\r\n Object tag = msg.obj;\r\n dispatcher.performPauseTag(tag);\r\n break;\r\n }\r\n case TAG_RESUME: {\r\n Object tag = msg.obj;\r\n dispatcher.performResumeTag(tag);\r\n break;\r\n }\r\n case HUNTER_COMPLETE: {\r\n BitmapHunter hunter = (BitmapHunter) msg.obj;\r\n dispatcher.performComplete(hunter);\r\n break;\r\n }\r\n case HUNTER_RETRY: {\r\n BitmapHunter hunter = (BitmapHunter) msg.obj;\r\n dispatcher.performRetry(hunter);\r\n break;\r\n }\r\n case HUNTER_DECODE_FAILED: {\r\n BitmapHunter hunter = (BitmapHunter) msg.obj;\r\n dispatcher.performError(hunter, false);\r\n break;\r\n }\r\n case HUNTER_DELAY_NEXT_BATCH: {\r\n dispatcher.performBatchComplete();\r\n break;\r\n }\r\n case NETWORK_STATE_CHANGE: {\r\n NetworkInfo info = (NetworkInfo) msg.obj;\r\n dispatcher.performNetworkStateChange(info);\r\n break;\r\n }\r\n case AIRPLANE_MODE_CHANGE: {\r\n dispatcher.performAirplaneModeChange(msg.arg1 == AIRPLANE_MODE_ON);\r\n break;\r\n }\r\n default:\r\n Picasso.HANDLER.post(new Runnable() {\r\n @Override public void run() {\r\n throw new AssertionError(\"Unknown handler message received: \" + msg.what);\r\n }\r\n });\r\n }\r\n }<\/pre>\n
void dispatchNetworkStateChange(NetworkInfo info) { \r\n handler.sendMessage(handler.obtainMessage(NETWORK_STATE_CHANGE, info));\r\n}<\/pre>\n
case NETWORK_STATE_CHANGE: {\r\n NetworkInfo info = (NetworkInfo) msg.obj;\r\n dispatcher.performNetworkStateChange(info);\r\n break;\r\n }<\/pre>\n
void adjustThreadCount(NetworkInfo info) {\r\n if (info == null || !info.isConnectedOrConnecting()) {\r\n setThreadCount(DEFAULT_THREAD_COUNT);\r\n return;\r\n }\r\n switch (info.getType()) {\r\n case ConnectivityManager.TYPE_WIFI:\r\n case ConnectivityManager.TYPE_WIMAX:\r\n case ConnectivityManager.TYPE_ETHERNET:\r\n setThreadCount(4);\r\n break;\r\n case ConnectivityManager.TYPE_MOBILE:\r\n switch (info.getSubtype()) {\r\n case TelephonyManager.NETWORK_TYPE_LTE: \/\/ 4G\r\n case TelephonyManager.NETWORK_TYPE_HSPAP:\r\n case TelephonyManager.NETWORK_TYPE_EHRPD:\r\n setThreadCount(3);\r\n break;\r\n case TelephonyManager.NETWORK_TYPE_UMTS: \/\/ 3G\r\n case TelephonyManager.NETWORK_TYPE_CDMA:\r\n case TelephonyManager.NETWORK_TYPE_EVDO_0:\r\n case TelephonyManager.NETWORK_TYPE_EVDO_A:\r\n case TelephonyManager.NETWORK_TYPE_EVDO_B:\r\n setThreadCount(2);\r\n break;\r\n case TelephonyManager.NETWORK_TYPE_GPRS: \/\/ 2G\r\n case TelephonyManager.NETWORK_TYPE_EDGE:\r\n setThreadCount(1);\r\n break;\r\n default:\r\n setThreadCount(DEFAULT_THREAD_COUNT);\r\n }\r\n break;\r\n default:\r\n setThreadCount(DEFAULT_THREAD_COUNT);\r\n }\r\n }\r\n\r\n private void setThreadCount(int threadCount) {\r\n setCorePoolSize(threadCount);\r\n setMaximumPoolSize(threadCount);\r\n }<\/pre>\n
return new Picasso(context, dispatcher, cache, listener, transformer, requestHandlers, stats,defaultBitmapConfig, indicatorsEnabled, loggingEnabled);<\/pre>\n
\u2460\uff1aurl\r\npublic RequestCreator load(Uri uri) {\r\n return new RequestCreator(this, uri, 0);\r\n}\r\n\r\n\u2461\uff1apath\r\npublic RequestCreator load(String path) {\r\n if (path == null) {\r\n return new RequestCreator(this, null, 0);\r\n }\r\n if (path.trim().length() == 0) {\r\n throw new IllegalArgumentException(\"Path must not be empty.\");\r\n }\r\n return load(Uri.parse(path));\r\n}\r\n\r\n\u2462\uff1afile\r\npublic RequestCreator load(File file) {\r\n if (file == null) {\r\n return new RequestCreator(this, null, 0);\r\n }\r\n return load(Uri.fromFile(file));\r\n}\r\n\r\n\u2463\uff1aresourceId\r\npublic RequestCreator load(int resourceId) {\r\n if (resourceId == 0) {\r\n throw new IllegalArgumentException(\"Resource ID must not be zero.\");\r\n }\r\n return new RequestCreator(this, null, resourceId);\r\n}<\/pre>\n
Builder(Uri uri, int resourceId, Bitmap.Config bitmapConfig) {\r\n this.uri = uri;\r\n this.resourceId = resourceId;\r\n this.config = bitmapConfig;\r\n }<\/pre>\n
public void into(ImageView target, Callback callback) {\r\n long started = System.nanoTime();\r\n checkMain();\r\n\r\n if (target == null) {\r\n throw new IllegalArgumentException(\"Target must not be null.\");\r\n }\r\n\r\n if (!data.hasImage()) {\r\n picasso.cancelRequest(target);\r\n if (setPlaceholder) {\r\n setPlaceholder(target, getPlaceholderDrawable());\r\n }\r\n return;\r\n }\r\n\r\n if (deferred) {\r\n if (data.hasSize()) {\r\n throw new IllegalStateException(\"Fit cannot be used with resize.\");\r\n }\r\n int width = target.getWidth();\r\n int height = target.getHeight();\r\n if (width == 0 || height == 0) {\r\n if (setPlaceholder) {\r\n setPlaceholder(target, getPlaceholderDrawable());\r\n }\r\n picasso.defer(target, new DeferredRequestCreator(this, target, callback));\r\n return;\r\n }\r\n data.resize(width, height);\r\n }\r\n\r\n Request request = createRequest(started);\r\n String requestKey = createKey(request);\r\n\r\n if (shouldReadFromMemoryCache(memoryPolicy)) {\r\n Bitmap bitmap = picasso.quickMemoryCacheCheck(requestKey);\r\n if (bitmap != null) {\r\n picasso.cancelRequest(target);\r\n setBitmap(target, picasso.context, bitmap, MEMORY, noFade, picasso.indicatorsEnabled);\r\n if (picasso.loggingEnabled) {\r\n log(OWNER_MAIN, VERB_COMPLETED, request.plainId(), \"from \" + MEMORY);\r\n }\r\n if (callback != null) {\r\n callback.onSuccess();\r\n }\r\n return;\r\n }\r\n }\r\n\r\n if (setPlaceholder) {\r\n setPlaceholder(target, getPlaceholderDrawable());\r\n }\r\n\r\n Action action =\r\n new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,\r\n errorDrawable, requestKey, tag, callback, noFade);\r\n\r\n picasso.enqueueAndSubmit(action);\r\n }<\/pre>\n
void submit(Action action) {\r\n dispatcher.dispatchSubmit(action);\r\n}<\/pre>\n
void dispatchSubmit(Action action) {\r\n handler.sendMessage(handler.obtainMessage(REQUEST_SUBMIT, action));\r\n }<\/pre>\n
case REQUEST_SUBMIT: {\r\n Action action = (Action) msg.obj;\r\n dispatcher.performSubmit(action);\r\n break;<\/pre>\n
void performSubmit(Action action, boolean dismissFailed) {\r\n if (pausedTags.contains(action.getTag())) {\r\n pausedActions.put(action.getTarget(), action);\r\n if (action.getPicasso().loggingEnabled) {\r\n log(OWNER_DISPATCHER, VERB_PAUSED, action.request.logId(),\r\n \"because tag '\" + action.getTag() + \"' is paused\");\r\n }\r\n return;\r\n }\r\n\r\n BitmapHunter hunter = hunterMap.get(action.getKey());\r\n if (hunter != null) {\r\n hunter.attach(action);\r\n return;\r\n }\r\n\r\n if (service.isShutdown()) {\r\n if (action.getPicasso().loggingEnabled) {\r\n log(OWNER_DISPATCHER, VERB_IGNORED, action.request.logId(), \"because shut down\");\r\n }\r\n return;\r\n }\r\n\r\n hunter = forRequest(action.getPicasso(), this, cache, stats, action);\r\n hunter.future = service.submit(hunter);\r\n hunterMap.put(action.getKey(), hunter);\r\n if (dismissFailed) {\r\n failedActions.remove(action.getTarget());\r\n }\r\n\r\n if (action.getPicasso().loggingEnabled) {\r\n log(OWNER_DISPATCHER, VERB_ENQUEUED, action.request.logId());\r\n }\r\n }<\/pre>\n
public abstract boolean canHandleRequest(Request data);<\/pre>\n
String scheme = data.uri.getScheme();<\/pre>\n
private static final String SCHEME_HTTP = \"http\";\r\nprivate static final String SCHEME_HTTPS = \"https\";\r\n\r\n@Override \r\npublic boolean canHandleRequest(Request data) {\r\n String scheme = data.uri.getScheme();\r\n return (SCHEME_HTTP.equals(scheme) || SCHEME_HTTPS.equals(scheme));\r\n }<\/pre>\n
hunter.future = service.submit(hunter);\r\n hunterMap.put(action.getKey(), hunter);<\/pre>\n
@Override\r\n public Future<?> submit(Runnable task) {\r\n PicassoFutureTask ftask = new PicassoFutureTask((BitmapHunter) task);\r\n execute(ftask);\r\n return ftask;\r\n }<\/pre>\n
boolean workerStarted = false;\r\n boolean workerAdded = false;\r\n Worker w = null;\r\n try {\r\n w = new Worker(firstTask);\r\n final Thread t = w.thread;\r\n if (t != null) {\r\n final ReentrantLock mainLock = this.mainLock;\r\n mainLock.lock();\r\n try {\r\n \/\/ Recheck while holding lock.\r\n \/\/ Back out on ThreadFactory failure or if\r\n \/\/ shut down before lock acquired.\r\n int rs = runStateOf(ctl.get());\r\n\r\n if (rs < SHUTDOWN ||\r\n (rs == SHUTDOWN && firstTask == null)) {\r\n if (t.isAlive()) \/\/ precheck that t is startable\r\n throw new IllegalThreadStateException();\r\n workers.add(w);\r\n int s = workers.size();\r\n if (s > largestPoolSize)\r\n largestPoolSize = s;\r\n workerAdded = true;\r\n }\r\n } finally {\r\n mainLock.unlock();\r\n }\r\n if (workerAdded) {\r\n t.start();\r\n workerStarted = true;\r\n }<\/pre>\n
Bitmap hunt() throws IOException {\r\n Bitmap bitmap = null;\r\n\r\n if (shouldReadFromMemoryCache(memoryPolicy)) {\r\n bitmap = cache.get(key);\r\n if (bitmap != null) {\r\n stats.dispatchCacheHit();\r\n loadedFrom = MEMORY;\r\n if (picasso.loggingEnabled) {\r\n log(OWNER_HUNTER, VERB_DECODED, data.logId(), \"from cache\");\r\n }\r\n return bitmap;\r\n }\r\n }\r\n\r\n data.networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;\r\n RequestHandler.Result result = requestHandler.load(data, networkPolicy);\r\n if (result != null) {\r\n loadedFrom = result.getLoadedFrom();\r\n exifRotation = result.getExifOrientation();\r\n\r\n bitmap = result.getBitmap();\r\n\r\n \/\/ If there was no Bitmap then we need to decode it from the stream.\r\n if (bitmap == null) {\r\n InputStream is = result.getStream();\r\n try {\r\n bitmap = decodeStream(is, data);\r\n } finally {\r\n Utils.closeQuietly(is);\r\n }\r\n }\r\n }\r\n\r\n if (bitmap != null) {\r\n if (picasso.loggingEnabled) {\r\n log(OWNER_HUNTER, VERB_DECODED, data.logId());\r\n }\r\n stats.dispatchBitmapDecoded(bitmap);\r\n if (data.needsTransformation() || exifRotation != 0) {\r\n synchronized (DECODE_LOCK) {\r\n if (data.needsMatrixTransform() || exifRotation != 0) {\r\n bitmap = transformResult(data, bitmap, exifRotation);\r\n if (picasso.loggingEnabled) {\r\n log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId());\r\n }\r\n }\r\n if (data.hasCustomTransformations()) {\r\n bitmap = applyCustomTransformations(data.transformations, bitmap);\r\n if (picasso.loggingEnabled) {\r\n log(OWNER_HUNTER, VERB_TRANSFORMED, data.logId(), \"from custom transformations\");\r\n }\r\n }\r\n }\r\n if (bitmap != null) {\r\n stats.dispatchBitmapTransformed(bitmap);\r\n }\r\n }\r\n }\r\n\r\n return bitmap;\r\n }<\/pre>\n
protected HttpURLConnection openConnection(Uri path) throws IOException {\r\n HttpURLConnection connection = (HttpURLConnection) new URL(path.toString()).openConnection();\r\n connection.setConnectTimeout(Utils.DEFAULT_CONNECT_TIMEOUT_MILLIS);\r\n connection.setReadTimeout(Utils.DEFAULT_READ_TIMEOUT_MILLIS);\r\n return connection;\r\n }\r\n\r\n @Override public Response load(Uri uri, int networkPolicy) throws IOException {\r\n if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {\r\n installCacheIfNeeded(context);\r\n }\r\n\r\n HttpURLConnection connection = openConnection(uri);\r\n connection.setUseCaches(true);\r\n\r\n if (networkPolicy != 0) {\r\n String headerValue;\r\n\r\n if (NetworkPolicy.isOfflineOnly(networkPolicy)) {\r\n headerValue = FORCE_CACHE;\r\n } else {\r\n StringBuilder builder = CACHE_HEADER_BUILDER.get();\r\n builder.setLength(0);\r\n\r\n if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {\r\n builder.append(\"no-cache\");\r\n }\r\n if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {\r\n if (builder.length() > 0) {\r\n builder.append(',');\r\n }\r\n builder.append(\"no-store\");\r\n }\r\n\r\n headerValue = builder.toString();\r\n }\r\n\r\n connection.setRequestProperty(\"Cache-Control\", headerValue);\r\n }\r\n\r\n int responseCode = connection.getResponseCode();\r\n if (responseCode >= 300) {\r\n connection.disconnect();\r\n throw new ResponseException(responseCode + \" \" + connection.getResponseMessage(),\r\n networkPolicy, responseCode);\r\n }\r\n\r\n long contentLength = connection.getHeaderFieldInt(\"Content-Length\", -1);\r\n boolean fromCache = parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));\r\n\r\n return new Response(connection.getInputStream(), fromCache, contentLength);\r\n }<\/pre>\n
void dispatchComplete(BitmapHunter hunter) {\r\n handler.sendMessage(handler.obtainMessage(HUNTER_COMPLETE, hunter));\r\n }<\/pre>\n
case HUNTER_COMPLETE: {\r\n BitmapHunter hunter = (BitmapHunter) msg.obj;\r\n dispatcher.performComplete(hunter);\r\n break;\r\n }<\/pre>\n
case HUNTER_DELAY_NEXT_BATCH: {\r\n dispatcher.performBatchComplete();\r\n break;\r\n }<\/pre>\n
void performBatchComplete() {\r\n List<BitmapHunter> copy = new ArrayList<BitmapHunter>(batch);\r\n batch.clear();\r\n mainThreadHandler.sendMessage(mainThreadHandler.obtainMessage(HUNTER_BATCH_COMPLETE, copy));\r\n logBatch(copy);\r\n }<\/pre>\n
Dispatcher dispatcher = new Dispatcher(context, service, HANDLER, downloader, cache, stats);<\/pre>\n
void complete(BitmapHunter hunter) {\r\n Action single = hunter.getAction();\r\n List<Action> joined = hunter.getActions();\r\n\r\n boolean hasMultiple = joined != null && !joined.isEmpty();\r\n boolean shouldDeliver = single != null || hasMultiple;\r\n\r\n if (!shouldDeliver) {\r\n return;\r\n }\r\n\r\n Uri uri = hunter.getData().uri;\r\n Exception exception = hunter.getException();\r\n Bitmap result = hunter.getResult();\r\n LoadedFrom from = hunter.getLoadedFrom();\r\n\r\n if (single != null) {\r\n deliverAction(result, from, single);\r\n }\r\n\r\n if (hasMultiple) {\r\n \/\/noinspection ForLoopReplaceableByForEach\r\n for (int i = 0, n = joined.size(); i < n; i++) {\r\n Action join = joined.get(i);\r\n deliverAction(result, from, join);\r\n }\r\n }\r\n\r\n if (listener != null && exception != null) {\r\n listener.onImageLoadFailed(this, uri, exception);\r\n }\r\n }<\/pre>\n
Action action =\r\n new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,\r\n errorDrawable, requestKey, tag, callback, noFade);<\/pre>\n
\n