Android中怎么隔離第三方庫(kù)

本篇文章給大家分享的是有關(guān)Android中怎么隔離第三方庫(kù),小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

成都創(chuàng)新互聯(lián)公司專(zhuān)業(yè)為企業(yè)提供金湖網(wǎng)站建設(shè)、金湖做網(wǎng)站、金湖網(wǎng)站設(shè)計(jì)、金湖網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、金湖企業(yè)網(wǎng)站模板建站服務(wù),10年金湖做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

首先抽象一個(gè)ImageLoader接口

**  * 圖片加載器功能接口;  * 添加或者替換新的圖片加載器實(shí)現(xiàn)該接口即可  */public interface ImageLoader {    /**      * Init ImageLoader      */     void init(Context context);    /**      * Show Image      *      * @param imageUrl      * @param imageView      * @param defaultImage      */     void displayImage(String imageUrl, ImageView imageView, int defaultImage);  }

我們當(dāng)前是使用UniversalImageLoader來(lái)展示項(xiàng)目中的圖片我們就建一個(gè)UniversalImageLoader類(lèi)來(lái)實(shí)現(xiàn)上面的接口

public class UniversalImageLoader implements ImageLoader {    private final long discCacheLimitTime = 3600 * 24 * 15L;    private com.nostra13.universalimageloader.core.ImageLoader imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();    @Override     public void init(Context context) {        if (!imageLoader.isInited()) {             ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(                     context)                     .threadPriority(Thread.NORM_PRIORITY)                     .denyCacheImageMultipleSizesInMemory()                     .memoryCache(new WeakMemoryCache())                     .memoryCacheSize((2 * 1024 * 1024))                     .memoryCacheSizePercentage(13)                     .discCacheFileNameGenerator(new Md5FileNameGenerator())                     .discCache(                            new LimitedAgeDiskCache(StorageUtils                                     .getCacheDirectory(context),                                     discCacheLimitTime))                     .tasksProcessingOrder(QueueProcessingType.LIFO).build();             com.nostra13.universalimageloader.core.ImageLoader.getInstance().init(config);         }     }    @Override     public void displayImage(String uri, ImageView img, int default_pic) {         DisplayImageOptions options = new DisplayImageOptions.Builder()                 .showImageOnLoading(default_pic)                 .showImageForEmptyUri(default_pic).showImageOnFail(default_pic)                 .cacheInMemory(true).cacheOnDisc(true)                 .bitmapConfig(Bitmap.Config.RGB_565)                 .displayer(new SimpleBitmapDisplayer()).build();         imageLoader.displayImage(uri, img, options);     }

接下來(lái)我們寫(xiě)一個(gè)代理類(lèi)來(lái)幫我們實(shí)現(xiàn)圖片加載的任務(wù)

/**  * 圖片加載代理類(lèi),所有的圖片操作都通過(guò)該代理類(lèi)去實(shí)現(xiàn);  * 如果要改變圖片加載框架,只需要在該類(lèi)里替換相應(yīng)的圖片加載框架即可,客戶(hù)端所有引用的圖片操作都不需要修改   */public class ImageLoaderProxy implements ImageLoader {    private ImageLoader imageLoader;//代理對(duì)象     private static ImageLoaderProxy imageLoaderProxy;    public static ImageLoaderProxy getInstance() {        if (imageLoaderProxy == null) {             imageLoaderProxy = new ImageLoaderProxy();         }        return imageLoaderProxy;     }    public ImageLoaderProxy() {         imageLoader= new UniversalImageLoader();     }    @Override     public void init(Context context) {         imageLoader.init(context);     }    @Override     public void displayImage(String imageUrl, ImageView imageView, int defaultImage) {         imageLoader.displayImage(imageUrl, imageView, defaultImage);     }  }

這樣客戶(hù)端所有需要顯示圖片的地方只需要調(diào)用代理類(lèi)的圖片顯示方法即可  ImageLoaderProxy.getInstance().displayImage();

當(dāng)老板讓我們換成Picasso來(lái)完成圖片加載時(shí) ,我們只需增加一個(gè) PicassoImageLoader類(lèi)然后將代理類(lèi)中的這行代碼  imageLoaderProxy = new UniversalImageLoader();換成imageLoaderProxy = new  PicassoImageLoader()即可。

以上就是Android中怎么隔離第三方庫(kù),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站題目:Android中怎么隔離第三方庫(kù)
本文鏈接:http://muchs.cn/article20/gjgcjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、自適應(yīng)網(wǎng)站、企業(yè)建站面包屑導(dǎo)航、電子商務(wù)網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站