小超的实习生涯之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),别把权限搞忘咾!(网络访问权限和写入内存权限)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值