Android图片加载框架Glide简单使用

wshunli
2018-01-19 / 0 评论 / 169 阅读 / 正在检测是否收录...

Glide 是一个快速高效的 Android 图片加载库,注重于平滑的滚动。Glide 提供了易用的 API,高性能、可扩展的图片解码管道(decode pipeline),以及自动的资源池技术。

Glide 介绍

Glide 支持拉取,解码和展示视频快照,图片,和GIF动画。Glide的Api是如此的灵活,开发者甚至可以插入和替换成自己喜爱的任何网络栈。默认情况下,Glide使用的是一个定制化的基于HttpUrlConnection的栈,但同时也提供了与Google Volley和Square OkHttp快速集成的工具库。

虽然Glide 的主要目标是让任何形式的图片列表的滚动尽可能地变得更快、更平滑,但实际上,Glide几乎能满足你对远程图片的拉取/缩放/显示的一切需求。

Glide 简单使用

0、添加 Glide 依赖

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.5.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'
}

1、准备图片容器

我们在布局文件中添加 ImageView 控件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#8f8f8f"
    tools:context="com.wshunli.glidedemo.MainActivity">

    <Button
        android:id="@+id/bt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

2、加载图片

我们以加载 https://img.wshunli.com/Android/Glide/Glide.min.png 为例。

Glide.with(MainActivity.this)
        .load("https://img.wshunli.com/Android/Glide/Glide.min.png")
        .into(img);

真的超级简单。

MainActivity 完整代码如下:

package com.wshunli.glidedemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {

    private ImageView img;
    private Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = findViewById(R.id.img);
        bt = findViewById(R.id.bt);

        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Glide.with(MainActivity.this)
                        .load("https://img.wshunli.com/Android/Glide/Glide.min.png")
                        .into(img);
            }
        });

    }
}

注意别忘了声明权限:

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

3、取消加载图片

Glide.with(MainActivity.this).clear(img);

取消加载也超级简单。

4、最终加载效果

Glide加载图图片

ProGuard 混淆规则

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

参考资料
1、bumptech/glide: An image loading and caching library for Android focused on smooth scrolling
https://github.com/bumptech/glide
2、Android图片加载框架最全解析(一),Glide的基本用法 - 郭霖的专栏 - CSDN博客
http://blog.csdn.net/guolin_blog/article/details/53759439
3、Glide v4 : 快速高效的Android图片加载库
https://muyangmin.github.io/glide-docs-cn/

0

评论 (0)

取消