Android 使用开源库加载网络图片

Android 使用开源库加载网络图片,使用开源库加载图片。单击listview弹出popupwindow弹出框详情查看:

Android 单击listview弹出popupwindow弹出框

,里面还附带listview右滑删除按钮详情查看: Android 使用NineOldAndroids实现仿微信listview左滑出现删除itembutton。项目就是这样慢慢变得复杂,是往里面逐渐加功能,效果,代码。

本文项目源码下载地址:点击

一、看实现的效果



二、项目结构图



三、部分源码

1、MyApplication继承了Application

package com.main.listviewsideslip;

import java.io.File;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
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.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application{

	public void onCreate() {
		super.onCreate();
		initImageLoader(getApplicationContext());
	}

	public static void initImageLoader(Context context) {
		//缓存文件的目录
		File cacheDir = StorageUtils.getOwnCacheDirectory(context, "imageloader/Cache"); 
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
				.memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽 
				.threadPoolSize(3) //线程池内加载的数量
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密
				.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
				.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
				.diskCacheSize(50 * 1024 * 1024)  // 50 Mb sd卡(本地)缓存的最大值
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				// 由原先的discCache -> diskCache
				.diskCache(new UnlimitedDiskCache(cacheDir))//自定义缓存路径  
				.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间  
				.writeDebugLogs() // Remove for release app
				.build();
		//全局初始化此配置  
		ImageLoader.getInstance().init(config);
	}
}

2、适配器RecentAdapter里面同样设置着网络加载配置。

package com.listview.adapter;

import java.util.LinkedList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.main.entity.RecentItem;
import com.main.listviewsideslip.Constants;
import com.main.listviewsideslip.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.listview.SwipeListView;

public class RecentAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private LinkedList<RecentItem> mData;
	private SwipeListView mListView;
	private Context mContext;
	private DisplayImageOptions mOptions; // 设置图片显示相关参数
	public static String[] mImageUrls; // 图片路径
	private ImageLoader mImageLoader;   //网络图片加载类

	public RecentAdapter(Context context, LinkedList<RecentItem> data,
			SwipeListView listview) {
		mContext = context;
		this.mInflater = LayoutInflater.from(mContext);
		mData = data;
		this.mListView = listview;
		mImageLoader = ImageLoader.getInstance();
		initImageView();
	}

	public void remove(int position) {
		if (position < mData.size()) {
			mData.remove(position);
			notifyDataSetChanged();
		}
	}

	public void remove(RecentItem item) {
		if (mData.contains(item)) {
			mData.remove(item);
			notifyDataSetChanged();
		}
	}

	public void addFirst(RecentItem item) {
		if (mData.contains(item)) {
			mData.remove(item);
		}
		mData.addFirst(item);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final RecentItem item = mData.get(position);
		ViewHolder vHolder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.recent_listview_item, null);
			vHolder = new ViewHolder();
			vHolder.nameTV = (TextView) convertView.findViewById(R.id.recent_list_item_name);
			vHolder.numTV = (TextView) convertView.findViewById(R.id.unreadmsg);
			vHolder.headIV = (ImageView) convertView.findViewById(R.id.icon);
			vHolder.deleteBtn = (Button) convertView.findViewById(R.id.recent_del_btn);
			
			convertView.setTag(vHolder);
		}else {
			vHolder  = (ViewHolder) convertView.getTag();
		}

		vHolder.nameTV.setText(item.getName());
		
		/**
		 * imageUrl 图片的Url地址 imageView 承载图片的ImageView控件 options
		 * DisplayImageOptions配置文件
		 */
		mImageLoader.displayImage(mImageUrls[position],
				vHolder.headIV, mOptions);
		
		int num = item.getNewNum();
		if (num > 0) {
			vHolder.numTV.setVisibility(View.VISIBLE);
			vHolder.numTV.setText(num + "");
		} else {
			vHolder.numTV.setVisibility(View.GONE);
		}
		
		vHolder.deleteBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mData.remove(position);
				notifyDataSetChanged();
				if (mListView != null)
					mListView.closeOpenedItems();
			}
		});
		return convertView;
	}
	
	class ViewHolder{
		public TextView nameTV;
		public TextView numTV;
		public ImageView headIV;
		public Button deleteBtn;
	}


