简体   繁体   中英

How to create a background with Image, Rounded Corners without borders

I'm trying to create a background for my LinearLayout that has an Image with rounded corners. I've seen many examples how to do that but not exactly what I want. In most of cases I've seen people using padding to create it, but when I do this it draws a kind of border, and I don't want any border, just the rounded corner

    <?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item >
    <shape>
            <corners android:topLeftRadius="20dp" android:topRightRadius="20dp"/>
    </shape> 
    </item>
     <item >
        <bitmap android:src="@drawable/header"/>
    </item>
</layer-list>

you can try using ImageView . In Image view set

android:src="@drawable/yourimage"
android:background="@drawable/cornershape"

now use the image view in FrameLayout . so that other layouts can be placed over the ImageView

Romain Guy's image with rounded corner

Use a custom Drawable that draws a rounded rectangle using Canvas.drawRoundRect(). The trick is to use a Paint with a BitmapShader to fill the rounded rectangle with a texture instead of a simple color.

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

The sample can be downloaded @ https://docs.google.com/file/d/0B3dxhm5xm1sia2NfM3VKTXNjUnc/edit?pli=1

Here's another link

How to make an ImageView with rounded corners?

Another link

http://ruibm.com/?p=184

public class ImageHelper {
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
        .getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);

final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = pixels;

paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);

return output;
} 
}

You can just use RoundedBitmapDrawable from Android Support Library v4. All you need is create an instance and set corner radius:

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap);
final float roundPx = (float) bitmap.getWidth() * 0.06f;
roundedBitmapDrawable.setCornerRadius(roundPx);

you can follow this below: on your gradle in dependencies

implementation 'com.makeramen:roundedimageview:2.3.0'

on your xml file

<com.makeramen.roundedimageview.RoundedImageView
        android:id="@+id/img_home_assistance"
        android:layout_width="match_parent"
        android:layout_height="123dp"
        app:riv_corner_radius="@dimen/padding_margin_15"
        android:layout_marginTop="6.8dp"
        android:layout_marginLeft="7.3dp"
        android:layout_marginRight="7.5dp"
        android:src="@drawable/migrate"
        android:scaleType="centerCrop"/>

i used an example from this blog and this helped me out. hope this will be useful to you

http://manishkpr.webheavens.com/android-rounded-corner-image-bitmap-example/

I was having the same problem and I simply created an image with rounded corners in Photoshop. This is not an answer that involves code or drawables.

The above suggestion with the library 'com.makeramen:roundedimageview:2.3.0' did not work for me as I actually wanted to set the background of a Relative Layout to an image with rounded corner.

Using a cardview didnt work, neither did using an image as the first view in the relative layout and manipulating the roundness of the corner.

Creating a rounded corner in photoshop did the trick.

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