Android-Universal-Image-Loader
github地址:https://github.com/nostra13/Android-Universal-Image-Loader
加载缓存网络图片的工具,此工具的最大优点是提供了下载缓存进度接口,缺点需要自己处理加载后的图片
使用效果:
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;/** 图片下载demo* https://github.com/nostra13/Android-Universal-Image-Loader*/
public class UnlversalImageLoaderDemo extends AppCompatActivity implements View.OnClickListener {private Button mBtn1,mBtn2,mBtn3,mBtn4,mBtn5,mBtn6;private ImageView mImage;private ImageLoader mImageLoader;private Bitmap mBitmap;private DisplayImageOptions mOptions;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_unlversal_image_loader);mBtn1 = (Button)findViewById(R.id.btn1);mBtn2 = (Button)findViewById(R.id.btn2);mBtn3 = (Button)findViewById(R.id.btn3);mBtn4 = (Button)findViewById(R.id.btn4);mBtn5 = (Button)findViewById(R.id.btn5);mBtn6 = (Button)findViewById(R.id.btn6);mBtn1.setOnClickListener(this);mBtn2.setOnClickListener(this);mBtn3.setOnClickListener(this);mBtn4.setOnClickListener(this);mBtn5.setOnClickListener(this);mBtn6.setOnClickListener(this);mImage = (ImageView)findViewById(R.id.image);/*** 初始化工作*/ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(UnlversalImageLoaderDemo.this).build();mImageLoader = ImageLoader.getInstance();mImageLoader.init(imageLoaderConfiguration);/* 选项功能模板DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub) // resource or drawable.showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable.showImageOnFail(R.drawable.ic_error) // resource or drawable.resetViewBeforeLoading(false) // default.delayBeforeLoading(1000).cacheInMemory(false) // default.cacheOnDisk(false) // default.preProcessor(...).postProcessor(...).extraForDownloader(...).considerExifParams(false) // default.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default.bitmapConfig(Bitmap.Config.ARGB_8888) // default.decodingOptions(...).displayer(new SimpleBitmapDisplayer()) // default.handler(new Handler()) // default.build();*/mOptions = new DisplayImageOptions.Builder().build();}@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.btn1:/*** 第一种加载图片的方式*/mImageLoader.displayImage("https://s0.52poke.wiki/wiki/thumb/2/21/001Bulbasaur.png/300px-001Bulbasaur.png",mImage);break;case R.id.btn2:/*** 第二种加载图片的方式*/mImageLoader.loadImage("https://s0.52poke.wiki/wiki/thumb/7/73/002Ivysaur.png/300px-002Ivysaur.png",new SimpleImageLoadingListener(){@Overridepublic void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {mImage.setImageBitmap(loadedImage);}});break;case R.id.btn3:/*** 第三种加载图片的方式*/mImageLoader.displayImage("https://s0.52poke.wiki/wiki/thumb/a/ae/003Venusaur.png/300px-003Venusaur.png", mImage,mOptions, new ImageLoadingListener() {@Overridepublic void onLoadingStarted(String s, View view) {//开始加载Toast.makeText(UnlversalImageLoaderDemo.this,"开始加载妙蛙花",Toast.LENGTH_SHORT).show();}@Overridepublic void onLoadingFailed(String s, View view, FailReason failReason) {//加载失败Toast.makeText(UnlversalImageLoaderDemo.this,"妙蛙花加载失败",Toast.LENGTH_SHORT).show();}@Overridepublic void onLoadingComplete(String s, View view, Bitmap bitmap) {//加载成功Toast.makeText(UnlversalImageLoaderDemo.this,"妙蛙花加载成功",Toast.LENGTH_SHORT).show();}@Overridepublic void onLoadingCancelled(String s, View view) {//取消加载Toast.makeText(UnlversalImageLoaderDemo.this,"妙蛙花取消加载",Toast.LENGTH_SHORT).show();}}, new ImageLoadingProgressListener() {@Overridepublic void onProgressUpdate(String s, View view, int i, int i1) {//下载进度/*** String s = 下载网站url* View view = 你导入的view 可以是null* int i = 当前下载字节* int i1 = 总字节*/}});break;case R.id.btn4:/*** 第四种加载图片的方式*/new Thread(new Runnable() {@Overridepublic void run() {mBitmap = mImageLoader.loadImageSync("https://s0.52poke.wiki/wiki/thumb/5/53/054Psyduck.png/300px-054Psyduck.png");runOnUiThread(new Runnable() {@Overridepublic void run() {mImage.setImageBitmap(mBitmap);}});}}).start();break;case R.id.btn5:/*** 第五种处理图片的方式,处理图片大小*/ImageSize targetSize1 = new ImageSize(100, 100); // result Bitmap will be fit to this sizemImageLoader.loadImage("https://s0.52poke.wiki/wiki/thumb/c/c6/094Gengar.png/300px-094Gengar.png", targetSize1, mOptions, new SimpleImageLoadingListener() {@Overridepublic void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {mImage.setImageBitmap(loadedImage);}});break;case R.id.btn6:new Thread(new Runnable() {@Overridepublic void run() {ImageSize targetSize2 = new ImageSize(80, 50); // result Bitmap will be fit to this sizemBitmap = mImageLoader.loadImageSync("https://s0.52poke.wiki/wiki/thumb/e/e2/133Eevee.png/300px-133Eevee.png", targetSize2, mOptions);runOnUiThread(new Runnable() {@Overridepublic void run() {mImage.setImageBitmap(mBitmap);}});}}).start();break;default:break;}}
}