本文主要是介绍ImageLoader初见,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
下载项目地址
点击打开链接
activity_main.xml主页布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:orientation="vertical"android:layout_height="fill_parent" ><ImageViewandroid:id="@+id/image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@drawable/ic_launcher" /><ImageViewandroid:id="@+id/image2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@drawable/ic_launcher" /></LinearLayout>
权限
<uses-permission android:name="android.permission.INTERNET" /> <!-- Include next permission if you want to allow UIL to cache images on SD card --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MyApplication
package com.example.imageloader;import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import android.app.Application;public class MyApplication extends Application {@Overridepublic void onCreate() {super.onCreate();// 创建默认的ImageLoader配置参数ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);// Initialize ImageLoader with configuration.ImageLoader.getInstance().init(configuration);}}
MainActivity
package com.example.imageloader;import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.download.ImageDownloader.Scheme;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;public class MainActivity extends Activity {private ImageView mImageView;private ImageView image2;public static final String CACHE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zhbj_cache_52";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mImageView = (ImageView) findViewById(R.id.image);image2 = (ImageView) findViewById(R.id.image2);String imageUrl = "http://img.mukewang.com/552640c300018a9606000338-300-170.jpg";ImageSize mImageSize = new ImageSize(100, 100);DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();/*** loadImage()加载图片*/ImageLoader.getInstance().loadImage(imageUrl, mImageSize, options,new SimpleImageLoadingListener() {@Overridepublic void onLoadingStarted(String imageUri, View view) {}@Overridepublic void onLoadingFailed(String imageUri, View view,FailReason failReason) {}@Overridepublic void onLoadingComplete(String imageUri, View view,Bitmap loadedImage) {mImageView.setImageBitmap(loadedImage);}@Overridepublic void onLoadingCancelled(String imageUri, View view) {}});/*** displayImage()加载图片*/DisplayImageOptions options2 = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher).showImageOnFail(R.drawable.checked).cacheInMemory(true).cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();ImageLoader.getInstance().displayImage(imageUrl, image2, options2);ImageLoader.getInstance().displayImage(imageUrl, image2, options2,new SimpleImageLoadingListener(),new ImageLoadingProgressListener() {@Overridepublic void onProgressUpdate(String arg0, View arg1,int arg2, int arg3) {}});}}
这篇关于ImageLoader初见的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!