Android基礎篇之在ListView中顯示網狀圖片

來源:互聯網
上載者:User

ListView中顯示網狀圖片

2011-05-20   10:48:22

      最近在做一個天氣預報的例子,想在ListView中添加網狀圖片,在翻閱很多文檔,在Baidu上Google很久,終於找到了辦法,現在跟大家分享一下解決方案,此篇僅做一個比較簡單的執行個體;

大家知道ImageView 類雖然有一個 setImageUri 方法,但不能直接接受一個由網路地址產生的uri作為參數從而顯示圖片,我們只好使用其 setImageBitmap 方法,如

 Bitmap mBitmap = null;</p><p>URL url = new URL(imageUrl);</p><p>HttpURLConnection conn = (HttpURLConnection) url.openConnection();</p><p>InputStream is = conn.getInputStream();</p><p>mBitmap = BitmapFactory.decodeStream(is);

     在SimpleAdapter中需要一個資料來源,用來儲存資料的,在顯示圖片時我們要用HashMap<>儲存一個Bitmap;但僅存取了Bitmap時在ListView中是無法顯示圖片的,我們需要對SimpleAdapter進行處理 。

如下是對SimpleAdaptr處理的主要代碼:

adapter.setViewBinder(new ViewBinder() {</p><p>public boolean setViewValue(View view, Object data,<br />String textRepresentation) {<br />//判斷是否為我們要處理的對象<br />if(view instanceof ImageView && data instanceof Bitmap){<br />ImageView iv = (ImageView) view;</p><p>iv.setImageBitmap((Bitmap) data);<br />return true;<br />}else<br />return false;<br />}<br />});

看API對android.widget.SimpleAdapter.ViewBinder的解釋:

This class can be used by external clients of SimpleAdapter to bind
values to views.
You should use this class to bind values to views that are not
directly supported by SimpleAdapter or to change the way binding
occurs for views supported by SimpleAdapter.


你可以使用該類來處理SimpleAdapter不直接支援的資料;


下面來看看完整的代碼:

main.xml 中就是一個ListView  , list_item.xml 是為ListView中控制項提供布局 ,就是一個ImageView。這兩個比較簡單,不貼出代碼


主要代碼   MainActivity.java

package com.yin.listView;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.net.HttpURLConnection;<br />import java.net.MalformedURLException;<br />import java.net.URL;<br />import java.util.ArrayList;<br />import java.util.HashMap;<br />import java.util.List;<br />import android.app.Activity;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapFactory;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.view.View;<br />import android.widget.ImageView;<br />import android.widget.ListView;<br />import android.widget.SimpleAdapter;<br />import android.widget.SimpleAdapter.ViewBinder;<br />public class MainActivity extends Activity {<br />ListView mListView;<br />View mView;<br />public static final String imageUrl = "http://www.google.com//ig/images/weather/mostly_sunny.gif";<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> mView = LayoutInflater.from(this).inflate(R.layout.list_item,null);<br /> mListView = (ListView) findViewById(R.id.mList);<br /> List<HashMap<String,Object>> mListData = getListData();</p><p> SimpleAdapter adapter = new SimpleAdapter(this, mListData, R.layout.list_item,<br /> new String[]{"icon"}, new int[]{R.id.image});</p><p> adapter.setViewBinder(new ViewBinder() {</p><p>public boolean setViewValue(View view, Object data,<br />String textRepresentation) {<br />//判斷是否為我們要處理的對象<br />if(view instanceof ImageView && data instanceof Bitmap){<br />ImageView iv = (ImageView) view;</p><p>iv.setImageBitmap((Bitmap) data);<br />return true;<br />}else<br />return false;<br />}<br />});</p><p> mListView.setAdapter(adapter);<br /> }<br /> public List<HashMap<String,Object>> getListData(){<br /> List<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>();<br /> HashMap<String,Object> map = null;<br /> for(int i=0;i<5;i++){<br /> map = new HashMap<String, Object>();<br /> map.put("icon",getBitmap());<br /> list.add(map);<br /> }<br /> return list;<br /> }</p><p> public Bitmap getBitmap(){<br /> Bitmap mBitmap = null;<br /> try {<br />URL url = new URL(imageUrl);<br />HttpURLConnection conn = (HttpURLConnection) url.openConnection();<br />InputStream is = conn.getInputStream();<br />mBitmap = BitmapFactory.decodeStream(is);</p><p>} catch (MalformedURLException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}</p><p> return mBitmap;<br /> }<br />}


注意:此例是顯示網狀圖片 要記得添加許可權 

<uses-permission android:name="android.permission.INTERNET">




相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.