android網絡圖片加載處理庫Picasso和universalimageloader使用簡單案例


一、Picasso

1、導入jar包到項目libs目錄下。 (picasso-2.4.0.jar)

2、調用api。示例

        iv_img = (ImageView) this.findViewById(R.id.iv_img);
        Picasso.with(this)
                .load("https://www.baidu.com/img/bdlogo.png")
                .into(iv_img);
        Picasso.with(this)
                .load("https://www.baidu.com/img/bdlogo.png")
                .resize(50, 50).into(iv_img);
        Picasso.with(this)
                .load("https://www.baidu.com/img/bdlogo.png")
                .error(R.drawable.ic_launcher).into(iv_img);
    <ImageView
        android:id="@+id/iv_img"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

 

 

二、universalimageloader使用

1、導入jar包到項目libs目錄下。 (universal-image-loader-1.9.3.jar)

2、創建自定義Application,並配置到androidMainfest.xml中。

    <application
        android:name="com.mytest.imageloaderdemo.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
package com.mytest.imageloaderdemo;

import java.io.File;

import android.app.Application;
import android.graphics.Bitmap;
import android.os.Environment;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;


public class MyApplication extends Application {
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this)
                .memoryCacheExtraOptions(480, 800)
                // max width, max height,即保存的每個緩存文件的最大長寬
                .discCacheExtraOptions(480, 800, null)
                // Can slow ImageLoader, use it carefully (Better don't use
                // it)/設置緩存的詳細信息,最好不要設置這個
                .threadPoolSize(3)
                // 線程池內加載的數量
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
                // You can pass your own memory cache
                // implementation/你可以通過自己的內存緩存實現
                .memoryCacheSize(2 * 1024 * 1024)
                .discCacheSize(50 * 1024 * 1024)
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                // 將保存的時候的URI名稱用MD5 加密
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .discCacheFileCount(100)
                // 緩存的文件數量
                .discCache(
                        new UnlimitedDiscCache(new File(Environment
                                .getExternalStorageDirectory()
                                + "/myApp/imgCache")))
                // 自定義緩存路徑
                .defaultDisplayImageOptions(getDisplayOptions())
                .imageDownloader(
                        new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
                .writeDebugLogs() // Remove for release app
                .build();// 開始構建
        ImageLoader.getInstance().init(config);
    }

    private DisplayImageOptions getDisplayOptions() {
        DisplayImageOptions options;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher) // 設置圖片在下載期間顯示的圖片
                .showImageForEmptyUri(R.drawable.ic_launcher)// 設置圖片Uri為空或是錯誤的時候顯示的圖片
                .showImageOnFail(R.drawable.ic_launcher) // 設置圖片加載/解碼過程中錯誤時候顯示的圖片
                .cacheInMemory(true)// 設置下載的圖片是否緩存在內存中
                .cacheOnDisc(true)// 設置下載的圖片是否緩存在SD卡中
                .considerExifParams(true) // 是否考慮JPEG圖像EXIF參數(旋轉,翻轉)
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 設置圖片以如何的編碼方式顯示
                .bitmapConfig(Bitmap.Config.RGB_565)// 設置圖片的解碼類型//
                // .delayBeforeLoading(int delayInMillis)//int
                // delayInMillis為你設置的下載前的延遲時間
                // 設置圖片加入緩存前,對bitmap進行設置
                // .preProcessor(BitmapProcessor preProcessor)
                .resetViewBeforeLoading(true)// 設置圖片在下載前是否重置,復位
                .displayer(new RoundedBitmapDisplayer(20))// 是否設置為圓角,弧度為多少
                .displayer(new FadeInBitmapDisplayer(100))// 是否圖片加載好后漸入的動畫時間
                .build();// 構建完成
        return options;
    }
}
View Code

 

調用代碼

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;


    private ImageLoader loader;
    private ImageView iv_img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loader = ImageLoader.getInstance();
        iv_img = (ImageView) this.findViewById(R.id.iv_img);
        String uri = "file:///" + "本地路徑";
//        loader.displayImage(
//                "https://www.baidu.com/img/bdlogo.png",
//                iv_img);
        loader.displayImage(
                "https://www.baidu.com/img/bdlogo.png",
                iv_img, new ImageLoadingListener() {

                    @Override
                    public void onLoadingStarted(String arg0, View arg1) {
                        Log.i("info", "onLoadingStarted");
                    }

                    @Override
                    public void onLoadingFailed(String arg0, View arg1,
                            FailReason arg2) {
                        Log.i("info", "onLoadingFailed");
                    }

                    @Override
                    public void onLoadingComplete(String arg0, View arg1,
                            Bitmap arg2) {
                        Log.i("info", "onLoadingComplete");
                    }

                    @Override
                    public void onLoadingCancelled(String arg0, View arg1) {
                        Log.i("info", "onLoadingCancelled");
                    }
                });
    }
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM