android glide 4旋转图片,Glide入门教程——21.如何旋转图片

Glide — 如何旋转图片

不久前,我们有一个问题:如何用Glide旋转图像,由于Picasso提供此即学即用的功能。不幸的是,Glide不提供这一点方法调用,但在这个博客文章中,我们将展示如何几乎一样容易地使它。

如果您需要更多Glide的内容,浏览我们博客文章列表上的主题:

Glide 系列概览

如何用Glide旋转图片

实际上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旋转图像,这里提供所有的代码和知识,即使它没有在库里直接提供。如果这对你有用,我们会对你在下面友好评论表示感激!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值