java - 如何以编程方式向 ImageView 添加圆角边框?

标签 java android xml imageview drawable

到目前为止,我能够做到这一点的方法是创建一个 XML 可绘制对象:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#00ffffff" />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp" />
    <corners android:radius="12dp" />
    <stroke android:width="3dp" android:color="#ffffffff" />
</shape>

在我的布局 XML 中:

        <FrameLayout
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="top|center_horizontal">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/profilePicture"
                android:scaleType="fitXY"
                android:clickable="true"
                android:contentDescription="@string/content_desc_profile_picture" />

            <ImageView
                android:src="@drawable/frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:contentDescription="@string/content_desc_default_profile_picture" />

        </FrameLayout>

我的问题是框架没有覆盖其后面可绘制的 profilePicture 的角,导致实际图像超出边框。怎样才能遮住角落呢?真希望现在有一个类似 CSS 的属性!

最佳答案

形状.xml

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

您的布局.xml

 <FrameLayout
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_gravity="top|center_horizontal" xmlns:android="http://schemas.android.com/apk/res/android">



    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/shape"
    />
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/profilePicture"
            android:scaleType="fitXY"
            android:clickable="true"
            android:layout_margin="5dp"
            android:src="@drawable/your_image"
            android:background="@drawable/shape" />

</FrameLayout>

关于java - 如何以编程方式向 ImageView 添加圆角边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39663716/

相关文章:

java桌面应用程序-在本地服务器上共享一台数据库主机

java - Spring boot 无法访问多个数据源

java - Spring Boot Jar 中的类路径扫描不起作用

java - 我的 setter 和 getter 中的问题

android - ViewPager 中的不同配置

android - android中的摇晃/摆动 View 动画

java - 从日志文件中提取 Java 错误堆栈

android - 如何获取 Cell 当前使用的频率 GSM/CDMA (Android)

xml - 在 XML 中使用大于符号

sql-server - SQL Server 为 xml 路径添加属性和值