{"id":933,"date":"2023-03-11T17:58:04","date_gmt":"2023-03-11T09:58:04","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=933"},"modified":"2023-04-29T15:44:55","modified_gmt":"2023-04-29T07:44:55","slug":"android-componentization-development-imageloader-packaging","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/11\/android-componentization-development-imageloader-packaging\/","title":{"rendered":"Android\u7ec4\u4ef6\u5316\u5f00\u53d1\u4e4bImageLoader\u5c01\u88c5"},"content":{"rendered":"<h2>ImageLoader\u7b80\u4ecb<\/h2>\n<p>Github\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/nostra13\/Android-Universal-Image-Loader\">https:\/\/github.com\/nostra13\/Android-Universal-Image-Loader<\/a><\/p>\n<p>\u7279\u5f81<\/p>\n<p><!-- more --><\/p>\n<ul>\n<li>\u591a\u7ebf\u7a0b\u4e0b\u8f7d\u56fe\u7247\uff0c\u56fe\u7247\u53ef\u4ee5\u6765\u6e90\u4e8e\u7f51\u7edc\uff0c\u6587\u4ef6\u7cfb\u7edf\uff0c\u9879\u76ee\u6587\u4ef6\u5939assets\u4e2d\u4ee5\u53cadrawable\u4e2d\u7b49<\/li>\n<li>\u652f\u6301\u968f\u610f\u7684\u914d\u7f6eImageLoader\uff0c\u4f8b\u5982\u7ebf\u7a0b\u6c60\uff0c\u56fe\u7247\u4e0b\u8f7d\u5668\uff0c\u5185\u5b58\u7f13\u5b58\u7b56\u7565\uff0c\u786c\u76d8\u7f13\u5b58\u7b56\u7565\uff0c\u56fe\u7247\u663e\u793a\u9009\u9879\u4ee5\u53ca\u5176\u4ed6\u7684\u4e00\u4e9b\u914d\u7f6e<\/li>\n<li>\u652f\u6301\u56fe\u7247\u7684\u5185\u5b58\u7f13\u5b58\uff0c\u6587\u4ef6\u7cfb\u7edf\u7f13\u5b58\u6216\u8005SD\u5361\u7f13\u5b58<\/li>\n<li>\u652f\u6301\u56fe\u7247\u4e0b\u8f7d\u8fc7\u7a0b\u7684\u76d1\u542c<\/li>\n<li>\u6839\u636e\u63a7\u4ef6(ImageView)\u7684\u5927\u5c0f\u5bf9Bitmap\u8fdb\u884c\u88c1\u526a\uff0c\u51cf\u5c11Bitmap\u5360\u7528\u8fc7\u591a\u7684\u5185\u5b58<\/li>\n<li>\u8f83\u597d\u7684\u63a7\u5236\u56fe\u7247\u7684\u52a0\u8f7d\u8fc7\u7a0b\uff0c\u4f8b\u5982\u6682\u505c\u56fe\u7247\u52a0\u8f7d\uff0c\u91cd\u65b0\u5f00\u59cb\u52a0\u8f7d\u56fe\u7247\uff0c\u4e00\u822c\u4f7f\u7528\u5728ListView\u3001GridView\u4e2d\uff0c\u6ed1\u52a8\u8fc7\u7a0b\u4e2d\u6682\u505c\u52a0\u8f7d\u56fe\u7247\uff0c\u505c\u6b62\u6ed1\u52a8\u7684\u65f6\u5019\u53bb\u52a0\u8f7d\u56fe\u7247<\/li>\n<li>\u63d0\u4f9b\u5728\u8f83\u6162\u7684\u7f51\u7edc\u4e0b\u5bf9\u56fe\u7247\u8fdb\u884c\u52a0\u8f7d<\/li>\n<\/ul>\n<h2>ImageLoader\u5c01\u88c5<\/h2>\n<pre><code>public class ImageLoaderManager {\n    private static final int THREAD_COUNT = 4;\n    private static final int PRIORITY = 2;\n    private static final int MEMORY_CACHE_SIZE = 2 * 1024 * 1024;\n    private static final int DISK_CACHE_SIZE = 50 * 1024 * 1024;\n    private static final int CONNECTION_TIME_OUT = 5 * 1000;\n    private static final int READ_TIME_OUT = 30 * 1000;\n\n    private static ImageLoaderManager mInstance = null;\n    private static ImageLoader mImageLoader = null;\n\n    public static ImageLoaderManager getInstance(Context context) {\n        if (mInstance == null) {\n            synchronized (ImageLoaderManager.class) {\n                if (mInstance == null) {\n                    mInstance = new ImageLoaderManager(context);\n                }\n            }\n        }\n        return mInstance;\n    }\n\n    \/**\n     * \u79c1\u6709\u6784\u9020\u65b9\u6cd5\u5b8c\u6210\u521d\u59cb\u5316\u5de5\u4f5c\n     *\n     * @param context\n     *\/\n    private ImageLoaderManager(Context context) {\n        ImageLoaderConfiguration config = new ImageLoaderConfiguration\n            .Builder(context)\n            .threadPoolSize(THREAD_COUNT)\n            .threadPriority(Thread.NORM_PRIORITY - PRIORITY)\n            .denyCacheImageMultipleSizesInMemory()\n            \/\/.memoryCache(new UsingFreqLimitedMemoryCache(MEMORY_CACHE_SIZE))\n            .memoryCache(new WeakMemoryCache())\n            .diskCacheSize(DISK_CACHE_SIZE)\n            .diskCacheFileNameGenerator(new Md5FileNameGenerator())\/\/\u5c06\u4fdd\u5b58\u7684\u65f6\u5019\u7684URI\u540d\u79f0\u7528MD5 \u52a0\u5bc6\n            .tasksProcessingOrder(QueueProcessingType.LIFO)\n            .defaultDisplayImageOptions(getDefaultOptions())\n            .imageDownloader(new BaseImageDownloader(context, CONNECTION_TIME_OUT, READ_TIME_OUT))\n            .writeDebugLogs()\n            .build();\n\n        ImageLoader.getInstance().init(config);\n        mImageLoader = ImageLoader.getInstance();\n    }\n\n    \/\/load the image\n    public void displayImage(ImageView imageView, String path, ImageLoadingListener listener) {\n        if (mImageLoader != null) {\n            mImageLoader.displayImage(path, imageView, listener);\n        }\n    }\n\n    public void displayImage(ImageView imageView, String path) {\n        displayImage(imageView, path, null);\n    }\n\n    \/**\n     * \u9ed8\u8ba4\u7684\u56fe\u7247\u663e\u793aOptions,\u53ef\u8bbe\u7f6e\u56fe\u7247\u7684\u7f13\u5b58\u7b56\u7565\uff0c\u7f16\u89e3\u7801\u65b9\u5f0f\u7b49\uff0c\u975e\u5e38\u91cd\u8981\n     * @return\n     *\/\n    private DisplayImageOptions getDefaultOptions() {\n\n        DisplayImageOptions options = new\n            DisplayImageOptions.Builder()\n            .showImageForEmptyUri(R.drawable.default_user_avatar)\n            .showImageOnFail(R.drawable.default_user_avatar)\n            .cacheInMemory(true)  \/\/\u8bbe\u7f6e\u4e0b\u8f7d\u7684\u56fe\u7247\u662f\u5426\u7f13\u5b58\u5728\u5185\u5b58\u4e2d\n            .cacheOnDisk(true)  \/\/\u8bbe\u7f6e\u4e0b\u8f7d\u7684\u56fe\u7247\u662f\u5426\u7f13\u5b58\u5728SD\u5361\u4e2d\n            .considerExifParams(true)  \/\/\u662f\u5426\u8003\u8651JPEG\u56fe\u50cfEXIF\u53c2\u6570\uff08\u65cb\u8f6c\uff0c\u7ffb\u8f6c\uff09\n            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)  \/\/\u8bbe\u7f6e\u56fe\u7247\u4ee5\u5982\u4f55\u7684\u7f16\u7801\u65b9\u5f0f\u663e\u793a\n            .bitmapConfig(Bitmap.Config.RGB_565)  \/\/\u8bbe\u7f6e\u56fe\u7247\u7684\u89e3\u7801\u7c7b\u578b\/\/\n            .decodingOptions(new BitmapFactory.Options())  \/\/\u8bbe\u7f6e\u56fe\u7247\u7684\u89e3\u7801\u914d\u7f6e\n            .resetViewBeforeLoading(true)  \/\/\u8bbe\u7f6e\u56fe\u7247\u5728\u4e0b\u8f7d\u524d\u662f\u5426\u91cd\u7f6e\n            .build();\n        return options;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ImageLoader\u7b80\u4ecb Github\uff1ahttps:\/\/github.com\/nostra13\/Androi [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[206],"tags":[255],"class_list":["post-933","post","type-post","status-publish","format-standard","hentry","category-android-image-loader","tag-imageloader"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/933","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=933"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/933\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}