繁体   English   中英

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

[英]How can I programatically add a rounded border to my ImageView?

到目前为止,我能够做到这一点的方法是创建一个XML drawable:

<?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的属性!

shape.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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM