今天来介绍图片加载的框架Android-Universal-Image-Loader
GITHUB上的下载路径为:https://github.com/nostra13/Android-Universal-Image-Loader
也可以自行百度下载。
首先来封装的一个类CacheTool ,由于其他加载图片的方法有点繁琐,所以这里仅封装了一个简单实用的加载方法:
import android.graphics.Bitmap; import android.widget.ImageView; import com.ncct.app.R; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.assist.ImageLoadingListener; /** * 图片加载框架 * * @author jiang * */ public class CacheTool { //showStubImage 加载中显示的图片
//showImageForEmptyUri 404显示的图片
//showImageOnFail 加载失败显示的图片 private static DisplayImageOptions options = new DisplayImageOptions.Builder().showStubImage(R.drawable.loading_img)
.showImageForEmptyUri(R.drawable.loading_error).showImageOnFail(R.drawable.loading_error) .cacheInMemory(true).cacheOnDisc(true).bitmapConfig(Bitmap.Config.RGB_565).build(); /** * 加载图片并监听回调结果 * * @param iv * @param url * @param mImageLoadingListener */ public static void displayImg(ImageView iv, String url, ImageLoadingListener mImageLoadingListener) { ImageLoader.getInstance().displayImage(url, iv, options, mImageLoadingListener); } /** * 加载图片 * * @param iv * @param url */ public static void displayImg(ImageView iv, String url) { ImageLoader.getInstance().displayImage(url, iv, options); } /** * 清除内存 */ public static void clearMemoryCache() { ImageLoader.getInstance().clearMemoryCache(); } /** * 清除缓存 */ public static void clearDiskCache() { ImageLoader.getInstance().clearDiscCache(); } }
封装好了,里面都有详细的介绍,这里介绍下上面的中的ImageLoadingListener 接口回调,按ctrl + 鼠标左键可以进入jar包里的java文件:
/******************************************************************************* * 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.core.assist; import android.graphics.Bitmap; import android.view.View; /** * Listener for image loading process.<br /> * You can use {@link SimpleImageLoadingListener} for implementing only needed methods. * * @author Sergey Tarasevich (nostra13[at]gmail[dot]com) * @see SimpleImageLoadingListener * @see FailReason * @since 1.0.0 */ public interface ImageLoadingListener { /** * Is called when image loading task was started * * @param imageUri Loading image URI * @param view View for image */ void onLoadingStarted(String imageUri, View view); /** * Is called when an error was occurred during image loading * * @param imageUri Loading image URI * @param view View for image. Can be <b>null</b>. * @param failReason {@linkplain FailReason The reason} why image loading was failed */ void onLoadingFailed(String imageUri, View view, FailReason failReason); /** * Is called when image is loaded successfully (and displayed in View if one was specified) * * @param imageUri Loaded image URI * @param view View for image. Can be <b>null</b>. * @param loadedImage Bitmap of loaded and decoded image */ void onLoadingComplete(String imageUri, View view, Bitmap loadedImage); /** * Is called when image loading task was cancelled because View for image was reused in newer task * * @param imageUri Loading image URI * @param view View for image. Can be <b>null</b>. */ void onLoadingCancelled(String imageUri, View view); }
从以上代码中我们可以了解到接口中我们可以监听到开始、失败、完成、取消的动作。
现在开始使用吧:
private ImageView My_Head; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.personcenter); My_Head = (ImageView) findViewById(R.id.My_Head); String Url = "http://pic.nipic.com/2007-11-09/200711912453162_2.jpg"; CacheTool.displayImg(My_Head , Url ); }
时间: 2024-12-28 00:58:05