3、怎么调用,其实都在上面那个适配器里面。也顺便看看MainActivity

package com.main.listviewsideslip;

import java.util.Collections;
import java.util.LinkedList;
import com.listview.adapter.RecentAdapter;
import com.main.entity.RecentItem;
import com.view.listview.BaseSwipeListViewListener;
import com.view.listview.SwipeListView;
import com.view.pop.ActionItem;
import com.view.pop.QuickAction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity{

	//listview的适配器
	private RecentAdapter mAdapter;
	//listview数据源
	public  LinkedList<RecentItem> mRecentDatas;
	//自定义的listview
	private SwipeListView mRecentListView;
	private TextView mEmpty;
	private int mClickItem = -1;
	// 点击item弹出
	protected QuickAction mItemClickQuickAction = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView(savedInstanceState);
		
		initPop();
	}

	//单击listview某一项时,可以弹出pop。QuickAction实现类是继承PopupWindows
	private void initPop() {
		// 设置Action
		mItemClickQuickAction = new QuickAction(this, QuickAction.VERTICAL);
		
		ActionItem pointItem = new ActionItem(1, "详细信息", null);
		ActionItem areaItem = new ActionItem(2, "信息2", null);
		pointItem.setIcon(null);
		areaItem.setIcon(null);
		
		mItemClickQuickAction.addActionItem(pointItem);
		mItemClickQuickAction.addActionItem(areaItem);
		
		mItemClickQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
			@Override
			public void onItemClick(QuickAction source, int pos, int actionId) {
				if (pos == 0) {
					Intent intent = new Intent(source.GetActionItemsGroup().getContext(),ListViewItemShowActivity.class);
					RecentItem item = new RecentItem();
					item = mRecentDatas.get(mClickItem);
					Bundle bundle = new Bundle();
//					bundle.putString("HeadImg", mImageUrls[mClickItem]);
					bundle.putInt("NewNum", item.getNewNum());
					bundle.putString("Name", item.getName());
					intent.putExtras(bundle);
					startActivity(intent);					
				}
				mItemClickQuickAction.dismiss();
			}
		});
	}
	
	private void initView(Bundle savedInstanceState) {
		mRecentListView = (SwipeListView) findViewById(R.id.recent_listview);
		mRecentDatas = getRecentList();
		mAdapter = new RecentAdapter(this, mRecentDatas, mRecentListView);
		mRecentListView.setAdapter(mAdapter);
		
		mEmpty = (TextView) findViewById(R.id.empty);
		mRecentListView.setEmptyView(mEmpty);
		mRecentListView
				.setSwipeListViewListener(new BaseSwipeListViewListener() {
					
					@Override
					public void onClickFrontView(View view, int position) {
						mClickItem = position;
						mItemClickQuickAction.show(view);
					}

					@Override
					public void onClickBackView(int position) {
						mRecentListView.closeOpenedItems();// 关闭打开的项
					}

					@Override
					public void onDismiss(int[] reverseSortedPositions) {
						for (int position : reverseSortedPositions) {
							mAdapter.remove(position);
						}
						
					}
				});
	}
	
	public LinkedList<RecentItem> getRecentList() {
		LinkedList<RecentItem> list = new LinkedList<RecentItem>();
		nameArray = getResources().getStringArray(R.array.jazzy_effects_ch);
		for (int i = 0; i < 20; i++) {
//			int icon = heads[i];
			String name = nameArray[i];
			int num = numArray[i];
			RecentItem item = new RecentItem(name, 0, num);
			list.add(item);
		}
		Collections.sort(list);// 降序
		return list;
	}
	
	public static  int[] numArray = { 21,67,65,8,54,67,23,78,98,54,67,23,67,423,23,67,90,12,34,56,978};
	
	public static String[] nameArray = new String[21];
}

四、最后别忘了AndroidManifest.xml配置网络权限,以及application

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    
    <application
        android:name="com.main.listviewsideslip.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

本文相关文章:

1、Android 使用NineOldAndroids实现仿微信listview左滑出现删除itembutton

2、Android 单击listview弹出popupwindow弹出框

本文项目源码下载地址:点击

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mmsx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值