直接看代码:MainActivity:
package com.example.textwsjdemo;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private String url;
private Button bt_hehe;
private ImageView iv_photo;
private DisplayImageOptions defaultOptions;
private ImageLoaderConfiguration config;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
defaultOptions = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher) //载入開始默认的图片
.showImageForEmptyUri(R.drawable.ic_launcher) //url爲空會显示该图片。自己放在drawable里面的
.showImageOnFail(R.drawable.ic_launcher) //载入图片出现故障,会显示该图片
.cacheInMemory(true) //缓存用 fslae 不缓存
.cacheOnDisc(true) //缓存用
.displayer(new RoundedBitmapDisplayer(5)) //图片圆角显示,值为整数
.build();
config = new ImageLoaderConfiguration.Builder(MainActivity.this)
.defaultDisplayImageOptions(defaultOptions)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
// .enableLogging() // Not necessary in common
.build();
ImageLoader.getInstance().init(config);
}
private void initView() {
//图片URL
url = "http://file.koolearn.com/20131018/1382077932449.jpg";
bt_hehe = (Button) findViewById(R.id.bt_hehe);
iv_photo = (ImageView) findViewById(R.id.iv_photo);
bt_hehe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageLoader.getInstance().displayImage(url,iv_photo, defaultOptions); //url为图片地址,iv_photo 为ImageView布局,
}
});
}
}
就这点代码就能够实现载入网络图片。导入Image-Loader 的jar包就可以
Image-Loader下载: https://github.com/nostra13/Android-Universal-Image-Loader