【问题标题】:Add rounded corners to images为图像添加圆角
【发布时间】:2020-05-13 15:12:34
【问题描述】:

如何为图像添加圆角和一点阴影,使其看起来像附加的?

这是我的活动中的内容

        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <!-- Content here -->

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="107dp"
            android:layout_height="166dp"
            android:layout_marginStart="20dp"
            android:elevation="5dp"
            android:scaleType="fitCenter"
            android:translationZ="12dp"
            android:layout_marginTop="15dp" />

【问题讨论】:

标签: android kotlin


【解决方案1】:

你有不同的选择。

  • 您可以使用CardView 作为根布局。
  • 您可以使用MaterialShapeDrawable (check this answer) 将圆角应用于您的LinearLayout
  • Material Components Library1.2.0-alpha03版本开始,您可以将ImageView更改为新的ShapeableImageView

类似:

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

与:

  <style name="roundedImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">8dp</item>
  </style>

或以编程方式:

float radius = getResources().getDimension(R.dimen.corner_radius);
imageView.setShapeAppearanceModel(imageView.getShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,radius)
    .build());

【讨论】:

    【解决方案2】:

    您可以使用准备好的东西,例如https://github.com/hdodenhof/CircleImageView

    或制作您的自定义背景

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <!-- View background color -->
        <solid
            android:color="@color/colorPrimary" >
        </solid>
        <!-- The radius makes the corners rounded -->
        <corners
            android:radius="8dp" >
        </corners>
    
    </shape>
    

    您可以在角标签中更改半径 然后将其标记为背景

     <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2015-01-29
      • 1970-01-01
      • 2011-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      相关资源
      最近更新 更多