【问题标题】:Is Every ImageView Need CardView?每个 ImageView 都需要 CardView 吗?
【发布时间】:2019-11-13 09:24:36
【问题描述】:

我正在使用 imageView 上传个人资料图片,但我遇到了一个问题..

    <ImageView
        android:id="@+id/profile_image"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@drawable/roundimage"
        android:src="@drawable/profile_pic"
        android:scaleType="centerCrop"
        android:layout_centerHorizontal="true" />

我设置了圆角背景并设置了图像图标的来源,因此当我从图库中选择图片时,我选择的图片没有圆角。我的意思是它看起来像方形图像而不是圆角图像..

【问题讨论】:

  • 如果你想在没有CardView的情况下这样做,那么你需要实现一个自定义ImageView。详情请参阅stackoverflow.com/questions/2459916/…
  • 这不是 src 和后台的工作方式。 src 图像未剪辑到背景。绘制背景(忽略比例类型),然后在其上绘制 src。本机图像视图不符合您的要求。有多种第三方库可以做到这一点。

标签: android xml imageview cardview


【解决方案1】:

如果您使用的是 Glide V4

这样试试

Glide.with(this.context)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(14)))
                .into(ImageView);

【讨论】:

    【解决方案2】:

    不需要。有几个库,例如

    1. CircleImageView

    2。 RoundedImageView

    你可以像下面这样使用 CircleImageView

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

    内部布局

    <de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/profile_image"
        android:layout_width="96dp"
        android:layout_height="96dp"
        android:src="@drawable/profile"
        app:civ_border_width="2dp"
        app:civ_border_color="#FF000000"/>
    

    您可以使用 RoundedImageView 如下

        repositories {
        mavenCentral()
    }
    
    dependencies {
        compile 'com.makeramen:roundedimageview:2.3.0'
    }
    

    内部布局

     <com.makeramen.roundedimageview.RoundedImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/imageView1"
            android:src="@drawable/photo1"
            android:scaleType="fitCenter"
            app:riv_corner_radius="30dip"
            app:riv_border_width="2dip"
            app:riv_border_color="#333333"
            app:riv_mutate_background="true"
            app:riv_tile_mode="repeat"
            app:riv_oval="true" />
    

    有关更多信息,请访问以下网站 CircleImageView

    RoundedImageView

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多