【问题标题】:How to load only part of image with Glide from Firebase?如何使用 Firebase 中的 Glide 仅加载部分图像?
【发布时间】:2017-02-20 09:57:24
【问题描述】:

我有 imageview,它应该只显示图像的一部分(适合屏幕的一部分)。用户单击后,它将打开整个图像。但我不知道如何仅将部分图像加载到 imageview 中。问题是 Glide 总是将图像放入 imageview 中。

Glide.with(context)
     .using(new FirebaseImageLoader())
     .load(storageReference)
     .into(imageView);

现在的样子。红色部分为 ImageView。

我多么希望拥有它。

编辑

以下代码会适合,但它会显示整个图像,仅显示其中的一部分,并且会破坏比例。

android:scaleType="fitXY"
android:adjustViewBounds="true"

【问题讨论】:

  • 在更新为imageView之前可以使用simpletarget处理图片
  • 我更新了答案

标签: android firebase imageview android-glide


【解决方案1】:

在您的XMLImageView 中添加比例类型。添加这个android:scaleType="matrix"

【讨论】:

  • 我认为你应该在负值中添加 marginRight 和 marginBottom。
  • android:scaleType="matrix" 是解决方案。我遇到了问题,该图像没有足够的点来覆盖从一侧显示到另一侧的图像视图。这可以通过重新采样图像或使用 android:scaleY 和 android:scaleX 来处理。
  • 好的,希望你喜欢我的解决方案。添加你也可以应用我的第二个解决方案。因为你的 ImageView 从右和底部切开,这会给你想要的效果。
【解决方案2】:
Glide.with(mContext)
                .using(new FirebaseImageLoader())
                .load(storageReference)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Bitmap targetBitmap = Bitmap.createBitmap(resource, x, y, width, height);
                        imageView.setImageBitmap(targetBitmap);
                    }
                });

您可以在此处根据您的尺寸从 Glide 调整位图的大小。但请确保您提供的尺寸始终小于实际位图。

由于您在列表中使用此代码,因此应全局声明 Bitmap targetBitmap; 以在整个列表中重用相同的位图对象。这将减少您应用的 RAM 消耗。

如果您需要更清晰的代码Bitmap targetBitmap = Bitmap.createBitmap(resource, x, y, width, height);,请阅读与裁剪位图相关的链接

不要忘记将targetBitmap设为Global,否则内存影响太大。

【讨论】:

    【解决方案3】:

    只需将其添加到您的 Imageview 中

    android:adjustViewBounds="true"
        android:scaleType="fitXY"
    

    【讨论】:

    • 这只会使图像适合 ImageView 而没有任何不需要的背景。但是这家伙想要显示图像的一部分。
    • 谢谢,但这似乎不是解决方案,正如您在我的问题编辑中看到的那样。
    • 你尝试过其他的 scaletypes 吗?像矩阵,centercrop等
    • 我尝试了矩阵(它非常接近所需的结果,但它不适合左右两侧的图像 - 左右两侧都有空间)并且我不想居中,因为我必须显示图像的顶部。 FitStart 也不好,其余的都是中心。
    【解决方案4】:

    将此添加到您的 xml 中

     android:scaleType="fitXY"
     android:adjustViewBounds="true"
    

    【讨论】:

    • 谢谢,但这似乎不是解决方案,正如您在我的问题编辑中看到的那样。
    • 你也可以使用android:scaleType="centerCrop"
    【解决方案5】:

    好的,感谢 Mohammed Atif、Patel Jaimin 和 Henry,我最终得到了以下解决方案。

    <ImageView
     android:id="@+id/ivImage"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:scaleType="matrix"/>
    
     Glide.with(this)
            .using(new FirebaseImageLoader())
            .load(storageReference)
            .asBitmap()
            .into(new SimpleTarget<Bitmap>() {
             @Override
             public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        Bitmap targetBitmap = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight());
                        ivImage.setImageBitmap(targetBitmap);
    
                        final Matrix matrix = ivImage.getImageMatrix();
                        final float imageWidth = ivImage.getDrawable().getIntrinsicWidth();
                        final float scaleRatio = ivImage.getWidth() / imageWidth;
                        matrix.postScale(scaleRatio, scaleRatio);
                        ivImage.setImageMatrix(matrix);
                    }
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多