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

后端 未结 6 1206
盖世英雄少女心
盖世英雄少女心 2020-12-20 05:29

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

6条回答
  •  生来不讨喜
    2020-12-20 06:15

    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;
    } 
    }
    

提交回复
热议问题