小超的實習生涯之ImageLoader

盼了好久終於盼到今天,問我今天什麼日子?偷偷告訴你今天是我實習生涯的第一天。來到這陌生的環境,難免有些緊張。不過熟悉的編程語言,卻讓我感到無比的興奮,終於可以和技術大牛們在一起探討編程知識了。由於我是新手,所以老大叫我先複習一下Android編程知識,任務是:ImageLoader加載圖片。額...這個以前就沒用過,沒辦法只有學習學習咯,在網上找了許多資料,花了一天時間終於搞懂這個玩意兒了,其實ImageLoader並不難,只是換了開發工具不太習慣,(eclipse->andriod studio)。好了不說廢話了,直入主題,

第一步:導入jar包:universal-image-loader-1.9.4.jar

下載地址:https://github.com/nostra13/Android-Universal-Image-Loader

第二步:重寫Application,配置ImageLoaderConfiguration相關參數

<pre name="code" class="java">
import android.app.Application;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

/**
 * Created by Spaceboy on 2015/7/21.
 */
public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPriority(Thread.NORM_PRIORITY-2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(configuration);
    }
}




第三步 
activity_main.xml

</pre><pre name="code" class="java">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:id="@+id/imageView"/>
    
</LinearLayout>

</pre><p></p><pre>

第四步 Mainactivity設置ImageLoader相關屬性

<pre name="code" class="java">import android.app.Activity;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;



/**
 * Created by Spaceboy on 2015/7/21.
 */
public class MainActivity extends Activity{


    private DisplayImageOptions options;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView)findViewById(R.id.imageView);

        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.img_empty)
                .showStubImage(R.drawable.img_loading)
                .showImageOnLoading(R.drawable.img_loading)
                .showImageOnFail(R.drawable.img_error)
                .delayBeforeLoading(4000)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoader.getInstance().displayImage("http://p0.so.qhimg.com/bdr/_240_/t018a848aa56176d12f.jpg", imageView, options);
    }
    }


第五步:配置Androidmanifest.xml中網絡訪問權限和寫入內存權限,設置application的name=".MyApplication"

<pre name="code" class="html">    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <application android:allowBackup="true" android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme"
        android:name=".MyApplication">
        ..</application>



最後總結兩句話:

先導相關的Jar包(universal-image-loader-1.9.4.jar),再將參數配置好。(ImageLoaderConfiguration).

後將屬性詳設定(DisplayImageOptions),別把權限搞忘咾!(網絡訪問權限和寫入內存權限)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章