In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is a detailed introduction to "Android multi-threaded instance analysis" for everyone. The content is detailed, the steps are clear, and the details are properly handled. I hope this article "Android multi-threaded instance analysis" can help you solve your doubts. The following follows the ideas of Xiaobian and goes deeper slowly to learn new knowledge together.
In android programs, there will be some time-consuming operations, such as grabbing pictures from the Internet, downloading files, updating databases in batches, etc. These operations will take a long time for mobile phones, and the application interface cannot wait until these operations are completed. Of course, there are other solutions, such as Service.
Let's start with an example, something like this: there's a list, one image per line, and the images are stored online. If you don't need multithreading, it's OK, but you can't show it until all the images have been downloaded. This approach is very unfriendly to the user experience, so we adopt a multithreaded approach, starting a thread for each image, and when it downloads the data, it is displayed in the main thread.
main 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 = {"https://cache.yisu.com/upload/information/20210522/379/538936.jpg","https://cache.yisu.com/upload/information/20210522/379/538938.jpg"}; imageListAdapter = new ImageListAdapter(getApplicationContext(), images); setListAdapter(imageListAdapter); } }
adapter
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"/>读到这里,这篇"Android多线程实例分析"文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注行业资讯频道。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.