XML:
<?xml version="1.0" encoding="utf-8"?> <ScrollView android:id="@+id/activity_main" 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" tools:context="com.github.glide_use_method.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:id="@+id/iv_img" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:id="@+id/btn_loadImage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Glide加载图片"/> <Button android:id="@+id/btn_loadImageGif" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Glide加载Gif图片"/> </LinearLayout> </ScrollView> MainActivity:package com.github.glide_use_method; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.ImageView; import com.bumptech.glide.Glide; import com.bumptech.glide.load.engine.DiskCacheStrategy; public class MainActivity extends Activity implements View.OnClickListener { private ImageView ivShow; private Button btnLoadImag; private Button btnLoadGif; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { ivShow = (ImageView) findViewById(R.id.iv_img); btnLoadImag = (Button) findViewById(R.id.btn_loadImage); btnLoadImag.setOnClickListener(this); btnLoadGif = (Button) findViewById(R.id.btn_loadImageGif); btnLoadGif.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btn_loadImage: loadImage(); break; case R.id.btn_loadImageGif: loadGifImag(); break; } } private void loadImage() { Glide.with(this) .load("http://d.hiphotos.baidu.com/image/h%3D200/sign=4241e02c86025aafcc3279cbcbecab8d/562c11dfa9ec8a13f075f10cf303918fa1ecc0eb.jpg") //设置占位图 .placeholder(R.mipmap.ic_launcher) //加载错误图 .error(R.mipmap.ic_launcher) //磁盘缓存的处理 .diskCacheStrategy(DiskCacheStrategy.ALL) .into(ivShow); } private void loadGifImag() { Glide.with(MainActivity.this) .load("http://img1.imgtn.bdimg.com/it/u=2931812858,1706079417&fm=21&gp=0.jpg") .asGif() .diskCacheStrategy(DiskCacheStrategy.SOURCE) .into(ivShow); } }