您的位置:首页 > 移动开发 > Android开发

小超的实习生涯之ImageLoader

2015-07-22 11:14 483 查看
盼了好久终于盼到今天,问我今天什么日子?偷偷告诉你今天是我实习生涯的第一天。来到这陌生的环境,难免有些紧张。不过熟悉的编程语言,却让我感到无比的兴奋,终于可以和技术大牛们在一起探讨编程知识了。由于我是新手,所以老大叫我先复习一下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);
}
}





第三步
[b]activity_main.xml
[/b]

</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>


第四步 [b]Mainactivity设置ImageLoader相关属性[/b]

<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),别把权限搞忘咾!(网络访问权限和写入内存权限)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ImageLoader android 技术