ImageLoader的使用

来源:互联网 发布:人民网软件下载 编辑:程序博客网 时间:2024/05/03 08:25
添加jar包,或者添加依赖找到Gradle  open...打开将下面这行复制进去,并同步
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
//位置如下
dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })    compile 'com.android.support:appcompat-v7:26.+'    compile 'com.android.support.constraint:constraint-layout:1.0.2'    testCompile 'junit:junit:4.12'    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'    compile 'com.google.code.gson:gson:2.8.1'}


//记得加权限
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
在这里面添加android:name=".App"
.App是你新建的类
<application android:allowBackup="true"
android:name=".App"
/>

//新建一个类并继承
public class App extends Application {
//两个oncreat要一个就可以了,第一个有注释了,第二个是最简单的
    public void onCreate() {        super.onCreate();        //storage/sdcard/data/<package>/cache        //sd/mycache/        File cacheDir=this.getExternalCacheDir();//自定义缓存路径        ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//设置内存缓存区大小                .memoryCacheSize(2 * 1024 * 1024)//设置缓存区大小                .memoryCacheExtraOptions(320,320)//缓存图片最大的宽度与高度 px                .diskCacheSize(50*1024*1024)//设置sd卡缓存的空间大小                .diskCacheFileNameGenerator(new Md5FileNameGenerator())//sd卡缓存图片的命名 使用md5加密方式//                .diskCache(new UnlimitedDiscCache(cacheDir))//自定义sd卡的缓存路径                .diskCache(new UnlimitedDiskCache(cacheDir))                .diskCacheFileCount(100)//缓存文件的最大数量                .writeDebugLogs()//写入日志                .threadPoolSize(3)//线程池                .build();        //对imageLoader进行初使化        ImageLoader.getInstance().init(configuration);    }

//第二个oncreate()
    @Override    public void onCreate() {        super.onCreate();        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration                .Builder(this)                .threadPoolSize(4)                .build();//开始构建        ImageLoader.getInstance().init(configuration);    }}
//Main内容如下
String[] images = {        "http://img.my.csdn.net/uploads/201407/26/1406383264_8243.jpg",        "http://img.my.csdn.net/uploads/201407/26/1406383248_3693.jpg",        "http://img.my.csdn.net/uploads/201407/26/1406383243_5120.jpg",
};
 protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);
//获取控件        ImageView tu = (ImageView) findViewById(R.id.tu);
        ImageLoader instance = ImageLoader.getInstance();
//第一个参数要更改地址,第二个要更改的控件        instance.displayImage(images[1],tu);
//或者   这是第二种情况
//使用的时候可以设置属性
//第二步:进行图片显示设置DisplayImageOptions options=new DisplayImageOptions.Builder()        .cacheInMemory(true)//进行内存缓存        .cacheOnDisk(true)//进行sd卡缓存        .showImageOnLoading(R.drawable.a) // 设置正在下载中的图片        .showImageForEmptyUri(R.drawable.b) //没有请求地址时        .showImageOnFail(R.drawable.c)//下载错误时        .bitmapConfig(Bitmap.Config.RGB_565)//设置图片质量        .displayer(new RoundedBitmapDisplayer(360))//圆角边框        .build();
//如果设置圆角就要给图片固定的宽高,否则图片不现实//第三步:进行加载显示ImageLoader.getInstance().displayImage(img,holder2.imageView1,options);


原创粉丝点击