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

后端 未结 6 1205
盖世英雄少女心
盖世英雄少女心 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 05:54

    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/

    0 讨论(0)
  • 2020-12-20 05:57

    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"/>
    
    0 讨论(0)
  • 2020-12-20 05:59

    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

    0 讨论(0)
  • 2020-12-20 05:59

    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);
    
    0 讨论(0)
  • 2020-12-20 06:13

    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.

    0 讨论(0)
  • 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;
    } 
    }
    
    0 讨论(0)
提交回复
热议问题