Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 使用開源庫加載網絡圖片

Android 使用開源庫加載網絡圖片

編輯:關於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 mData;
	private SwipeListView mListView;
	private Context mContext;
	private DisplayImageOptions mOptions; // 設置圖片顯示相關參數
	public static String[] mImageUrls; // 圖片路徑
	private ImageLoader mImageLoader;   //網絡圖片加載類

	public RecentAdapter(Context context, LinkedList 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 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 getRecentList() {
		LinkedList list = new LinkedList();
		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

 

   
    
    
    

 

本文相關文章:

1、Android 使用NineOldAndroids實現仿微信listview左滑出現刪除itembutton

2、Android 單擊listview彈出popupwindow彈出框

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved