访问网络从网络中获取图片
2015-06-29 21:21
375 查看
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.2UI布局代码:
<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这个类来实现。
代码修改为:
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。
学习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.2UI布局代码:
<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。
相关文章推荐
- 简单的网络聊天室
- TCP三次握手,四次挥手
- 黑马程序员——Java基础——网络编程
- 抓包工具Wireshark---实例分析TCP窗口及reset
- Wireshark抓包:实例诊断TCP连接问题
- TCP/IP数据包结构详解
- Java基础---网络编程
- 黑马程序员---计算机网络分层结构及原则
- 深度学习“引擎”之争:GPU加速还是专属神经网络芯片?
- JAVA基础知识:网络
- CentOS7 桥接网络配置
- HTTP 指纹识别v0.1
- Android 网络编程之 XML 和 JSON 解析
- 指令中directive 如何获取父级中通过$http获取的数据
- Android http 相关
- Requests: HTTP for Humans
- iOS常用网络库收集
- 【网络编程笔记】Linux系统常见的网络编程I/O模型简述
- Android实时监听网络状态
- ios开发网络知识 TCP,IP,HTTP,SOCKET区别和联系