Android

Android JetPack学习总结(十二)

简易图片画廊

Volley入门

添加volley依赖:

implementation 'com.android.volley:volley:1.1.1'

使用Volley获取数据:

String url="https://www.jd.com";
        final TextView textView=findViewById(R.id.textView);

        //创建一个请求队列
        RequestQueue queue= Volley.newRequestQueue(this);
        //创建一个request,可以请求string的request或json的request
        //参数:请求类型(获取|上传),网址,成功回调、错误回调
        StringRequest stringRequest=new StringRequest(
                StringRequest.Method.GET,
                url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        textView.setText(response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e(TAG,"onErrorResponse",error);
                    }
                }
        );
        queue.add(stringRequest);

使用volley加载图片:

String url="https://cdn.pixabay.com/photo/2020/09/02/18/03/girl-5539094_960_720.jpg";
        final ImageView imageView=findViewById(R.id.imageView);

        //创建一个请求队列
        RequestQueue queue= Volley.newRequestQueue(this);
        //第一个参数是请求队列,第二个参数是缓存(此处没有设置)
        ImageLoader imageLoader=new ImageLoader(queue, new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String url) {
                return null;
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {

            }
        });
        //获取图片
        imageLoader.get(url, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                imageView.setImageBitmap(response.getBitmap());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG,"onErrorResponse",error);
            }
        });

为ImageLoader设置缓存:

ImageLoader imageLoader=new ImageLoader(queue, new ImageLoader.ImageCache() {
           private LruCache<String,Bitmap> cache=new LruCache<>(50);
            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url,bitmap);
            }
        });

使用NetWorkImageView来加载图片:

final NetworkImageView imageView=findViewById(R.id.imageView);

        //创建一个请求队列
        RequestQueue queue= Volley.newRequestQueue(this);
        //第一个参数是请求队列,第二个参数是缓存(此处没有设置)
        ImageLoader imageLoader=new ImageLoader(queue, new ImageLoader.ImageCache() {
           private LruCache<String,Bitmap> cache=new LruCache<>(50);
            @Override
            public Bitmap getBitmap(String url) {
                return cache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                cache.put(url,bitmap);
            }
        });
        //获取图片
        imageView.setImageUrl(url,imageLoader);

Glide入门

添加Glide依赖:

implementation 'com.github.bumptech.glide:glide:4.11.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

使用Glide来获取、显示图片:

Glide.with(this)
                .load(url)
                .placeholder(R.drawable.ic_launcher_background)//占位符
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(imageView);

Glide会自动处理缓存等问题。

Swiperefreshlayout入门

下拉刷新

添加Swiperefreshlayout依赖:

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.1.0"

设置下拉触发器:

swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadImage();
            }
        });

在资源就绪或者下载失败时取消加载的动画:

void loadImage(){
        Random random=new Random();
        String url =random.nextBoolean()?url1:url2;
        Glide.with(this)
                .load(url)
                .placeholder(R.drawable.ic_launcher_background)//占位符
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        if (swipeRefreshLayout.isRefreshing()){
                            swipeRefreshLayout.setRefreshing(false);
                        }
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (swipeRefreshLayout.isRefreshing()){//当资源就绪时,取消下拉
                            swipeRefreshLayout.setRefreshing(false);
                        }
                        return false;
                    }
                })
                .into(imageView);
    }

发表评论