访问网络从网络中获取图片

Android消息处理机制:

学习Android的消息处理机制,有几个概念(类)必须了解:

1. Message

消息,理解为线程间通讯的数据单元。例如后台线程在处理数据完毕后需要更新UI,则可发送一条包含更新信息的Message给UI线程。

2. Message Queue

消息队列,用来存放通过Handler发布的消息,按照先进先出执行。

3. Handler

Handler是Message的主要处理者,负责将Message添加到消息队列以及对消息队列中的Message进行处理。

4. Looper

循环器,扮演Message Queue和Handler之间桥梁的角色,循环取出Message Queue里面的Message,并交付给相应的Handler进行处理。



5. 线程

UI thread 通常就是main thread,而Android启动程序时会替它建立一个Message Queue。

每一个线程里可含有一个Looper对象以及一个MessageQueue数据结构。在你的应用程序里,可以定义Handler的子类别来接收Looper所送出的消息。




1.1任务描述:

输入网络图片的地址,点击浏览按钮可以显示网络中的图片。

1.2.1 UI设计

1.2 任务实现:

1.2.2 UI布局代码:
<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=".MainActivity" >

<ImageView
android:id="@+id/ivImage"
android:layout_width="288dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.82"
android:src="@drawable/ic_launcher" />

<EditText
android:id="@+id/etImageUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="http://pic.nipic.com/2007-11-08/2007118192311804_2.jpg"
android:hint="请输入图片的地址" />

<Button
android:id="@+id/btnView"
android:layout_width="100dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="@drawable/button_bg"
android:onClick="showImage"
android:text="浏览" />

</LinearLayout>

1.2.3 Java代码截图:


在Android4.0以上模拟器上运行是不能正常运行的,原因如下:
ANR(Application Not Responding):应用程序无响应,如果应用程序不能响应用户输入的话,系统会显示ANR。主线程也是UI线程本身就干了很多事情,绘制界面响应事件等。如果里面再直接放入一些耗时的操作,如连接网络进行IO操作,就会阻塞主线程,带来较差的用户体验。


1.2.4 修改程序:


再次运行程序,这时程序运行不正常,因为在java中是不允许跨线程修改UI元素的,如我们在新启动的线程中想去修改UI主线程中TextView的文本时,会报错误的。如果想做这样的操作,我们就得借助Handler这个类来实现。

1.2.5 Android消息处理机制


代码修改为:
private android.os.Handler handler=new android.os.Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case SHOWIMAGE:
Bitmap bitmap=(Bitmap) msg.obj;
ivImage.setImageBitmap(bitmap);
break;

default:
break;
}
};
};


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews() {
btnView=(Button) findViewById(R.id.btnView);
etImageUrl=(EditText) findViewById(R.id.etImageUrl);
ivImage= (ImageView) findViewById(R.id.ivImage);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void showImage(View view){
final String path=etImageUrl.getText().toString();
if(TextUtils .isEmpty(path)){
Toast.makeText(this, "图片路径不为空", Toast.LENGTH_LONG).show();
}else{
new Thread(){
public void run(){
try {
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
int responseCode=connection.getResponseCode();
if(responseCode==200){
InputStream inputStream=connection.getInputStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
//ivImage.setImageBitmap(bitmap);
Message message=new Message();
message.what=SHOWIMAGE;
message.obj=bitmap;
handler.sendMessage(message);

}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

};
}.start();
}
}
}

再次运行程序,运行成功。
1.3效果图:





封装任务 Message:

在整个消息处理机制中,message又叫task,封装了任务携带的信息和处理该任务的handler.message的用法比较简单,这里不做总结了。但是有这么几点需要注意(待补充):

1.尽管Message有public的默认构造方法,但是你应该通过Message.obtain()来从消息池中获得空消息对象,以节省资源。

2.如果你的message只需要携带简单的int信息,请优先使用Message.arg1和Message.arg2来传递信息,这比用Bundle更省内存

3.擅用message.what来标识信息,以便用不同方式处理message。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值