您好,登錄后才能下訂單哦!
Android-Universal-Image-Loader-master 使用
下載地址:https://github.com/nostra13/Android-Universal-Image-Loader
步驟:
1)、先要配置ImageLoaderConfiguration這個類實現全局ImageLoader的實現情況,
可知Application中配置。
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
注:更多配置項到ImageLoaderConfiguration.Builder中查看。
2)、初始化全局配置
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
3)、初始化顯示圖片格式DisplayImageOptions的設置
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
4)、加載圖片到控件上
ImageLoader.getInstance().displayImage(p_w_picpathUrls[position], holder.p_w_picpath, options,null);
注:displayImage此方法有多個重載形式,根據情況選擇其一。
5)實例:
5.1)配置全局參數并初始化:
/*******************************************************************************
public class UILApplication extends Application {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressWarnings("unused")
@Override
public void onCreate() {
if (Constants.Config.DEVELOPER_MODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyDialog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build());
}
super.onCreate();
initImageLoader(getApplicationContext());
}
//初始化ImageLoader全局配置
public static void initImageLoader(Context context) {
// This configuration tuning is custom. You can tune every option, you may tune some of them,
// or you can create default configuration by
// ImageLoaderConfiguration.createDefault(this);
// method.
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}
}
5.2)、
public class MyActivity extends Activity {
private LayoutInflater inflater;
private DisplayImageOptions options;
private ListView listView;
String[] p_w_picpathUrls = Constants.IMAGES;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fr_p_w_picpath_list);
inflater=LayoutInflater.from(this);
init();
listView= (ListView) findViewById(android.R.id.list);
listView.setAdapter(new MyBaseAdapter());
}
private class MyBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return p_w_picpathUrls.length;
}
@Override
public Object getItem(int position) {
return p_w_picpathUrls[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
view = inflater.inflate(R.layout.item_list_p_w_picpath, parent, false);
holder.p_w_picpath = (ImageView) view.findViewById(R.id.p_w_picpath);
holder.text = (TextView) view.findViewById(R.id.text);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.text.setText("Item " + (position + 1));
//實例化ImageLoader并加載圖片
ImageLoader.getInstance().displayImage(p_w_picpathUrls[position], holder.p_w_picpath, options,null);
return view;
}
}
//view緩存對象
private static class ViewHolder {
TextView text;
ImageView p_w_picpath;
}
private void init() {
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。