public class MainActivity extends Activity {
private static final String imageUrl1 = "http://img.ivsky.com/img/bizhi/co/201601/19/linyun-001.jpg";//330*225
private static final String imageUrl2 = "http://image52.360doc.com/DownloadImg/2012/06/0316/24581213_1.jpg";//1280*720
private static final String imageUrl3 = "http://img.bimg.126.net/photo/rl0IM2SIJK8jWXgIgxhJsw==/2871889187379358521.jpg";//3216*2028
private static final String imageUrl4 = "http://img.bimg.126.net/photo/rl0IM2SIJK8jWXgIgxhJsw==/2871889187379358521.jpg";
private static final String imageUrl5 = "http://img.ivsky.com/img/bizhi/co/201601/19/fireworks-005.jpg";
private static final String imageUrl6 = Scheme.FILE.wrap(Environment.getExternalStorageDirectory().getPath() + "/very_big.jpg");//超级大图6000*8000
private static final String imageUrl7 = "file:///mnt/sdcard/video.mp4";//可以是视频文件,此工具会自动加载视频的缩略图
private static final String imageUrl8 = "assets://big.jpg";//超大图,3600*5400
private static final String imageUrl9 = "drawable://" + R.drawable.ic_launcher;
private static final String imageUrl10 = "content://media/external/audio/albumart/a";//加载失败
private static final int[] ids = { R.id.iv1, R.id.iv2, R.id.iv3, R.id.iv4, R.id.iv5, R.id.iv6, R.id.iv7, R.id.iv8, R.id.iv9, R.id.iv10 };
private ImageView[] imageViews = new ImageView[ids.length];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < ids.length; i++) {
imageViews[i] = (ImageView) findViewById(ids[i]);
}
//1、默认,330*225
ImageLoader.getInstance().loadImage(imageUrl1, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
imageViews[0].setImageBitmap(loadedImage);
Log.i("bqt", loadedImage.getWidth() + "-1-" + loadedImage.getHeight());//330*225。和原图大小一致
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
//2、指定图片的大小。注意,这里的大小并不一定有用,因为图片的宽高只会同时被缩放为2的n次幂的倍数。这是一个在很多地方都通用的规则。
ImageLoader.getInstance().loadImage(imageUrl2, new ImageSize(100, 100), new SimpleImageLoadingListener() {//使用缺省适配器
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
imageViews[1].setImageBitmap(loadedImage);
Log.i("bqt", loadedImage.getWidth() + "-2-" + loadedImage.getHeight());//320*180。原图大小1280*720
}
});
//3、配置一些图片显示的选项。测试时发现加载中以及加载失败时设置显示的图片均没有显示出来!
DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true).cacheOnDisk(true)//将图片缓存到内存和硬盘中
.showImageOnLoading(R.drawable.loading).showImageOnFail(R.drawable.failed)//设置加载中以及图片加载出现错误时显示的图片
.bitmapConfig(Bitmap.Config.RGB_565).build();
ImageLoader.getInstance().loadImage(imageUrl3, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
super.onLoadingComplete(imageUri, view, loadedImage);
Log.i("bqt", loadedImage.getWidth() + "-3-" + loadedImage.getHeight());//1608*1014。原图3216*2028。宽高自动缩放为1/2
imageViews[2].setImageBitmap(loadedImage);
}
});
//**********************************************************建议用displayImage加载图片************************************************************
ImageLoader.getInstance().displayImage(imageUrl4, imageViews[3], options);
ImageLoader.getInstance().displayImage(imageUrl5, imageViews[4], options, null, new ImageLoadingProgressListener() {//设置图片加载进度监听
@Override
public void onProgressUpdate(String imageUri, View view, int current, int total) {
Log.i("bqt", current + "----" + total);
}
});
ImageLoader.getInstance().displayImage(imageUrl6, imageViews[5], options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String paramString, View paramView, Bitmap loadedImage) {
Log.i("bqt", loadedImage.getWidth() + "-6-" + loadedImage.getHeight());//750*1000。原图6000*8000。宽高自动缩放为1/8。
}
});
ImageLoader.getInstance().displayImage(imageUrl7, imageViews[6], options);
ImageLoader.getInstance().displayImage(imageUrl8, imageViews[7], options);//或使用【Scheme.ASSETS.wrap("a.png")】
ImageLoader.getInstance().displayImage(imageUrl9, imageViews[8], options);
ImageLoader.getInstance().displayImage(imageUrl10, imageViews[9], options);
}
}