Glide加载图片,视频,Gif图,设置圆形图片和圆角图片,清除缓存

Glide

      • Glide的应用

Glide的应用

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private String image_url = "http://p1.music.126.net/yC_df5u0myXVp-bM99K3Lw==/5870292580832850.jpg";
    private String video_url = "http://uvideo.spriteapp.cn/video/2019/0512/56488d0a-7465-11e9-b91b-1866daeb0df1_wpd.mp4";
    private String image_path = "/storage/emulated/0/xxx.jpg";
    private String video_path = "/storage/emulated/0/movie.mp4";
    private String gif_url = "http://i.kinja-img.com/gawker-media/image/upload/s--B7tUiM5l--/gf2r69yorbdesguga10i.gif";

    private Button bt1;
    private Button bt2;
    private Button bt3;
    private Button bt4;
    private Button bt5;
    private Button bt6;
    private Button bt7;
    private ImageView iv;
    private Button bt0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
        initView();
    }

    private void initView() {
        bt1 = (Button) findViewById(R.id.bt1);
        bt2 = (Button) findViewById(R.id.bt2);
        bt3 = (Button) findViewById(R.id.bt3);
        bt4 = (Button) findViewById(R.id.bt4);
        bt5 = (Button) findViewById(R.id.bt5);
        bt6 = (Button) findViewById(R.id.bt6);
        bt7 = (Button) findViewById(R.id.bt7);
        iv = (ImageView) findViewById(R.id.iv);

        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt0 = (Button) findViewById(R.id.bt0);
        bt0.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt1:
                Glide.with(this).load(image_url).into(iv);
                break;
            case R.id.bt2:
                Glide.with(this).load(image_path).into(iv);
                break;
            case R.id.bt3:
                Glide.with(this).load(video_url).into(iv);
                break;
            case R.id.bt4:
                Glide.with(this).load(video_path).into(iv);
                break;
            case R.id.bt5:
                Glide.with(this).load(gif_url).into(iv);
                break;
            case R.id.bt6:
                RequestOptions options = new RequestOptions()
                        .bitmapTransform(new CircleCrop())//圆形
                        .error(R.mipmap.error)//加载失败显示的图片
                        .placeholder(R.mipmap.ic_launcher);//占位图
                Glide.with(this).load(image_url).apply(options).into(iv);
                break;
            case R.id.bt7:
                //圆角图片
                Glide.with(this).load(image_url).apply(new RequestOptions().bitmapTransform(new RoundedCorners(50))).into(iv);
                break;
            case R.id.bt0:
                Glide.get(this).clearMemory();//清除运行缓存
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Glide.get(MainActivity.this).clearDiskCache();//清除SD卡缓存
                    }
                });
                break;
        }
    }
}

你可能感兴趣的:(Glide加载图片,视频,Gif图,设置圆形图片和圆角图片,清除缓存)