简体   繁体   中英

Android Image Background with Rounded Corners

I am trying to make my image with round corners. I am doing like this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/bg" />
    <item>

        <shape android:shape="rectangle" android:padding="10dp">
            <solid android:color="#00000000" />
            <corners
                android:bottomRightRadius="15dp"
                android:bottomLeftRadius="15dp"
                android:topLeftRadius="15dp"
                android:topRightRadius="15dp"/>
            <stroke android:width="3dp" android:color="#a9a9a9"/>
        </shape>
    </item>

</layer-list>

and result is like this

在此处输入图片说明

You can see there grey color behind round corner, How I can remove it? I think I am missing something. Let me know if someone can help me for solve my issue. Thanks

You can use cardView to make corner rounded try this one

   <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="8dp"
        android:layout_margin="5dp"
        android:elevation="10dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/bg"
            android:scaleType="centerCrop"
            />
    </android.support.v7.widget.CardView>

In your code remove

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

then you can remove grey color border

I don't know if the corner space is removable or not. But you can use a mask as the foreground to your image view. Use the xml below to create a mask.

<?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:color="#555555"
                android:width="5dp"/>
        </shape>

</item>
<item android:top="-1dp" android:bottom="-1dp"  android:right="-1dp" android:left="-1dp">
        <shape android:shape="rectangle">
            <stroke
                android:color="#555555"
                android:width="6dp"/>
            <corners android:radius="16dp"/>
        </shape>
</item>

Now if your minimum SDK version is 23 then you can just set the drawable as the foreground.

android:foreground="@drawable/mask"

But if you are targeting below 23 then you can try the code below.

<FrameLayout
    android:layout_margin="16dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/thumbImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/top_tracks_song" />

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/test">
    </FrameLayout>

</FrameLayout>

try this

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#398B27"/>
            <corners android:radius="15dp" />
        </shape>

    </item>

    <item
        android:left="1dp"
        android:right="1dp"
        android:top="1dp"
        android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="15dp" />
        </shape>

    </item>

</layer-list>

Adjust

android:left="1dp"
        android:right="1dp"
        android:top="1dp"
        android:bottom="1dp"

this to get desire width


<ImageView
android:id="@+id/make_msg_attach"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/my_image"
android:layout_margin="5dp"
android:background="@drawable/this_xml_file.xml"
android:padding="2dp"
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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