任务描诉:
输入网络图片的地址,点击浏览按钮可以显示网络中的图片。
界面效果:
启动时:
任务实现
xml布局文件:
<RelativeLayout 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: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.bzu.r2.lookimage.MainActivity" >
<ImageView
android:id="@+id/ivlook"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/edset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ivlook"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="请输入图片路径。。。"
android:text="http://www.baidu.com/img/bdlogo.png" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edset"
android:layout_centerHorizontal="true"
android:onClick="showImage"
android:text="浏览" />
</RelativeLayout>
事件处理:
public class MainActivity extends Activity {
private final int SHOW_IMAGE = 1;
private ImageView iglook;
private EditText edset;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// 对接收到的消息进行处理
switch (msg.what) {
case SHOW_IMAGE:
Bitmap bitmap = (Bitmap) msg.obj;
iglook.setImageBitmap(bitmap);
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
isfind();
}
private void isfind() {
edset = (EditText) findViewById(R.id.edset);
iglook = (ImageView) findViewById(R.id.ivlook);
}
public void showImage(View view) {
final String path = edset.getText().toString();
if (TextUtils.isEmpty(path)) {
Toast.makeText(this, "图片路径不能为空!", Toast.LENGTH_LONG).show();
} else {
new Thread() {
@Override
public void run() { try {
// 链接服务器,请求获取图片
URL url = new URL(path);
try {
// 发出http请求
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// httpURLConnection.setDoInput(true);
// httpURLConnection.connect();
httpURLConnection.setRequestMethod("GET");
// 设置链接超时时间
httpURLConnection.setConnectTimeout(5000);
int responseode = httpURLConnection
.getResponseCode();
// 在主线程中创建消息处理器
if (responseode == 200) {
InputStream is = httpURLConnection
.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
// /告诉主线程,更新界面内容是bitmap
Message msg = new Message();
msg.what = SHOW_IMAGE;
msg.obj = bitmap;
handler.sendMessage(msg);
//is.close();
}else{
Toast.makeText(MainActivity.this, "显示图片失败!",Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
super.run();
};
}.start();
}
}
}
注释:在你的AndroidManifest.xml文件的< /manifest>节点上面添加< uses-permission android:name=”android.permission.INTERNET” />,这是由于Android有很多的权限限制,否则图片是不能在你的模拟器上显示的。
总结:
Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用。比如可以用handler发送一个message,然后在handler的线程中来接收、处理该消息,以避免直接在UI主线程中处理事务导致影响UI主线程的其他处理工作,Android提供了Handler作为主线程和子线程的纽带;也可以将handler对象传给其他进程,以便在其他进程中通过handler给你发送事件;还可以通过handler的延时发送message,可以延时处理一些事务的处理。
通常情况下,当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发。如果此时需要一个耗时的操作,例如:联网读取数据,或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象,如果5秒钟还没有完成的话,会收到Android系统的一个错误提示”强制关闭”.
这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,但是当子线程中有涉及到操作UI的操作时,就会对主线程产生危险,也就是说,更新UI只能在主线程中更新,在子线程中操作是危险的. 这个时候,Handler就出现了来解决这个复杂的问题,由于Handler运行在主线程中(UI线程中),它与子线程可以通过Message对象来传递数据,这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传递)Message对象,(里面包含数据), 把这些消息放入主线程队列中,配合主线程进行更新UI。