ImageView simpleAdapter 加载网络图片的处理方案
文章目录
背景:最近做一个安卓的app软件,由于android只能使用本地的照片,和数据库设计本省有点出入,让我头有点疼;通过万能的百娘,我快速锁定一个smartImageView的插件。可以直接使用url的方式显示图片。
先看效果图:
数据格式:
[{
"dateTime": "2020-04-16T05:38:35.000+0000",
"image": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1587032030220&di=bfc40a54c3c376905f4ff044a149f6aa&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F295%2F41%2F0E87XZ5MPO7J_3epECXX_600.jpg",
"name": "00000000012",
"count": 1,
"x": "12313212",
"y": "1212121",
"id": "1173865949930065920"
}, {
"dateTime": "2020-04-16T05:38:38.000+0000",
"image": "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1587032030220&di=bfc40a54c3c376905f4ff044a149f6aa&imgtype=0&src=http%3A%2F%2Fimage.tianjimedia.com%2FuploadImages%2F2015%2F295%2F41%2F0E87XZ5MPO7J_3epECXX_600.jpg",
"name": "00000000013",
"count": 1,
"x": "21313112",
"y": "3123213",
"id": "1173865949930065921"
}]
1、下载smartImageView
git:https://github.com/JackCho/SmartImageView
下载后将文件src下文件放入到自己的 工程目录中。
2、使用smartImageView标签
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.loopj.android.image.SmartImageView
android:id="@+id/shebei_item_image"
android:layout_width="80dp"
android:layout_height="80dp"
app:srcCompat="@drawable/button_bg" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="序号" />
<TextView
android:id="@+id/shebei_item_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="总数量" />
<TextView
android:id="@+id/shebei_item_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="设备名称" />
<TextView
android:id="@+id/shebei_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="入网时间" />
<TextView
android:id="@+id/shebei_item_dateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
3 修改数据绑定方式
package com.liu.znybq.adapter;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import com.liu.znybq.utils.L;
import com.loopj.android.image.SmartImage;
import com.loopj.android.image.SmartImageView;
/*
* 项目名: android_znybq
* 包名: com.liu.znybq.adapter
* 文件名: CustomImageViewBinder
* 创建者: shi860715@126.com liubj
* 创建时间: 2020/4/16 15:54
* 描述: 用来处理照片
*/
public class CustomImageViewBinder implements SimpleAdapter.ViewBinder {
@Override
public boolean setViewValue(View view, Object data, String textRepresentation) {
if( view instanceof SmartImageView){
((SmartImageView) view).setImageUrl(data.toString());
return true;
}
return false;
}
}
修改数据和视图的绑定方式;
4 将数据绑定方式注册到simpleAdapter中
simpleAdapter = new MySimpleAdapter(getActivity(),list,R.layout.shebei_item,
new String[]{"id","name","count","dateTime","image"},
new int[]{R.id.shebei_item_id,R.id.shebei_item_name,R.id.shebei_item_count,R.id.shebei_item_dateTime,R.id.shebei_item_image});
simpleAdapter.setViewBinder(new CustomImageViewBinder());
listView.setAdapter(simpleAdapter);