12. Glide framework - Android intensive course notes

Keywords: Android Google Gradle github

1, introduction

The image loading frameworks commonly used in Android are:

  • UniversalImageLoader
  • Volley
  • Picasso
  • Fresco
  • Glide

Google's official recommended framework is Glide, and the use of other frameworks is similar.

2. Basic use of Glide

1. Create a project named GlideDemo. First add the dependencies in the Module's build.gradle file.

implementation 'com.github.bumptech.glide:glide:4.7.1'

2. The layout documents are prepared as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/ivGlide"
        android:layout_width="300dp"
        android:layout_height="300dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="load"
        android:text="Load" />
</LinearLayout>

3.MainActivity code is written as follows:

public class MainActivity extends AppCompatActivity {
    private ImageView mIvGlide;

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

        mIvGlide = findViewById(R.id.ivGlide);
    }

    public void load(View view) {
        Glide.with(this)
                .load("http://img.mukewang.com/55666c0a0001d6b506000338-240-135.jpg")
                .into(mIvGlide);
    }
}

4. Add network permission in Android manifest.

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

5. Run the application and click Load, as shown below.

3. Glide

For the above applications, we further do some optimization. For example, if the pictures are not downloaded late, they will be blank, so the experience is not good.

Posted by cirma on Thu, 09 Jan 2020 10:49:14 -0800