【问题标题】:Universal Image Loader: Crop center image loading synchronously (no imageview) (Android, UIL)通用图像加载器:裁剪中心图像同步加载(无图像视图)(Android,UIL)
【发布时间】:2014-05-25 18:45:28
【问题描述】:

我正在使用通用图像加载器来:

  • 同步加载图片

  • 固定方向

  • 裁剪中心(这不起作用)

  • 以低内存占用完成所有这些操作

如果我可以使用通用图像加载器来缩放位图,我不想自己缩放。我问这个问题的原因是因为我不知道通用图像加载器是否可能。

我不想(或不需要)使用 ImageView 来加载位图,我只需要一个位图,但可以即时裁剪它。 UIL可以做到这一点吗?我已经通过关于 GH 的问题,通过关于 SO 的问题,但找不到它的示例。

这是代码:

private Bitmap getBitmap(Uri uri, int width, int height) {
    DisplayImageOptions options = new DisplayImageOptions.Builder()
            .considerExifParams(true)
            .cacheOnDisk(true)
            .build();
    ImageSize size = new ImageSize(width, height);
    return mImageLoader.loadImageSync(uri.toString(), size, options);
}

谢谢大家!

【问题讨论】:

  • Android Crop Center of Bitmap 的可能重复项
  • 您提到的问题要求通用解决方案。我在问如何使用 Universal Image Loader 来做这件事,我相信可以用 Yes 或 No 和一些代码来回答。
  • 您的 getBitmap() 方法返回一个 Bitmap 实例。这就是您在另一个问题上应用答案所需的全部内容。
  • 我不想手工做。如果可能的话,我想用 UIL 来做这件事,这正是我所要求的。

标签: android universal-image-loader


【解决方案1】:

我认为这就是 BitmapProcessors 的用途(尽管我自己没有使用过这个库)。

我会这样做:

// preProcessor gets to work before image gets cached, so cache will contain the cropped
// version
[...Builder...].preProcessor(new BitmapProcessor() {
    public Bitmap process (Bitmap src) {
        // Roughly assuming that the bitmap is bigger than the needed dimensions
        // but you get the idea
        int xOffset = (src.getWidth() - width) / 2;
        int yOffset = (src.getHeight() - height) / 2;
        Bitmap result = Bitmap.createBitmap(src, xOffset, yOffset, width, height);
        // not sure whether this is necessary or the calling logic does it
        // my guess is it does 'cause it's a simple check [if (src != result) src.recycle();]
        // and the lib is rather smart altogether. Check the docs, they probably mention this
        src.recycle();
        return result;
    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 2013-05-12
    相关资源
    最近更新 更多