1. 程式人生 > >ViewPager+動態載入網路圖片

ViewPager+動態載入網路圖片

前言:昨晚上師傅讓我寫了發給一個人的,因為我很閒,於是就寫了,網上我看到好多例子,涉及到好多類,看的頭疼還不如自己寫,一個類搞定,如下:(PS:防止以後忘記)xml裡面就一個ViewPager控制元件,沒啦,哈哈^_^.

package com.yalantis.contextmenu.sample;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import
android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ImageView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import
com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import com.yalantis.contextmenu.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity2Activity extends ActionBarActivity { ViewPager viewPager; ViewPagerAdapter adapter; List<Map<String, Object>> data; protected
ImageLoader imageLoader = ImageLoader.getInstance(); DisplayImageOptions options; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_activity2); init(); } public void init() { viewPager = (ViewPager) findViewById(R.id.mypager); data = getData(); imageLoader=ImageLoader.getInstance(); imageLoader.init(ImageLoaderConfiguration.createDefault(this)); options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.ic_launcher) // 在ImageView載入過程中顯示圖片 .showImageForEmptyUri(R.drawable.ic_launcher) // image連線地址為空時 .showImageOnFail(R.drawable.ic_launcher) // image載入失敗 .cacheInMemory(true) // 載入圖片時會在記憶體中載入快取 .cacheOnDisc(true) // 載入圖片時會在磁碟中載入 .build(); adapter = new ViewPagerAdapter(data); viewPager.setAdapter(adapter); } //這個方法長度是動態的,可以改成你從伺服器獲取的圖片,這樣數量就不確定啦 public List<Map<String, Object>> getData() { List<Map<String, Object>> mdata = new ArrayList<Map<String, Object>>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("url", "http://img2.duitang.com/uploads/item/201207/19/20120719132725_UkzCN.jpeg"); map.put("view", new ImageView(this)); mdata.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("url", "http://img4.duitang.com/uploads/item/201404/24/20140424195028_vtvZu.jpeg"); map1.put("view", new ImageView(this)); mdata.add(map1); Map<String, Object> map2 = new HashMap<String, Object>(); map2.put("url", "http://download.pchome.net/wallpaper/pic-5041-8-240x320.jpg"); map2.put("view", new ImageView(this)); mdata.add(map2); Map<String, Object> map3 = new HashMap<String, Object>(); map3.put("url", "http://www.mangowed.com/uploads/allimg/130425/572-130425105311304.jpg"); map3.put("view", new ImageView(this)); mdata.add(map3); return mdata; } public class ViewPagerAdapter extends PagerAdapter { List<Map<String,Object>> viewLists; public ViewPagerAdapter(List<Map<String,Object>> lists) { viewLists = lists; } @Override public int getCount() { //獲得size // TODO Auto-generated method stub return viewLists.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0 == arg1; } @Override public void destroyItem(View view, int position, Object object) //銷燬Item { ImageView x=(ImageView)viewLists.get(position).get("view"); x.setScaleType(ImageView.ScaleType.FIT_CENTER); ((ViewPager) view).removeView(x); } @Override public Object instantiateItem(View view, int position) //例項化Item { ImageView x=(ImageView)viewLists.get(position).get("view"); x.setScaleType(ImageView.ScaleType.FIT_CENTER); imageLoader.displayImage(viewLists.get(position).get("url").toString(), x,options); ((ViewPager) view).addView(x, 0); return viewLists.get(position).get("view"); } } }