(一)android:layout_weight 在不同情況下的意義。
當(dāng) android:layout_width 和 android:layout_height都不為0的時(shí)候,android:layout_weight代表的是控件渲染的優(yōu)先級(jí),值越大,渲染的優(yōu)先級(jí)越低。默認(rèn)android:layout_weight=0。
當(dāng) android:layout_width 或 android:layout_height為0的時(shí)候,android:layout_weight才代表權(quán)重,值越大,權(quán)重越大。
(二)網(wǎng)絡(luò)圖片查看器的功能需求:根據(jù)給定的URL地址,去訪問(wèn)網(wǎng)絡(luò)獲取圖片, 將獲取的圖片顯示在界面中。程序界面運(yùn)行如下:
(三)網(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="請(qǐng)輸入網(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èn)網(wǎng)絡(luò)是個(gè)耗時(shí)的錯(cuò)誤,所以必須在子線程中訪問(wèn)網(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請(qǐng)求 HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setConnectTimeout(5000); // 設(shè)置連接的超時(shí)時(shí)間 conn.setReadTimeout(5000); // 設(shè)置讀數(shù)據(jù)的超時(shí)時(shí)間 conn.setRequestMethod("GET"); // 設(shè)置請(qǐng)求方式 int code = conn.getResponseCode(); // 得到服務(wù)器返回的響應(yīng)碼,200代表0K,404代表資源沒(méi)有找到,503代表服務(wù)器內(nèi)部錯(cuò)誤 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會(huì)出錯(cuò) }
}
}).start();
}
}
}
(五)由于需要訪問(wèn)網(wǎng)絡(luò),所以需要在AndroidManifest.xml加入權(quán)限: <uses-permission android:name="android.permission.INTERNET" />
(六)需要注意的地方:
1、訪問(wèn)網(wǎng)絡(luò)是個(gè)耗時(shí)間的操作,必須在子線程執(zhí)行。
2、訪問(wèn)網(wǎng)絡(luò)需要權(quán)限。
3、UI操作只能在主線程執(zhí)行,如果子線程要修改UI,則通過(guò)handler消息處理機(jī)制,首先在主線程創(chuàng)建一個(gè)消息處理器handler對(duì)象,然后子線程通過(guò)消息處理器handler發(fā)送一個(gè)消息給主線程,消息按時(shí)間順序從小到大將被放在主線程的消息隊(duì)列里面,主線程里面有一個(gè)looper消息的輪詢器,如果輪詢器發(fā)現(xiàn)了消息,則調(diào)用handlerMessage方法處理消息。大概原理機(jī)制如下圖所示:
,
網(wǎng)頁(yè)名稱:(十二)handler消息處理機(jī)制-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article0/dcjjoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、動(dòng)態(tài)網(wǎng)站、做網(wǎng)站、網(wǎng)站維護(hù)、電子商務(wù)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容