Glide的基本用法

Quibbler 2019-10-6 808

Glide的基本用法


       Glide是大名鼎鼎的Android图片加载框架,使用及其简单方便。先来了解一下Glide的基本用法吧。Glide图片加载框架的流程原理见另外一篇博客Glide源码调用流程分析

        GitHub:https://github.com/bumptech/glide



1、依赖

       Glide是一款由Bump Technologies开发的图片加载框架,使得我们可以在Android平台上以极度简单的方式加载和展示图片。要想使用Glide,首先需要将这个库引入到我们的项目当中。在app/build.gradle文件当中添加如下依赖:

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

       另外,Glide中需要用到网络功能,因此还得在AndroidManifest.xml中声明一下网络权限才行:

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



2、使用

        然后我们想要在程序当中去加载这张图片。那么首先打开项目的布局文件,在布局当中加入一个Button和一个ImageView,如下所示

<?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="vertical">

    <Button

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Load Image"

        android:onClick="loadImage"

        />

    <ImageView

        android:id="@+id/image_view"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />
 
</LinearLayout>

        为了让用户点击Button的时候能够将刚才的图片显示在ImageView上,我们需要修改MainActivity中的代码,如下所示:

public class MainActivity extends AppCompatActivity {
 
    ImageView imageView;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.image_view);
    }
 
    public void loadImage(View view) {

        String url = "http://cn.bing.com/az/hprichbg/rb/Dongdaemun_ZH-CN10736487148_1920x1080.jpg";

        Glide.with(this).load(url).into(imageView);
    }
 }

        没错,就是这么简单。现在我们来运行一下程序,效果如下图所示:


不忘初心的阿甘
最新回复 (0)
    • 安卓笔记本
      2
        登录 注册 QQ
返回
仅供学习交流,切勿用于商业用途。如有错误欢迎指出:fluent0418@gmail.com