【问题标题】:how to make a rounded corners for an image in image view using XML layout in android如何在android中使用XML布局为图像视图中的图像制作圆角
【发布时间】:2012-04-07 21:24:07
【问题描述】:

我已经尝试了很多次,但我知道我遗漏了一些东西,请你们解释一下.. 以下是我尝试过的

    <ImageView
        android:id="@+id/Dicimage"
        android:layout_width="130px"
        android:layout_height="100px"
        android:src="@drawable/slang"
        android:background="@drawable/corner"
        android:padding="1dp"/>

在资源文件夹中创建角 XML

  <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"         
    android:shape="rectangle"> 
<solid android:color="#fff"/>    

<stroke android:width="0dp"
        android:color="#ff000000"/>

<padding android:left="2dp"
         android:top="2dp"
         android:right="2dp"
         android:bottom="2dp"/> 

<corners android:radius="30px"/> 
   </shape>

我得到的是,边框只是圆角矩形,但图像的形状仍然是矩形

【问题讨论】:

标签: android android-layout android-emulator android-widget


【解决方案1】:

不,这是不可能的。您必须以编程方式进行。

您正在做的是创建圆角背景并在其上绘制可绘制对象。

这是来自 Romain Guy 的关于如何创建圆角图像的精彩文章:http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

【讨论】:

  • 链接失效了,能补充一下基本信息吗?
【解决方案2】:

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <solid android:color="#ffffff"/>    

    <stroke android:width="3dp"
            android:color="#ff000000"/>

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"/> 

    <corners android:radius="30px"/> 
</shape>

取自这篇文章:Android ImageView with Rounded Corners not working

【讨论】:

  • 似乎只有借助编码我们才能做到..得到了结果。但是尝试不编码部分
【解决方案3】:

使用该 xml 文件作为前景

我的意思是,而不是

android:background="@drawable/corner"

使用这个

android:foreground="@drawable/corner"

【讨论】:

  • 23以下的api找到解决方法了吗?我也在角落里出现了这种奇怪的表情,请参阅here@emre-aydin
  • 属性 android:foreground 对低于 23 的 API 级别没有影响
【解决方案4】:

您应该将此代码作为 some_foreground.xml 添加到可绘制文件夹。

<?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <stroke
                    android:width="8dp"
                    android:color="@color/white" />
            </shape>
        </item>
        <item>
            <shape android:shape="rectangle">
                <corners android:radius="@dimen/padding_margin_8" />
                <stroke
                    android:width="8dp"
                    android:color="@color/white" />
            </shape>
        </item>
    </layer-list>

然后您可以将其用作 ImageView 的前景。 你应该像这样添加:

<ImageView
        android:id="@+id/event_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foreground="@drawable/some_foreground"
        android:scaleType="centerCrop"
        android:src="@drawable/your_image" />

【讨论】:

    【解决方案5】:

    您可以将 ImageView 添加到具有属性 cardElevation=0 的 CardView 中

    <android.support.v7.widget.CardView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/thumbnail_card"
                    card_view:cardCornerRadius="15dp"
                    card_view:cardElevation="0dp"
                    android:layout_margin="10dp">
    
    
                <ImageView
                    android:id="@+id/thumbnail"
                    android:layout_width="110dp"
                    android:layout_height="75dp"
                    android:scaleType="centerCrop" />
    
                </android.support.v7.widget.CardView>
    

    【讨论】:

      【解决方案6】:

      这是一个超级迟到的答案,但如果您尝试创建一个圆形 ImageView:

      在drawable文件夹中创建一个drawable,名为circle:

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="oval">
          <solid
              android:color="@android:color/black"/>
          <size
              android:height="60dp"
              android:width="60dp"/>
      </shape>
      

      图像视图:

      <ImageView
          android:id="@+id/Dicimage"
          android:layout_width="130px"
          android:layout_height="100px"
          android:src="@drawable/slang"
          android:background="@drawable/circle"
          android:padding="1dp"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-14
        • 1970-01-01
        • 2016-06-30
        • 1970-01-01
        • 2019-01-01
        • 2011-11-28
        • 1970-01-01
        相关资源
        最近更新 更多