Android之Glide获取图片PathBitmap用法
Posted 台风中的橘子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android之Glide获取图片PathBitmap用法相关的知识,希望对你有一定的参考价值。
之前先整理了Glide加载图片的一些常用api;
今天主要研究了Glide获取图片Path、Bitmap用法,相信也困扰了大家很久,我在网上也找了很久,基本没有,后来研究了下,也参考了下api文档,总结了以下几个方式:
1. 获取Bitmap:
1)在图片下载缓存好之后获取
[java] view plain copy
- Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>()
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation)
- image.setImageBitmap(resource);
- ); //方法中设置<span style="font-family: Arial, Helvetica, sans-serif;">asBitmap可以设置回调类型</span>
[java] view plain copy
- Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>()
- @Override
- public void onLoadStarted(Drawable placeholder)
- @Override
- public void onLoadFailed(Exception e, Drawable errorDrawable)
- @Override
- public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation)
- //TODO set bitmap
- @Override
- public void onLoadCleared(Drawable placeholder)
- @Override
- public void getSize(SizeReadyCallback cb)
- @Override
- public void setRequest(Request request)
- @Override
- public Request getRequest()
- return null;
- @Override
- public void onStart()
- @Override
- public void onStop()
- @Override
- public void onDestroy()
- );
2)通过url获取
[java] view plain copy
- Bitmap myBitmap = Glide.with(applicationContext)
- .load(yourUrl)
- .asBitmap() //必须
- .centerCrop()
- .into(500, 500)
- .get()
2. 获取图片缓存路径
[java] view plain copy
- FutureTarget<File> future = Glide.with(mContext)
- .load("url")
- .downloadOnly(500, 500);
- try
- File cacheFile = future.get();
- String path = cacheFile.getAbsolutePath();
- catch (InterruptedException e)
- e.printStackTrace();
- catch (ExecutionException e)
- e.printStackTrace();
- Android之Glide获取图片PathBitmap用法