新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > Android異步加載網絡圖片

        Android異步加載網絡圖片

        作者: 時間:2016-09-12 來源:網絡 收藏

        實現思路是:

        本文引用地址:http://www.104case.com/article/201609/305034.htm

        1:在UI線程中啟動一個線程,讓這個線程去下載圖片。

        2:圖片完成下載后發送一個消息去通知UI線程

        2:UI線程獲取到消息后,更新UI。

        這里的UI線程就是主線程。

        這兩個步驟涉及到一些知識點,即是:ProgressDialog,Handler,Thread/Runnable,URL,HttpURLConnection等等一系列東東的使用。

        現在讓我們開始來實現這個功能吧!

        第一步:新建項目。

        第二步:設計好UI,如下所示

        View Code

        android:orientation=vertical

        android:layout_width=fill_parent

        android:layout_height=fill_parent

        >

        android:id=@+id/btnFirst

        android:layout_width=fill_parent

        android:layout_height=wrap_content

        android:text=異步下載方式一

        >

        android:id=@+id/btnSecond

        android:layout_width=fill_parent

        android:layout_height=wrap_content

        android:text=異步下載方式二

        >

        android:layout_width=fill_parent

        android:layout_height=match_parent

        android:id=@+id/frameLayout

        >

        android:id=@+id/image

        android:layout_width=match_parent

        android:layout_height=match_parent

        android:scaleType=centerInside

        android:padding=2dp

        >

        android:id=@+id/progress

        android:layout_width=wrap_content

        android:layout_height=wrap_content

        android:layout_gravity=center>

        第三步:獲取UI相應View組件,并添加事件監聽。

        View Codepublic class DownLoaderActivityextends Activityimplements OnClickListener{

        private static final String params=http://upload.wikimedia.org/wikipedia/commons/thumb/e/ea/Hukou_Waterfall.jpg/800px-Hukou_Waterfall.jpg;

        private Button btnFirst,btnSecond;

        private ProgressBar progress;

        private FrameLayout frameLayout;

        private Bitmap bitmap=null;

        ProgressDialog dialog=null;

        @Override

        public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        btnFirst=(Button)this.findViewById(R.id.btnFirst);

        btnSecond=(Button)this.findViewById(R.id.btnSecond);

        progress=(ProgressBar)this.findViewById(R.id.progress);

        progress.setVisibility(View.GONE);

        frameLayout=(FrameLayout)this.findViewById(R.id.frameLayout);

        btnFirst.setOnClickListener(this);

        btnSecond.setOnClickListener(this);

        }

        第四步:在監聽事件中處理我們的邏輯,即是下載服務器端圖片數據。

        這里我們需要講解一下了。

        通常的我們把一些耗時的工作用另外一個線程來操作,比如,下載上傳圖片,讀取大批量XML數據,讀取大批量sqlite數據信息。為什么呢?答案大家都明白,用戶體驗問題。

        在這里,首先我構造一個進度條對話框,用來顯示下載進度,然后開辟一個線程去下載圖片數據,下載數據完畢后,通知主UI線程去更新顯示我們的圖片。

        Handler是溝通Activity 與Thread/runnable的橋梁。而Handler是運行在主UI線程中的,它與子線程可以通過Message對象來傳遞數據。具體代碼如下:

        View Code

        private Handler handler=new Handler(){

        @Override

        public void handleMessage(Message msg){

        switch(msg.what){

        case 1:

        //關閉

        ImageView view=(ImageView)frameLayout.findViewById(R.id.image);

        view.setImageBitmap(bitmap);

        dialog.dismiss();

        break;

        }

        }

        };

        我們在這里彈出進度對話框,使用HTTP協議來獲取數據。

        View Code//前臺ui線程在顯示ProgressDialog,

        //后臺線程在下載數據,數據下載完畢,關閉進度框

        @Override

        public void onClick(View view) {

        switch(view.getId()){

        case R.id.btnFirst:

        dialog= ProgressDialog.show(this,,

        下載數據,請稍等 …,true,true);

        //啟動一個后臺線程

        handler.post(new Runnable(){

        @Override

        public void run() {

        //這里下載數據

        try{

        URL url= new URL(params);

        HttpURLConnection conn= (HttpURLConnection)url.openConnection();

        conn.setDoInput(true);

        conn.connect();

        InputStream inputStream=conn.getInputStream();

        bitmap= BitmapFactory.decodeStream(inputStream);

        Message msg=new Message();

        msg.what=1;

        handler.sendMessage(msg);

        }catch (MalformedURLException e1) {

        e1.printStackTrace();


        上一頁 1 2 下一頁

        關鍵詞:

        評論


        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 凤山市| 新密市| 青浦区| 林西县| 荆州市| 福鼎市| 冕宁县| 潜江市| 和田县| 镇雄县| 辽阳市| 潞西市| 安国市| 岳阳市| 云龙县| 鄂伦春自治旗| 南充市| 工布江达县| 涟水县| 图们市| 灌云县| 方城县| 将乐县| 江都市| 平罗县| 山东省| 柏乡县| 西乌| 沙坪坝区| 湘阴县| 崇信县| 聊城市| 静海县| 清远市| 大宁县| 鹤壁市| 修文县| 汤原县| 唐河县| 抚州市| 陆川县|