1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時間:8:30-17:00
      你可能遇到了下面的問題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
      android安卓異步加載網(wǎng)絡(luò)圖片,與viewpager結(jié)合使用示例
      【1】異步加載圖片類AsyncImageLoader
      package com.example.testdddleapk.cus;
      
      import java.io.IOException;
      import java.lang.ref.SoftReference;
      import java.util.HashMap;
      
      import org.apache.http.HttpEntity;
      import org.apache.http.HttpResponse;
      import org.apache.http.HttpStatus;
      import org.apache.http.client.ClientProtocolException;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.params.CoreConnectionPNames;
      
      import android.graphics.drawable.Drawable;
      import android.os.Handler;
      import android.os.Message;
      
      /**
       * 異步加載圖片
       */
      public class AsyncImageLoader {
      
      	// 軟引用,使用內(nèi)存做臨時緩存 (程序退出,或內(nèi)存不夠則清除軟引用)
      	private HashMap> p_w_picpathCache;
      
      	public AsyncImageLoader() {
      		p_w_picpathCache = new HashMap>();
      	}
      
      	/**
      	 * 定義回調(diào)接口
      	 */
      	public interface ImageCallback {
      		public void p_w_picpathLoaded(Drawable p_w_picpathDrawable, String p_w_picpathUrl);
      	}
      
      	/**
      	 * 創(chuàng)建子線程加載圖片
      	 * 子線程加載完圖片交給handler處理(子線程不能更新ui,而handler處在主線程,可以更新ui)
      	 * handler又交給p_w_picpathCallback,p_w_picpathCallback須要自己來實(shí)現(xiàn),在這里可以對回調(diào)參數(shù)進(jìn)行處理
      	 * @param p_w_picpathUrl :須要加載的圖片url
      	 * @param p_w_picpathCallback:
      	 * @return
      	 */
      	public Drawable loadDrawable(final String p_w_picpathUrl,final ImageCallback p_w_picpathCallback) {
      		//如果緩存中存在圖片  ,則首先使用緩存
      		if (p_w_picpathCache.containsKey(p_w_picpathUrl)) {
      			SoftReference softReference = p_w_picpathCache.get(p_w_picpathUrl);
      			Drawable drawable = softReference.get();
      			if (drawable != null) {
      				System.out.println("loadDrawable");
      				p_w_picpathCallback.p_w_picpathLoaded(drawable, p_w_picpathUrl);//執(zhí)行回調(diào)
      				return drawable;
      			}
      		}
      
      		/**
      		 * 在主線程里執(zhí)行回調(diào),更新視圖
      		 */
      		final Handler handler = new Handler() {
      			public void handleMessage(Message message) {
      				System.out.println("handleMessage");
      				p_w_picpathCallback.p_w_picpathLoaded((Drawable) message.obj, p_w_picpathUrl);
      			}
      		};
      
      		
      		/**
      		 * 創(chuàng)建子線程訪問網(wǎng)絡(luò)并加載圖片 ,把結(jié)果交給handler處理
      		 */
      		new Thread() {
      			@Override
      			public void run() {
      				Drawable drawable = loadImageFromUrl(p_w_picpathUrl);
      				// 下載完的圖片放到緩存里
      				p_w_picpathCache.put(p_w_picpathUrl, new SoftReference(drawable));
      				Message message = handler.obtainMessage(0, drawable);
      				handler.sendMessage(message);
      			}
      		}.start();
      		return null;
      	}
      	
      	/**
      	 * 下載圖片  (注意HttpClient 和httpUrlConnection的區(qū)別)
      	 */
      	public Drawable loadImageFromUrl(String url) {
      
      		try {
      			HttpClient client = new DefaultHttpClient();
      			client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000*15);
      			HttpGet get = new HttpGet(url);
      			HttpResponse response;
      			response = client.execute(get);
      			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
      				HttpEntity entity = response.getEntity();
      				Drawable d = Drawable.createFromStream(entity.getContent(),"src");
      				return d;
      			} else {
      				return null;
      			}
      		} catch (ClientProtocolException e) {
      			e.printStackTrace();
      		} catch (IOException e) {
      			e.printStackTrace();
      		}
      
      		return null;
      	}
      
      	//清除緩存
      	public void clearCache() {
      		if (this.p_w_picpathCache.size() > 0) {
      			this.p_w_picpathCache.clear();
      		}
      	}
      
      }

      【2】 pagerAdapter的instantiateItem方法

      創(chuàng)新互聯(lián)自2013年創(chuàng)立以來,先為東區(qū)等服務(wù)建站,東區(qū)等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為東區(qū)企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

      @SuppressLint("NewApi") @Override
      public Object instantiateItem(final ViewGroup container, final int position) {
              String url=imgsUrls[position];
      	Drawable cachedImage = asyncImageLoader.loadDrawable(url, new ImageCallback() {
      	    @SuppressLint("NewApi") public void p_w_picpathLoaded(Drawable p_w_picpathDrawable,String p_w_picpathUrl) {
      		ImageView img=(ImageView) viewList.get(position).findViewById(R.id.img);
      		img.setBackground(p_w_picpathDrawable);
      		container.addView(view);
      	    }
      	});
      	return viewList.get(position);
      }

      名稱欄目:android安卓異步加載網(wǎng)絡(luò)圖片,與viewpager結(jié)合使用示例
      本文路徑:http://www.ef60e0e.cn/article/pcehjh.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        古田县| 双鸭山市| 永吉县| 胶州市| 平湖市| 天门市| 柞水县| 顺义区| 台中县| 安西县| 寿宁县| 宜君县| 读书| 富宁县| 成安县| 西昌市| 城市| 万源市| 阿荣旗| 普定县| 洪洞县| 独山县| 临漳县| 沙田区| 靖宇县| 尚志市| 静乐县| 晴隆县| 沅陵县| 大城县| 无锡市| 靖江市| 张家界市| 广汉市| 凌海市| 崇文区| 贵定县| 吕梁市| 廉江市| 定边县| 湖南省|