使用Android-Universal-Image-Loader加载图片

本文介绍了一个流行的Android图像加载库——Universal Image Loader,并提供了两种实例化方法。一种是在Application类中初始化,另一种是在构造函数中实例化。文章还详细展示了如何配置参数以优化图像加载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

clone地址:https://github.com/nostra13/Android-Universal-Image-Loader.git

在使用这个开源框架时,容易出错的地方是ImageLoader的实例化,因为在源码的例子中,关于ImageLoader的实例化是放在应用程序的Application配置中,在应用加载之前实现的。

当然这样的方法我们可以借鉴:

第一种实例化的办法:

<span style="font-size:18px;">/*******************************************************************************
 * Copyright 2011-2013 Sergey Tarasevich
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.nostra13.universalimageloader.sample;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
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;

/**
 * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
 */
public class UILApplication extends Application {
	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	@SuppressWarnings("unused")
	@Override
	public void onCreate() {
		if (Constants.Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
		}

		super.onCreate();

		initImageLoader(getApplicationContext());
	}

	public static void initImageLoader(Context context) {
		// This configuration tuning is custom. You can tune every option, you may tune some of them,
		// or you can create default configuration by
		//  ImageLoaderConfiguration.createDefault(this);
		// method.
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.diskCacheFileNameGenerator(new Md5FileNameGenerator())
				.diskCacheSize(50 * 1024 * 1024) // 50 Mb
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.writeDebugLogs() // Remove for release app
				.build();
		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(config);
	}
}</span>

然后在AndroidManifest.xml中注册,红色标出的地方:

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nostra13.universalimageloader.sample"
    android:versionCode="39"
    android:versionName="1.9.4" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="20" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        <span style="color:#FF0000;">android:name=".UILApplication"</span>
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
		android:allowBackup="false">
        <activity
            android:name=".activity.HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".activity.SimpleImageActivity"
            android:label="@string/ac_name_image_list" />
		<activity
			android:name=".activity.ComplexImageActivity"
			android:label="@string/ac_name_complex" />

        <!-- Widget -->
        <receiver android:name=".widget.UILWidgetProvider" >
            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_provider" />

            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
        </receiver>
    </application>

</manifest>
</span>
第二种实例化的办法:

在构造函数中进行实例化:

<span style="font-size:18px;">ImageLoaderConfiguration config = new
ImageLoaderConfiguration.Builder(context).build();
ImageLoader.getInstance().init(config);
mImageLoader = ImageLoader.getInstance();</span>
注意mImageLoader提前声明。

接下来使用它来加载图片就在github的例子中给出了。

///////////////////////////////////////////////////////////////////////////////学习的教程

http://www.cnblogs.com/kissazi2/p/3886563.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值