Android 使用Glide加载网络图片等比例缩放
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形。显然按照android:scaleType不能实现,因为会有很多限制,所以必须要自己写算法。
通过Glide来缩放
其实glide提供了这样的方法。具体是显示继承Transformation 的 setResource 方法。
(1) 先获取网络或本地图片的宽高
(2) 获取需要的目标宽
(3) 按比例得到目标的高度
(4) 按照目标的宽高创建新图
/**
* ===========================================
* 版 本:1.0
* 描 述:设置图片等比缩放
* <p>glide处理图片.</p>
* ===========================================
*/
public class TransformationUtils extends ImageViewTarget<Bitmap> { private ImageView target; public TransformationUtils(ImageView target) {
super(target);
this.target = target;
} @Override
protected void setResource(Bitmap resource) {
view.setImageBitmap(resource); //获取原图的宽高
int width = resource.getWidth();
int height = resource.getHeight(); //获取imageView的宽
int imageViewWidth = target.getWidth(); //计算缩放比例
float sy = (float) (imageViewWidth * 0.1) / (float) (width * 0.1); //计算图片等比例放大后的高
int imageViewHeight = (int) (height * sy);
ViewGroup.LayoutParams params = target.getLayoutParams();
params.height = imageViewHeight;
target.setLayoutParams(params);
}
}
之后在Glide设置transform
Glide.with(this)
.load(newActiviteLeftBannerUrl)
.asBitmap()
.placeholder(R.drawable.placeholder)
.into(new TransformationUtils(target));
Transformation 这是Glide的一个非常强大的功能了,它允许你在load图片 -> into ImageView 中间这个过成对图片做一系列的变换。比如你要做图片高斯模糊、添加圆角、做度灰处理、圆形图片等等都可以通过Transformation来完成。
Android 使用Glide加载网络图片等比例缩放的更多相关文章
- Android 使用Picasso加载网络图片等比例缩放
在做android图片加载的时候,由于手机屏幕受限,很多大图加载过来的时候,我们要求等比例缩放,比如按照固定的宽度,等比例缩放高度,使得图片的尺寸比例得到相应的缩放,但图片没有变形.显然按照andro ...
- Android笔记之使用Glide加载网络图片、下载图片
Glide简介 不想说太多,真的很方便:P)可以节省我不少时间 GitHub地址:https://github.com/bumptech/glide 加载网络图片到ImageView Glide.wi ...
- 仿微信朋友圈图片查看-glide加载网络图片,photoview 实现缩放
http://www.cnblogs.com/csonezp/p/5083286.html 这里实现的效果就和微信朋友圈点击图片后查看大图一样,如果你不清楚是什么效果,可以拿出手机,打开朋友圈,找到一 ...
- Android使用Glide加载https链接的图片不显示的原因
平时我们使用Glide加载http网址的图片的时候,图片可以正常加载出来,但是如果服务器端加上了安全认证,当加载自签名的https图片的时候就会报如下错误(证书路径验证异常). 我们如果不修改Glid ...
- Android使用Glide加载Gif.解决Glide加载Gif非常慢问题
在Glide文档中找了半天没发现加载Gif的方式.然后通过基本的用法去加载: Glide.with(MainActivity.this).load(url).asGif().into(imageVie ...
- Android使用glide加载.9图片的方法
我们在开发过程中会经常使用.9图片, 因为它可以使图片拉伸的时候,保证其不会失真. 而我们把.9图片放在服务器端,通过glide直接加载,会报错. 我们的解决方法是 通过sdk的aapt工具 把.9图 ...
- Android通过URL加载网络图片
public static Bitmap getBitmap(String path) throws IOException { URL url = new URL(path); HttpURLCon ...
- xamarin.Android ImageView 异步加载网络图片
/// <summary> /// 异步获取文件流 /// </summary> /// <param name="url"></para ...
- Android 解决Glide 加载图片缓慢(第一次加载不出来图片)的Bug
随机推荐
- [Swift]LeetCode173. 二叉搜索树迭代器 | Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [Swift]LeetCode228. 汇总区间 | Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [Swift]LeetCode623. 在二叉树中增加一行 | Add One Row to Tree
Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...
- [Swift]LeetCode904. 水果成篮 | Fruit Into Baskets
In a row of trees, the i-th tree produces fruit with type tree[i]. You start at any tree of your cho ...
- SH2018笔试题之成长值问题
一.题目 详见代码 二.代码 import java.util.Scanner; /** * 3 * 1 1 5 10 * 2 3 4 * 1 4 6 -5 */ public class main4 ...
- 启动MongoDB shell客户端会什么会一闪而过
解决办法: 在MongoDB shell客户端根目录下右击选择 "在此处打开命令窗口"; 在命令窗口敲入mongod --dbpath=d:/mongodb/data ; //我的 ...
- Spring 中 SQL 的存储过程
SimpleJdbcCall 类可以被用于调用一个包含 IN 和 OUT 参数的存储过程.你可以在处理任何一个 RDBMS 时使用这个方法,就像 Apache Derby, DB2, MySQL, M ...
- ARP欺骗攻击
一.ARP攻击概述 ARP攻击主要是存在于局域网中,通过伪造IP地址和MAC地址实现ARP欺骗,能够在网络中产生大量的ARP通信量使网络阻塞,攻击者只要持续不断的发出伪造的ARP响应包就能更改目标主机 ...
- PHP大法
Topic Link http://ctf5.shiyanbar.com/DUTCTF/index.php Notes: 1) 进去发现 根据提示查看是否存在.txt文件,打开之后发现有东西 2)分析 ...
- C#线程安全使用(二)
刚才想了半天文章应该起什么名字,最后决定起名为<线程安全使用>,线程安全这个词很难理解,感觉就像托管这词一样,但是托管翻译成英文是managed,我通常把他翻译成被管理,这样就好理解多了, ...