LIstView多种类+ImageLoader

来源:互联网 发布:人工智能的优点英语 编辑:程序博客网 时间:2024/05/21 06:45

在我们开发Android应用的时候,对于图片,我们总是会遇到许多的问题,比如说:异步加载加载图片、加载大量图片等等。所以为了解决这些问题,很多的开源图片加载框架应运而生,比较著名的就是Universal-Image-Loader:

1.多线程加载图片和显示图片,图片可以来自网络,SD卡,assests文件夹,drawable文件夹;

2.支持图片的内存缓存,SD卡缓存;

3.可以控制图片的加载过程,在经常使用Listview,GridView中,可以设置滑动时暂停加载图片,停止时加载图片(比较省流量);

4.在网络慢的时候,依然可以对图片进行加载;

如果想使用ImageLoader,首先我们需要将jar包导入libs文件夹中,或导入远程依赖:

compile ‘com.nostra13.universalimageloader:universal-image-loader:1.9.5’

而后就需要自己写一个类(MApp)继承Appliction;重写onCreate()方法。然后初始化ImageLoader框架,(在此过程中可以设置自己的参数),如下图:

import android.app.Application;import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;import com.nostra13.universalimageloader.core.DisplayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class MApp extends Application {    @Override    public void onCreate() {        super.onCreate();        //初始化ImageLoader框架        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this)                .memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽                .threadPoolSize(5)//线程池内加载的数量                .threadPriority(Thread.NORM_PRIORITY - 2)                .denyCacheImageMultipleSizesInMemory()                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现                .memoryCacheSize(2 * 1024 * 1024)                .discCacheSize(50 * 1024 * 1024)                .discCacheFileNameGenerator(new Md5FileNameGenerator())//将保存的时候的URI名称用MD5 加密                .tasksProcessingOrder(QueueProcessingType.LIFO)                .discCacheFileCount(100) //缓存的文件数量                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())                .writeDebugLogs() // Remove for release app.build();                .build();//开始构建        //全局初始化此配置        ImageLoader.getInstance().init(configuration);    }}

但是在我们进行联网操作和资源加载的时候需要在清单文件中加入以下的权限:

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

主界面中的代码操作:

import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import com.nostra13.universalimageloader.core.ImageLoader;import java.util.ArrayList;import java.util.List;

public class MainActivity extends AppCompatActivity {    private ListView lv;    private List<Goods> goodsList;    private String imgUrl = "http://image.tianjimedia.com/uploadImages/2012/067/N80N0GUA36N0.jpg";    private ImageLoader instance;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化组件        lv = (ListView) findViewById(R.id.lv);        //使用图片框架得到实例        instance = ImageLoader.getInstance();        initData();    }    private void initData() {        goodsList = new ArrayList<>();        goodsList.add(new Goods("商品3", "23", 0));        goodsList.add(new Goods("商品4", "24", R.drawable.hu, 1));        goodsList.add(new Goods("商品1", "21", 0));        goodsList.add(new Goods("商品2", "22", R.drawable.hu, 1));        goodsList.add(new Goods("商品5", "25", 0));        goodsList.add(new Goods("商品6", "26", R.drawable.hu, 1));        goodsList.add(new Goods("商品21", "21", 0));        goodsList.add(new Goods("商品5", "25", 0));        goodsList.add(new Goods("商品22", "22", R.drawable.hu, 1));        goodsList.add(new Goods("商品23", "23", 0));        goodsList.add(new Goods("商品24", "24", R.drawable.hu, 1));        goodsList.add(new Goods("商品25", "25", 0));        goodsList.add(new Goods("商品26", "26", R.drawable.hu, 1));        goodsList.add(new Goods("商品31", "21", 0));        goodsList.add(new Goods("商品32", "22", R.drawable.hu, 1));        goodsList.add(new Goods("商品34", "24", R.drawable.hu, 1));        goodsList.add(new Goods("商品36", "26", R.drawable.hu, 1));        goodsList.add(new Goods("商品33", "23", 0));        //listview设置适配器        lv.setAdapter(new MyAdapter());    }    class MyAdapter extends BaseAdapter {        private final int TYPE0 = 0;//只有文字条目        private final int TYPE1 = 1;//文字+图片条目        @Override        public int getCount() {            return goodsList.size();        }        @Override        public Object getItem(int i) {            return goodsList.get(i);        }        @Override        public long getItemId(int i) {            return i;        }        //返回条目种类的数量        @Override        public int getViewTypeCount() {            return 2;        }        //返回条目类型        @Override        public int getItemViewType(int position) {            return goodsList.get(position).getTypeId();        }        //优化        @Override        public View getView(int i, View view, ViewGroup viewGroup) {            int type = getItemViewType(i);            Log.e("TAG",i+"");            ViewHolder0 holder0;            ViewHolder1 holder1;            switch (type) {                case TYPE0:                    if (view == null) {                        view = View.inflate(MainActivity.this, R.layout.item0, null);                        holder0 = new ViewHolder0();                        holder0.tv1 = view.findViewById(R.id.textView1);                        holder0.tv2 = view.findViewById(R.id.textView2);                        view.setTag(holder0);                    } else {                        holder0 = (ViewHolder0) view.getTag();                    }                    holder0.tv1.setText(goodsList.get(i).getName());                    holder0.tv2.setText(goodsList.get(i).getPrice());                    break;                case TYPE1:                    if (view == null) {                        view = View.inflate(MainActivity.this, R.layout.item1, null);                        holder1 = new ViewHolder1();                        holder1.tv1 = view.findViewById(R.id.textView1);                        holder1.tv2 = view.findViewById(R.id.textView2);                        holder1.imageView = view.findViewById(R.id.imageView1);                        view.setTag(holder1);                    } else {                        holder1 = (ViewHolder1) view.getTag();                    }                    holder1.tv1.setText(goodsList.get(i).getName());                    holder1.tv2.setText(goodsList.get(i).getPrice());                    //请求网络图片,并显示在控件上                    instance.displayImage(imgUrl, holder1.imageView);                    break;            }            return view;        }    }    class ViewHolder0 {        TextView tv1, tv2;    }    class ViewHolder1 {        TextView tv1, tv2;        ImageView imageView;    }}

bean

public class Goods {    private String name;    private String price;    private int resId;    private int typeId;    public Goods(String name, String price, int typeId) {        this.name = name;        this.price = price;        this.typeId = typeId;    }    public Goods(String name, String price, int resId, int typeId) {        this.name = name;        this.price = price;        this.resId = resId;        this.typeId = typeId;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getPrice() {        return price;    }    public void setPrice(String price) {        this.price = price;    }    public int getResId() {        return resId;    }    public void setResId(int resId) {        this.resId = resId;    }    public int getTypeId() {        return typeId;    }    public void setTypeId(int typeId) {        this.typeId = typeId;    }}


主界面布局

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.bwie.listtype_imageloader.MainActivity">    <ListView        android:id="@+id/lv"        android:layout_width="match_parent"        android:layout_height="match_parent"></ListView></android.support.constraint.ConstraintLayout>

文字界面的布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:textSize="24sp"        android:text="TextView" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"         android:textSize="24sp"        android:text="TextView" /></LinearLayout>

文字+图片布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:textSize="24sp"        android:text="TextView" />    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"         android:textSize="24sp"        android:text="TextView" />    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@mipmap/ic_launcher" /></LinearLayout>

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 定频空调外机噪音大怎么办 老美的定频空调出现p0怎么办 华为揽阅m2青春版卡顿了怎么办 华为揽阅M2青春版发热卡顿怎么办 全民突击网速不给力经常掉线怎么办 华为手机微信视频黑屏了怎么办 相机拍照后找不到拍的照片怎么办 苹果手机下载软件不被信任怎么办 苹果x手机下载软件不受信任怎么办 华为畅享7plus主板坏了怎么办 华为手机手机主板坏了没备份怎么办 华为手机一年内主板坏了怎么办 华为手机保修期内主板坏了怎么办 华为手机外置sd卡满了怎么办 红米4充不进去电怎么办 苹果5s锁屏密码忘记了怎么办 华为手机桌面和锁屏自动一样怎么办 苹果手机没电了没带充电器怎么办 华为p8手机后摄像头模糊的怎么办 中兴手机充电的地方坏了怎么办? 小米手机与电脑蓝牙传输失败怎么办 捡个华为收机没有账号密码怎么办 华为手机p9激活码忘了怎么办 华为畅享7plus声音小怎么办 华为畅享7plus忘记密码怎么办 华为畅享8plus卡顿怎么办 华为畅享7plus卡机怎么办 华为畅享8plus图标字小怎么办 华为畅享6反应慢发热怎么办 华为畅享5S反应迟钝怎么办 华为畅玩5x玩王者荣耀卡怎么办 不小心把手机里的照片删了怎么办 u盘文件在手机上删了怎么办 荒野行动透视挂功能加载失败怎么办 白色t桖衫被奶茶弄脏了该怎么办 游戏文件不小心解压到c盘了怎么办 装系统时c盘0mb怎么办 电脑设置的开机密码忘了怎么办 电脑开机密码忘了怎么办xp系统 我的电脑在开机时忘了密码怎么办? xp桌面我的电脑图标不见了怎么办