Glide的使用

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);

    }
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值