【移动应用开发技术】Android中如何使用Universal-Image-Loader图片异步加载框架_第1页
【移动应用开发技术】Android中如何使用Universal-Image-Loader图片异步加载框架_第2页
【移动应用开发技术】Android中如何使用Universal-Image-Loader图片异步加载框架_第3页
【移动应用开发技术】Android中如何使用Universal-Image-Loader图片异步加载框架_第4页
【移动应用开发技术】Android中如何使用Universal-Image-Loader图片异步加载框架_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】Android中如何使用Universal-Image-Loader图片异步加载框架

运行流程:/upload/information/20200623/125/123240.png使用步骤:实例:UniversalImageLoaderDemo/upload/information/20200623/125/123241.jpg运行效果:/upload/information/20200623/125/123242.jpgAndroidManifest.xml<?xml

version="1.0"

encoding="utf-8"?>

<manifest

xmlns:android="/apk/res/android"

package="com.rainsong.universalimageloaderdemo"

android:versionCode="1"

android:versionName="1.0"

>

<uses-sdk

android:minSdkVersion="8"

android:targetSdkVersion="19"

/>

<uses-permission

android:name="android.permission.WRITE_EXTERNAL_STORAGE"

/>

<uses-permission

android:name="android.permission.INTERNET"

/>

<application

android:name="com.rainsong.universalimageloaderdemo.UILApplication"

android:allowBackup="true"

android:icon="@drawable/logo"

android:label="@string/app_name"

android:theme="@style/AppTheme"

>

<activity

android:name="com.rainsong.universalimageloaderdemo.MainActivity"

android:label="@string/app_name"

>

<intent-filter>

<action

android:name="ent.action.MAIN"

/>

<category

android:name="ent.category.LAUNCHER"

/>

</intent-filter>

</activity>

</application>

</manifest>布局文件:activity_main.xml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity"

>

<ListView

android:id="@+id/list"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

</ListView>

</LinearLayout>布局文件:item_list.xml<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/apk/res/android"

android:layout_width="match_parent"

android:layout_height="wrap_content"

>

<ImageView

android:id="@+id/image"

android:layout_width="72dip"

android:layout_height="72dip"

android:layout_margin="3dip"

android:adjustViewBounds="true"

android:contentDescription="@string/descr_image"

android:scaleType="centerCrop"

/>

<TextView

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="start|center_vertical"

android:layout_marginLeft="20dip"

android:textSize="22sp"

/>

</LinearLayout>Java源代码文件:UILApplication.javapackage

com.rainsong.universalimageloaderdemo;

import

java.io.File;

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.ImageLoader;

import

com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import

com.nostra13.universalimageloader.core.assist.QueueProcessingType;

import

com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import

com.nostra13.universalimageloader.utils.StorageUtils;import

android.app.Application;

import

android.content.Context;

public

class

UILApplication

extends

Application

{

@Override

public

void

onCreate()

{

super.onCreate();

initImageLoader(getApplicationContext());

}

public

static

void

initImageLoader(Context

context)

{

//缓存文件的目录

File

cacheDir

=

StorageUtils.getOwnCacheDirectory(context,

"universalimageloader/Cache");

ImageLoaderConfiguration

config

=

new

ImageLoaderConfiguration.Builder(context)

.memoryCacheExtraOptions(480,

800)

//

max

width,

max

height,即保存的每个缓存文件的最大长宽

.threadPoolSize(3)

//线程池内线程的数量

.threadPriority(Thread.NORM_PRIORITY

-

2)

.denyCacheImageMultipleSizesInMemory()

.diskCacheFileNameGenerator(new

Md5FileNameGenerator())

//将保存的时候的URI名称用MD5

加密

.memoryCache(new

UsingFreqLimitedMemoryCache(2

*

1024

*

1024))

.memoryCacheSize(2

*

1024

*

1024)

//

内存缓存的最大值

.diskCacheSize(50

*

1024

*

1024)

//

SD卡缓存的最大值

.tasksProcessingOrder(QueueProcessingType.LIFO)

//

由原先的discCache

->

diskCache

.diskCache(new

UnlimitedDiscCache(cacheDir))//自定义缓存路径

.imageDownloader(new

BaseImageDownloader(context,

5

*

1000,

30

*

1000))

//

connectTimeout

(5

s),

readTimeout

(30

s)超时时间

.writeDebugLogs()

//

Remove

for

release

app

.build();

//全局初始化此配置

ImageLoader.getInstance().init(config);

}

}Java源代码文件:MainActivity.javapackage

com.rainsong.universalimageloaderdemo;

import

com.nostra13.universalimageloader.core.DisplayImageOptions;

import

com.nostra13.universalimageloader.core.ImageLoader;

import

com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import

android.os.Bundle;

import

android.app.Activity;

import

android.view.Menu;

import

android.view.MenuItem;

import

android.view.View;

import

android.view.ViewGroup;

import

android.widget.BaseAdapter;

import

android.widget.ImageView;

import

android.widget.ListView;

import

android.widget.TextView;

public

class

MainActivity

extends

Activity

{

private

ImageLoader

imageLoader;

private

ListView

lv;

private

String[]

imageUrls;

private

DisplayImageOptions

options;

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

imageLoader

=

ImageLoader.getInstance();

lv

=

(ListView)findViewById(R.id.list);

imageUrls

=

Constants.images;

//

使用DisplayImageOptions.Builder()创建DisplayImageOptions

options

=

new

DisplayImageOptions.Builder()

.showImageOnLoading(R.drawable.ic_stub)

//

设置图片下载期间显示的图片

.showImageForEmptyUri(R.drawable.ic_empty)

//

设置图片Uri为空或是错误的时候显示的图片

.showImageOnFail(R.drawable.ic_error)

//

设置图片加载或解码过程中发生错误显示的图片

.cacheInMemory(true)

//

设置下载的图片是否缓存在内存中

.cacheOnDisk(true)

//

设置下载的图片是否缓存在SD卡中

.displayer(new

RoundedBitmapDisplayer(20))

//

设置成圆角图片

.build();

//

构建完成

lv.setAdapter(new

ItemListAdapter());

}

@Override

public

boolean

onCreateOptionsMenu(Menu

menu)

{

//

Inflate

the

menu;

this

adds

items

to

the

action

bar

if

it

is

present.

getMenuInflater().inflate(R.menu.main,

menu);

return

true;

}

@Override

public

boolean

onOptionsItemSelected(MenuItem

item)

{

switch

(item.getItemId())

{

case

R.id.item_clear_memory_cache:

ImageLoader.getInstance().clearMemoryCache();

return

true;

case

R.id.item_clear_disc_cache:

ImageLoader.getInstance().clearDiskCache();

return

true;

default:

return

false;

}

}

class

ItemListAdapter

extends

BaseAdapter

{

@Override

public

int

getCount()

{

//

TODO

Auto-generated

method

stub

return

imageUrls.length;

}

@Override

public

Object

getItem(int

position)

{

//

TODO

Auto-generated

method

stub

return

imageUrls[position];

}

@Override

public

View

getView(int

position,

View

convertView,

ViewGroup

parent)

{

//

TODO

Auto-generated

method

stu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论