【问题标题】:how can I shape Circular the selected image from gallery如何从图库中塑造圆形选定的图像
【发布时间】:2020-01-15 11:04:18
【问题描述】:

如何拥有一个圆形的 ImageView。这是我的xml

<RelativeLayout
            android:id="@+id/imgUser"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:background="@drawable/circle_border">

            <ImageView
                android:id="@+id/studentimg"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:layout_margin="9dp"
                android:adjustViewBounds="true"
                android:background="@drawable/circlepf"
                android:padding="3dp"
                android:scaleType="centerInside"
                android:src="@drawable/ic_profile" />
</RelativeLayout>

这是我的问题图片示例

但我希望这张图片显示在circular view

【问题讨论】:

标签: java android xml material-components material-components-android


【解决方案1】:

您可以使用Material Components Library
1.2.0-alpha03 版本有新的 ShapeableImageView

  <com.google.android.material.imageview.ShapeableImageView
      ...
      android:scaleType="centerInside"
      android:adjustViewBounds="true"
      app:shapeAppearanceOverlay="@style/circleImageView"
      app:srcCompat="@drawable/ic_profile" />

与:

  <style name="circleImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

【讨论】:

  • 不,这不是我的问题,我的问题是当我通过 onclicklistner 从图库中获取图像时,显示的是完整形状而不是圆形。
  • 你可以在下面的图片中看到我的问题陈述
  • @ChudamaniMeher 抱歉,但我不清楚。答案中发布的代码允许将图像显示为圆形。
【解决方案2】:

我最近在 Github 上发现了这个库。 https://github.com/christophesmet/android_maskable_layout

只要向 maskLayout 的 mask 属性提供所需的形状,任何包含在 MaskableFrameLayout 中的布局或图像视图都将被裁剪为所需的形状,如下所示:

<com.christophesmet.android.views.maskableframelayout.MaskableFrameLayout
    android:id="@+id/frm_mask_animated"
    android:layout_width="100dp"
    app:porterduffxfermode="DST_IN"
    app:mask="@drawable/drawable_circular"
    android:layout_height="100dp">

    <ImageView android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:scaleType="centerCrop"
               android:src="@drawable/unicorn"/>

</com.christophesmet.android.views.maskableframelayout.MaskableFrameLayout>

此外,您还可以使用 Circleimageview 库:https://github.com/hdodenhof/CircleImageView

【讨论】:

  • 不,这不是我的问题,我的问题是当我通过 onclicklistner 从图库中获取图像时,显示的是完整形状而不是圆形。
  • 你可以在下面的图片中看到我的问题陈述
  • 是的,我知道如果您希望您的图像视图在圆形布局中被屏蔽,您可以使用 MaskableFrameLayout。在这种情况下应该可以正常工作
猜你喜欢
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-06
  • 2020-12-22
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
相关资源
最近更新 更多