【问题标题】:Make square image in Glide and then round it在 Glide 中制作方形图像,然后将其四舍五入
【发布时间】:2018-11-14 13:01:03
【问题描述】:

我解决了一个典型的图像任务:尝试centerCrop() 它并像How to round an image with Glide library? 一样制作循环,但结果看起来像Glide circular image gets cropped

(非圆形)。

我认为 Glide (v.4) 无法正确裁剪图像。我尝试了许多变体,例如GlideApp.with(photo).load(url).circleCrop().into(photo)。最好先从矩形创建一个正方形,然后将其变为圆形。

这是 XML 的一部分:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <ImageView
        android:id="@+id/photo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="20dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/image_1"
        />

    <TextView
        android:id="@+id/name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="7dp"
        android:layout_marginLeft="7dp"
        android:lineSpacingExtra="1sp"
        android:textColor="@color/black"
        android:textSize="20sp"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toEndOf="@+id/photo"
        app:layout_constraintTop_toTopOf="@+id/photo"
        tools:text="Name"
        />

</android.support.constraint.ConstraintLayout>

更新

抱歉,源图片有问题。我不认为它上面和下面有白色填充(添加了线条):

还有一个例子:

这是因为他们在后端缩小了矩形图像,使其成为带有白色边框的正方形

【问题讨论】:

  • 最好使用CircleImageView。简单有效。
  • @RumitPatel,是的,但是如果我有 Glide,它可能可以处理裁剪。
  • 是的@CoolMind,你写了你已经尝试过,Glide (v.4) 没有正确裁剪图像。 所以我建议了替代和更好的方法。我有同样的问题,我更喜欢使用CircleImageView. :-)
  • @RumitPatel,谢谢。奇怪你这里有同名。 :)
  • @RumitPatel,对不起,我更新了问题。

标签: android android-glide


【解决方案1】:

使用RequestOptions.circleCropTransform()

像这样:

    Glide.with(this).load(url)
            .apply(RequestOptions.circleCropTransform())
            .into((ImageView) photo);

另外,确保 ImageView 是一个正方形。如果您使用的是 ConstraintLayout,请使用app:layout_constraintDimensionRatio="1:1"

文档:https://bumptech.github.io/glide/javadocs/400/com/bumptech/glide/request/RequestOptions.html

【讨论】:

  • 是的,我有一个正方形 ImageView (50x50)。抱歉,结果是一样的。
  • @CoolMind 然后请发布 xml 布局
  • 好的,我加了。
  • 感谢这个提示app:layout_constraintDimensionRatio="1:1"
【解决方案2】:

你可以在拍照后尝试这种方式xml

   <de.hdodenhof.circleimageview.CircleImageView
   xmlns:app="http://schemas.android.com/apk/res-auto"
   />

    dependencies {
    ...
    implementation 'de.hdodenhof:circleimageview:2.2.0'}

【讨论】:

    【解决方案3】:

    请查看以下答案:

    试一试,如果有任何问题,请告诉我。

    private RequestOptions circleOptions = new RequestOptions()
                .centerCrop()
                .circleCrop()      // responsible for circle crop
                .placeholder(R.color.color_gray)    // replace with your placeholder image or remove if don't want to set
                .error(R.color.color_gray)     // replace with your placeholder image or remove if don't want to set
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
    
    public void loadImageCircle(String url, ImageView view) {
            Glide.with(context)
                    .load(url)
                    .apply(circleOptions)
                    .into(view);
        }
    

    调用方法为:

    loadImageCircle(imafgeURL,imageView);
    

    【讨论】:

      【解决方案4】:

      图片有问题。我试着用一个两边都有白色填充的正方形画一个圆。

      然后我使用了没有填充的原始矩形图像。

      GlideApp.with(imageView)
          .load(url)
          .transform(CircleCrop())
          .into(imageView)
      

      Glide Multiple transformations in Android,发现我们可以先把矩形裁剪成正方形,再做圆角。

      val transformation = MultiTransformation(CenterCrop(), RoundedCorners(10))
      GlideApp.with(imageView)
          .load(url)
          .transform(transformation)
          .into(imageView)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-06
        • 2013-08-21
        • 2023-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-13
        • 2016-03-27
        相关资源
        最近更新 更多