(十二)handler消息處理機制-創(chuàng)新互聯(lián)

(一)android:layout_weight 在不同情況下的意義。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),袁州企業(yè)網(wǎng)站建設(shè),袁州品牌網(wǎng)站建設(shè),網(wǎng)站定制,袁州網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,袁州網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

   當(dāng) android:layout_width  和 android:layout_height都不為0的時候,android:layout_weight代表的是控件渲染的優(yōu)先級,值越大,渲染的優(yōu)先級越低。默認(rèn)android:layout_weight=0。

   當(dāng) android:layout_width  或 android:layout_height為0的時候,android:layout_weight才代表權(quán)重,值越大,權(quán)重越大。

(二)網(wǎng)絡(luò)圖片查看器的功能需求:根據(jù)給定的URL地址,去訪問網(wǎng)絡(luò)獲取圖片, 將獲取的圖片顯示在界面中。程序界面運行如下:

(三)網(wǎng)絡(luò)圖片查看器的activity_main.xml文件

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.networkviewimage.MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3" /> <EditText android:id="@+id/et_path" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="/upload/otherpic29/95eef01f3a292df535a659dabe315c6035a8739c.jpg" android:hint="請輸入網(wǎng)絡(luò)圖片的地址" /> <Button android:text="瀏覽" android:id="@+id/bt_setImageView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="click" /> </LinearLayout>
(三)
MainActivity.java源碼:
package com.example.networkviewimage;

import java.io.InputStream;
import java.net.HttpURLConnection;

import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
protected static final int CHANGE_UI = 1;
protected static final int ERROR = 2;
public ImageView iv;
public EditText et_path;

// 創(chuàng)建消息處理器  private Handler handler = new Handler() {

        @Override
public void handleMessage(Message msg) {
            
switch (msg.what) {
case CHANGE_UI:
                iv.setImageBitmap((Bitmap) msg.obj);
break;
case ERROR:
                Toast.makeText(MainActivity.this, "獲取圖片失敗", 0).show();
break;

            }
        }

    };

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv= (ImageView) this.findViewById(R.id.iv);
        et_path= (EditText) this.findViewById(R.id.et_path);
    }

public void click(View v) {
final String path = et_path.getText().toString().trim();
if (TextUtils.isEmpty(path)) {
            Toast.makeText(this, "網(wǎng)絡(luò)圖片的路徑不能為空", 0).show();
        }else {
// 連接服務(wù)器,獲得圖片。由于訪問網(wǎng)絡(luò)是個耗時的錯誤,所以必須在子線程中訪問網(wǎng)絡(luò)(在androi4.0以上的特性) new Thread(new Runnable() {

                @Override
public void run() {
// TODO Auto-generated method stub   try {

                        URL url= new URL(path);
// 根據(jù)url發(fā)送http請求                        HttpURLConnection conn = (HttpURLConnection) url
                                .openConnection();

                        conn.setConnectTimeout(5000); // 設(shè)置連接的超時時間                        conn.setReadTimeout(5000); // 設(shè)置讀數(shù)據(jù)的超時時間                        conn.setRequestMethod("GET"); // 設(shè)置請求方式 int code = conn.getResponseCode(); // 得到服務(wù)器返回的響應(yīng)碼,200代表0K,404代表資源沒有找到,503代表服務(wù)器內(nèi)部錯誤 if (code == 200) {
                            InputStream is= conn.getInputStream(); // 從服務(wù)器獲得的數(shù)據(jù)流,在此為從服務(wù)器獲得圖片                            Bitmap bitmap = BitmapFactory.decodeStream(is); // 把流里面內(nèi)容轉(zhuǎn)化為Bitmap
// iv.setImageBitmap(bitmap);
// 告訴主線程,幫我更改界面,內(nèi)容是bitmap                            Message msg = new Message();
                            msg.what= CHANGE_UI;   
                            msg.obj= bitmap;

                            handler.sendMessage(msg);

                        }else {

                            Message msg= new Message();
                            msg.what= ERROR;

                            handler.sendMessage(msg);
                        }

                    }catch (Exception e) {
// TODO Auto-generated catch block                        e.printStackTrace();
                        Message msg= new Message();
                        msg.what= ERROR;
                        handler.sendMessage(msg);
// Toast.makeText(MainActivity.this, "獲取圖片失敗",
// 0).show();  土司也是UI,如果執(zhí)行catch直接彈出Toast會出錯                    }
                }
            }).start();
        }

    }
}

(五)由于需要訪問網(wǎng)絡(luò),所以需要在AndroidManifest.xml加入權(quán)限: <uses-permission android:name="android.permission.INTERNET" />

(六)需要注意的地方:

  1、訪問網(wǎng)絡(luò)是個耗時間的操作,必須在子線程執(zhí)行。

  2、訪問網(wǎng)絡(luò)需要權(quán)限。

  3、UI操作只能在主線程執(zhí)行,如果子線程要修改UI,則通過handler消息處理機制,首先在主線程創(chuàng)建一個消息處理器handler對象,然后子線程通過消息處理器handler發(fā)送一個消息給主線程,消息按時間順序從小到大將被放在主線程的消息隊列里面,主線程里面有一個looper消息的輪詢器,如果輪詢器發(fā)現(xiàn)了消息,則調(diào)用handlerMessage方法處理消息。大概原理機制如下圖所示:

,

網(wǎng)頁名稱:(十二)handler消息處理機制-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://muchs.cn/article0/dcjjoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、動態(tài)網(wǎng)站、做網(wǎng)站、網(wǎng)站維護、電子商務(wù)、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)