Android多线程,让耗时的操作去后台运行吧
2012-11-09 13:39
281 查看
在android程序中,会有一些耗时的操作,比如从网上抓取图片,下载文件,批量更新数据库等,这些操作对于手机而言会需要很长的时间,而应用程序界面又不能等到这些操作完成后再显示,所以要让界面各这些耗时的操作并行处理,用多线程可以解决这个问题。当然还有其它解决方案,比如用Service.
我们先作一个例子吧,大概是这样的:有一个列表,每行显示的一个图片,图片是存放在网上的。如果不用多线程,也是可以的,但是要等到所有图片下载完了才能展示出来。这种方式对用户体验很不友好,所以我们采用多线程的方式,对每一个图片开启一个线程,当其下载完数据后,在主线程中显示出来。
主Activity
适配器
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageListAdapter extends BaseAdapter {
private Context context;
private String[] myImages = null;
public ImageListAdapter(Context context, String[] myImages){
this.context = context;
this.myImages = myImages;
}
@Override
public int getCount() {
if(myImages == null){
return 0;
}
return myImages.length;
}
@Override
public String getItem(int position) {
if(position < 0 || myImages == null || position>myImages.length){
return null;
}
return myImages[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = null;
if(convertView != null){
item = convertView;
} else {
item = View.inflate(context, R.layout.image_item, null);
}
final ImageView imageView = (ImageView)item.findViewById(R.id.image);
final String image = getItem(position);
if(image == null){
return item;
}
//对每个图片地址创建一个线程,
new Thread(){
public void run(){
Message msg = new Message();
msg.what = 0;
//获得图片的Bitmap对象。getBitmap省略了,就是从网上通过http下载图片然后转化成一个Bitmap
Bitmap bitmap = getBitmap(image);
List list = new ArrayList();//将bitmap和imageView包装成一个List传到线程外
list.add(bitmap);
list.add(imageView);
msg.obj = list;
handler.sendMessage(msg);
}
}.start();
return item;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0://接到从线程内传来的图片bitmap和imageView.
//这里只是将bitmap传到imageView中就行了。只所以不在线程中做是考虑到线程的安全性。
List list = (List)msg.obj;
Bitmap bitmap = (Bitmap)list.get(0);
ImageView iv = (ImageView)list.get(1);
iv.setImageBitmap(bitmap);
break;
default:
super.handleMessage(msg);
}
}
};
}
布局xml
imagelist.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding = "10px"
android:gravity="center_horizontal"
android:background="#ffffff">
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
image_item.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/image"
android:layout_width="70px"
android:layout_height="50px"
android:paddingRight="5px"/>
转载请标明出处:3G Study :http://blog.3gstdy.com/archives/27
我们先作一个例子吧,大概是这样的:有一个列表,每行显示的一个图片,图片是存放在网上的。如果不用多线程,也是可以的,但是要等到所有图片下载完了才能展示出来。这种方式对用户体验很不友好,所以我们采用多线程的方式,对每一个图片开启一个线程,当其下载完数据后,在主线程中显示出来。
主Activity
public class TestListActivity extends ListActivity { private ImageListAdapter imageListAdapter = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.imagelist); String[] images = {"http://image.baidu.com/image1.jpg","http://image.baidu.com/image2.jpg"}; imageListAdapter = new ImageListAdapter(getApplicationContext(), images); setListAdapter(imageListAdapter); } }
适配器
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageListAdapter extends BaseAdapter {
private Context context;
private String[] myImages = null;
public ImageListAdapter(Context context, String[] myImages){
this.context = context;
this.myImages = myImages;
}
@Override
public int getCount() {
if(myImages == null){
return 0;
}
return myImages.length;
}
@Override
public String getItem(int position) {
if(position < 0 || myImages == null || position>myImages.length){
return null;
}
return myImages[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = null;
if(convertView != null){
item = convertView;
} else {
item = View.inflate(context, R.layout.image_item, null);
}
final ImageView imageView = (ImageView)item.findViewById(R.id.image);
final String image = getItem(position);
if(image == null){
return item;
}
//对每个图片地址创建一个线程,
new Thread(){
public void run(){
Message msg = new Message();
msg.what = 0;
//获得图片的Bitmap对象。getBitmap省略了,就是从网上通过http下载图片然后转化成一个Bitmap
Bitmap bitmap = getBitmap(image);
List list = new ArrayList();//将bitmap和imageView包装成一个List传到线程外
list.add(bitmap);
list.add(imageView);
msg.obj = list;
handler.sendMessage(msg);
}
}.start();
return item;
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0://接到从线程内传来的图片bitmap和imageView.
//这里只是将bitmap传到imageView中就行了。只所以不在线程中做是考虑到线程的安全性。
List list = (List)msg.obj;
Bitmap bitmap = (Bitmap)list.get(0);
ImageView iv = (ImageView)list.get(1);
iv.setImageBitmap(bitmap);
break;
default:
super.handleMessage(msg);
}
}
};
}
布局xml
imagelist.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding = "10px"
android:gravity="center_horizontal"
android:background="#ffffff">
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
image_item.xml
android:layout_width="fill_parent"
android:layout_height="wrap_content">
android:id="@+id/image"
android:layout_width="70px"
android:layout_height="50px"
android:paddingRight="5px"/>
转载请标明出处:3G Study :http://blog.3gstdy.com/archives/27
相关文章推荐
- Android多线程,让耗时的操作去后台运行吧
- Android多线程,让耗时的操作去后台运行吧
- Android多线程,让耗时的操作去后台运行吧
- Android多线程,让耗时的操作去后台运行吧
- Android多线程,让耗时的操作去后台,handler传递对象图片下载
- Android中AsyncTask的处理后台耗时操作(如list排序,进度加载)
- Android中AsyncTask的处理后台耗时操作(如list排序,进度加载)
- android教程之使用asynctask在后台运行耗时任务
- 如何在web页面中,实现耗时操作的后台执行?多线程吗?
- Android拾萃 - RxJava实战(后台执行耗时操作,实时通知 UI 更新)
- Android 四大组件之一:Service 后台运行
- 多线程判断用户是否在线(后台运行ping脚本)
- Android判断App前台运行还是后台运行(运行状态)
- 用PHP实现守护进程任务后台运行与多线程(php-resque使用说明)
- WinForms中后台执行耗时的操作
- Android开发安全问题之防止页面被其他程序覆盖监听,提示用户程序转移后台运行
- Android与web服务器数据交互编程---3网络爬虫项目实现虚拟浏览器的jsp后台运行
- 怎么让 Android 程序一直后台运行
- Android 后台运行那些事
- [Android ] 多线程操作中更新UI控件的方法