原文:How to Rotate Images 作者:Norman Peitek 翻译:Dexter0218
不久前,我们有一个问题:如何用Glide旋转图像,由于Picasso提供此即学即用的功能。不幸的是,Glide不提供这一点方法调用,但在这个博客文章中,我们将展示如何几乎一样容易地使它。 如果您需要更多Glide的内容,浏览我们博客文章列表上的主题:
文/签到钱就到(简书作者) 原文链接:http://www.jianshu.com/p/a0eb280af7ae著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
实际上android.graphics.Matrix类正好提供了我们需要的。旋转图片的代码实际上非常直截了当:
Bitmap toTransform = ... // your bitmap source Matrix matrix = new Matrix(); matrix.postRotate(rotateRotationAngle); Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);为了让它对我们更有用,特别是在使用Glide的情况下,我们在一个BitmapTransformation里包装它:
public class RotateTransformation extends BitmapTransformation { private float rotateRotationAngle = 0f; public RotateTransformation(Context context, float rotateRotationAngle) { super( context ); this.rotateRotationAngle = rotateRotationAngle; } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { Matrix matrix = new Matrix(); matrix.postRotate(rotateRotationAngle); return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true); } @Override public String getId() { return "rotate" + rotateRotationAngle; } }如果你还没有明白这个类里发生了什么,回顾看一下在自定义变换那篇文章的介绍,你会看到你需要知道的东西。
最后,我们看看新变换的几个例子:
private void loadImageOriginal() { Glide .with( context ) .load( eatFoodyImages[0] ) .into( imageView1 ); } private void loadImageRotated() { Glide .with( context ) .load( eatFoodyImages[0] ) .transform( new RotateTransformation( context, 90f )) .into( imageView3 ); }当然,你可以改变第二个参数去设置要被旋转多少角度。你可以动态地设置它!
你需要用Glide旋转图像,这里提供所有的代码和知识,即使它没有在库里直接提供。如果这对你有用,我们会对你在下面友好评论表示感激!
文/签到钱就到(简书作者) 原文链接:http://www.jianshu.com/p/a0eb280af7ae 著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。