【问题标题】:Imageview not showing rounded cornersImageview不显示圆角
【发布时间】:2020-09-07 15:30:28
【问题描述】:

我正在尝试使我的布局像这样,但我无法使我的图像视图像这样带有圆角。它在 Android Studio 的布局编辑器中完美显示,但在真实设备中我得到了平角。我厌倦了 XML 和代码,但没有一个对我有用。请帮我把我的布局制作成这个示例图片。

我的布局文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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_height="match_parent"
    android:layout_width="match_parent"
    android:clickable="true"
    android:background="?android:attr/colorBackground"
    android:focusable="true">

    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true">

        <ProgressBar

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/User_CoverPhoto"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <ImageView

            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:background="@drawable/round_shape_only"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/User_CoverPhoto"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <ProgressBar

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/User_ProfilePictures"
            app:layout_constraintTop_toTopOf="parent"
            />
        <ImageView

            android:layout_width="230dp"
            android:layout_height="match_parent"
            android:background="@drawable/round_shape_only"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/User_ProfilePictures"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:background="#40000000"
            android:backgroundTint="#CC000000"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Profile Picture"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/User_ProfilePictures"
            app:layout_constraintHorizontal_bias="0.01"
            app:layout_constraintStart_toStartOf="@+id/User_ProfilePictures" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:background="#40000000"
            android:backgroundTint="#CC000000"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Cover Photo"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/User_CoverPhoto" />

    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:clipToPadding="false"
        android:paddingTop="13dp"

        android:id="@+id/Grid_Recycler">

    </androidx.recyclerview.widget.RecyclerView>



</RelativeLayout>

圆形的XML

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#CCCCCC"/>
    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"/>


    <corners android:radius="15dp"/>
</shape>

【问题讨论】:

标签: android xml android-layout android-imageview


【解决方案1】:

只需使用材料组件库中的ShapeableImageView
比如:

  <com.google.android.material.imageview.ShapeableImageView
      ...
      app:shapeAppearanceOverlay="@style/roundedCornersImageView"
      app:srcCompat="@drawable/ic_image" />

与:

  <style name="roundedCornersImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">xxdp</item>
  </style>

*注意:至少需要1.2.0-alpha03版本。

【讨论】:

    【解决方案2】:

    你可以使用滑翔

    只需为 ImageView 编写扩展函数:

    1. 制作带圆角的矩形图像

       fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
           Glide.with(this)
               .load(resId)
               .transform(RoundedCorners(radius))
               .into(this)
      
    2. 制作带圆角的方形图像

       fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
           Glide.with(this)
               .load(resId)
               .transform(CenterCrop(), RoundedCorners(radius))
               .into(this)
      

    在代码中使用

    iv_service_avatar.loadImageWithCustomCorners(R.drawable.ic_default_avatar, 15)
    

    iv_service_avatar 是 ImageView ID

    【讨论】:

      【解决方案3】:

      当使用backgroundradius 时,您应该调用setClipToOutlinetrue,它只能以编程方式调用,不能由XML 设置

      imageView.setClipToOutline(true);
      

      【讨论】:

        【解决方案4】:

        对于圆角,您可以使用以下自定义布局:

        Java : RoundedCornerImageLayout.java

        public class RoundedCornerImageLayout extends FrameLayout {
            private final static float CORNER_RADIUS = 10.0f;
            private float cornerRadius;
        
            public RoundedCornerImageLayout(Context context) {
                super(context);
                init(context, null, 0);
            }
        
            public RoundedCornerImageLayout(Context context, AttributeSet attrs) {
                super(context, attrs);
                init(context, attrs, 0);
            }
        
            public RoundedCornerImageLayout(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init(context, attrs, defStyle);
            }
        
            private void init(Context context, AttributeSet attrs, int defStyle) {
                DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
                setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }
        
        
            @Override
            protected void dispatchDraw(Canvas canvas) {
                int count = canvas.save();
        
                final Path path = new Path();
                path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), cornerRadius, cornerRadius, Path.Direction.CW);
                canvas.clipPath(path, Region.Op.INTERSECT);
        
                canvas.clipPath(path);
                super.dispatchDraw(canvas);
                canvas.restoreToCount(count);
            }
        }
        

        您可以根据需要更改private final static float CORNER_RADIUS = 10.0f;

        在 XML 文件中

        <androidx.constraintlayout.widget.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="match_parent"
            tools:context=".RoundedImageViewActivity">
            <com.example.stackdemo.ui.RoundedCornerImageLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent">
                <ImageView
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:src="@drawable/ocean"
                    android:scaleType="centerCrop"/>
            </com.example.stackdemo.ui.RoundedCornerImageLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        上述代码的输出是:

        希望对你有帮助

        【讨论】:

        • 使用自定义ImageView 比使用FrameLayout 更好
        【解决方案5】:

        您可以将ImageView 放入CardView 并使用app:cardCornerRadius 设置图片角

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-15
          • 2021-06-09
          • 1970-01-01
          相关资源
          最近更新 更多