Glide进阶详解(十二)

来源:互联网 发布:英国的青年知乎 编辑:程序博客网 时间:2024/05/29 06:34

如何用 Glide 旋转图片

事实上,android.graphics.Matrix 类提供了我们所需要的准确办法(甚至更多办法)。这个代码片段就是用来旋转图像的:

Bitmap toTransform = ... // your bitmap sourceMatrix 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;    }}

如果你不知道上面这个类发生了,去看看我们介绍的 Custom Transformations,它将告诉你所有你要知道的。

最后,让我们看看新的转换的实例:

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 Rotate Transformation

当然,你可以改变第二个参数来设置你需要的旋转的角度。你甚至可以动态设置它!

图像基础,用 Glide 加载 Gif 和 Video

我们从简单介绍和演示了 Glide 加载图片,Gif 和本地视频的方式。这部分适用于每个刚开始用 Glide 的人。

  • 开始! 原文:Getting Started & Simple Loading
  • 加载进阶 原文:Advanced Loading
  • 显示 Gif 和 Video 原文:Displaying Gifs & Videos

图像显示和占位符

接下来,我们看了如何让 Glide 用在 ListView 或 GrideView 的适配器中。我们也向你展现了 Glide 的占位符实现和渐现动画。

  • ListAdapter(ListView, GridView) 原文:ListAdapter (ListView, GridView)
  • 占位符 和 渐现动画 原文:Placeholders & Fade Animations

图片大小重设和缩略图

后来学了如何加载和显示图片,我们转义到了基本的图片处理。首先我们已经介绍了你对 Glide 的可能的选项来改变图像的大小和缩放。我也展示了你可以请求和利用缩略图。

  • 图片重设大小 和 缩放 原文:Image Resizing & Scaling
  • 缩略图 原文:Thumbnails

缓存和请求优先级

Glide 就像任何 Android 中的图片加载库,它的缓存组件部分决定了这个库是活的还是死的。在缓存基础的文章中,我们已经呈现了 Glide 方法的构建预览。此外,我们展示了如何处理单个单个请求的缓存行为。在下面的博客中,我们呈现了你该如何去对请求做优先级排序,以及确保重要图片首先被加载和显示。

  • 缓存基础 原文:Caching Basics
  • 请求优先级 原文:Request Priorities

Glide Target 的回调

在接下来的两篇博客中,我们一直假定你是加载图片到标准的 ImageView 中。在这两篇中,我们可以选择如何使用 Glide 去异步加载图片到不同的 target 中。如果你需要去加载图片到自定义的视图中,通知或应用小部件,就这些啦:

  • 回调:SimpleTarget 和 ViewTarget 用于自定义视图类 原文:Callbacks: SimpleTarget and ViewTarget for Custom View Classes
  • 加载图片到通知栏和应用小部件中 原文:Loading Images into Notifications and RemoteViews

异常 和 调试

当创建了一个新应用,在开发过程中不是所有的事情都是正常工作的。重要的是当你不知道一些东西为什么不工作的时候有什么样的方法可以处理。这就是为什么我们介绍了一篇特别的文章就是为了调试和错误处理。这可能听起来不是很有吸引力,但重要的是让你知道在未来碰到此类问题的时候,你可以如何处理:

  • 异常:调试和错误处理 原文:Exceptions: Debugging and Error Handling

Glide 转换

在向你展示了所有 Glide 基础之后,我们开始了更多的自定义功能。如果你需要在显示图片之前做处理,自定义转换的博客写给你看了:

  • 自定义转换 原文:Custom Transformation
  • 如何旋转图像 原文:How to Rotate Images

Glide 动画

Glide 不仅可以转换动画,它还能控制图片的显示。如果你想要给你的图片增加一个 eye-popping 动画,可以读读这篇博客:

  • 用 animate() 自定义动画 原文:Custom Animations with animate()

Glide Module

我们最后一个主题是 Glide module。Glide module 给了一个抽象的方式来自定义每个组件和 Glide 的行为。如果你正式在生产应用中使用 Glide 的话,确保你都看过这些了。这可能和一块宝石一样重要:

  • 集成网络栈 原文:Integrating Network Stacks
  • 用 Module 自定义 Glide 原文:Customize Glide with Modules
  • Module 实例:接受自签名证书的 HTTPS 原文:Glide Module Example: Self-Signed HTTPS Network Stack
  • Module 实例:自定义缓存 原文:Glide Module Example: Customize Caching
  • Module 实例:用自定义尺寸优化加载的图片 原文:Glide Module Example: Optimizing By Loading Images In Custom Sizes
  • 动态使用 Model Loader 原文:Dynamically Use Model Loaders

0 0