ImageLoad(二)
来源:互联网 发布:苹果mac商店 编辑:程序博客网 时间:2024/05/17 22:51
效果图:
第一个按钮是上篇文章写的就在Activity中下载图片并展示,第二个则使用一个Dialog展示下载的一组图片
MainActivity代码:
public class MainActivity extends Activity {
private List<ImageBean> list = new ArrayList<ImageBean>();
private MyGridViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private ProgressDialog dialog;
//按钮事件
@SuppressLint("InflateParams")
public void click(View v){
switch (v.getId()) {
case R.id.main_dialog2:
LayoutInflater factory = LayoutInflater
.from(MainActivity.this);
// 得到自定义对话框
final View DialogView = factory.inflate(
R.layout.grid_view, null);
// 创建对话框
final AlertDialog dlg = new AlertDialog.Builder(
MainActivity.this)
.setView(DialogView).create();
dlg.show();
for (int i = 0; i < 5; i++) {
ImageBean bean = new ImageBean();
list.add(bean);
}
adapter = new MyGridViewAdapter(MainActivity.this, list);
GridView gridview = (GridView) DialogView.findViewById(R.id.gridView);
Button btn = (Button) DialogView.findViewById(R.id.gridView_btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dlg.dismiss();
}
});
gridview.setAdapter(adapter);
adapter.notifyDataSetChanged();
String[] str = {"https://www.baidu.com/img/baidu_logo.gif",
"https://www.baidu.com/img/baidu_logo.gif",
"https://www.baidu.com/img/baidu_logo.gif",
"https://www.baidu.com/img/baidu_logo.gif",
"https://www.baidu.com/img/baidu_logo.gif",
};
// 启动异步线程加载网络图片
new ImageLoadTask(adapter,str).execute();
break;
}
}
}
activity_main.xml:
<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"
tools:context="com.example.dialogimageload.MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/main_dialog1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="loading_dialog下载单张图片"
android:onClick="click"
/>
<Button
android:id="@+id/main_dialog2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="loading_dialog下载多张图片"
android:onClick="click"
/>
<ImageView
android:id="@+id/main_iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
grid_view.xml:
<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"
tools:context="com.example.dialogimageload.MainActivity"
android:orientation="vertical"
>
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_weight="1"
android:numColumns="4"
android:layout_height="0dp"
android:text="@string/hello_world"
/>
<Button
android:id="@+id/gridView_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:onClick="click"
/>
</LinearLayout>
ImageBean:
public class ImageBean{
private Bitmap bitmap;
private boolean isNeedBar = true;
public ImageBean() {
super();
}
public boolean isNeedBar() {
return isNeedBar;
}
public void setNeedBar(boolean isNeedBar) {
this.isNeedBar = isNeedBar;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}
}
MyGridViewAdapter:
public class MyGridViewAdapter extends BaseAdapter{
private Context context;
private List<ImageBean> list = new ArrayList<ImageBean>();
public MyGridViewAdapter(Context context, List<ImageBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout = new LinearLayout(context);
ImageView imageView = new ImageView(context);
layout.addView(imageView);
ImageBean bean = list.get(position);
if (bean.getBitmap() != null) {
imageView.setImageBitmap(bean.getBitmap());
}
if (bean.isNeedBar()) {
ProgressBar bar = new ProgressBar(context);
layout.addView(bar);
}
return layout;
}
}
ImageLoadTask:
public class ImageLoadTask extends AsyncTask<Void, Void, Void> {
private MyGridViewAdapter adapter;
private String[] list;
public ImageLoadTask(MyGridViewAdapter adapter,String[] list) {
this.adapter = adapter;
this.list = list;
}
@Override
protected Void doInBackground(Void... params) {
for (int i = 0; i < adapter.getCount(); i++) {
ImageBean bean = (ImageBean) adapter.getItem(i);
Bitmap bitmap = BitmapFactory
.decodeStream(getStream(list[i]));
bean.setBitmap(bitmap);
bean.setNeedBar(false);
publishProgress(); // 通知去更新UI
}
return null;
}
public void onProgressUpdate(Void... voids) {
if (isCancelled()) {
return;
}
// 更新UI
adapter.notifyDataSetChanged();
}
public InputStream getStream(String urlStr) {
InputStream inStream = null;
try {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
conn.setConnectTimeout(10 * 1000);
inStream = conn.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return inStream;
}
}
- ImageLoad(二)
- ImageLoad
- ImageLoad
- imageload
- imageload 知识
- ImageLoad(一)
- ImageLoad总结
- 微软的ImageLoad
- ImageLoad图片加载器
- imageload如何更改文件名
- ImageLoad中的配置属性
- 使用imageload加载图片
- 开源imageload的使用
- imageload的简单实用
- kaoshi(imageload接口)
- imageload缓存图片
- imageload默认配置
- ImageLoad自定义设置
- Java Thread的interrupt详解
- 重写ListView的Adapter时,getView()方法没有执行的原因
- oracl数据库的常用知识
- UITableView
- js基础1
- ImageLoad(二)
- mysql修改默认的环境的字符集为utf-8
- 用javascript语言循环输出表格
- css学习笔记(1)
- InnoSetup快速入门(三):添加安装文件并创建开始菜单快捷方式
- C++ const
- C语言函数理解
- ubuntu install scrapy
- AVA编写的使用Socket模拟Http的GET操作