在Glide中制作方形图像,然后将其四舍五入

时间:2018-11-14 13:01:03

标签: android android-glide

我解决了图像的典型任务:尝试centerCrop()并像How to round an image with Glide library?那样制作圆形,但是结果看起来像Glide circular image gets croppedenter image description here

(非圆形)。

我认为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>

更新

对不起,源图像中存在问题。我不认为是这样(添加了行):

enter image description here

4 个答案:

答案 0 :(得分:1)

使用RequestOptions.circleCropTransform()

喜欢这个:

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

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

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

答案 1 :(得分:1)

拍照后可以尝试xml

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

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

答案 2 :(得分:1)

请检查以下答案:

尝试一下,让我知道是否有任何疑问。

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);

答案 3 :(得分:1)

图像出现问题。我试图从一个正方形制作一个圆圈,该正方形的两边都有白色填充物。

enter image description here

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

enter image description here

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)

enter image description here