简体   繁体   English

如何创建带有图像、无边框圆角的背景

[英]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.我正在尝试为我的 LinearLayout 创建一个具有圆角图像的背景。 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 .您可以尝试使用ImageView In Image view set在图像视图集中

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

now use the image view in FrameLayout .现在使用FrameLayout的图像视图。 so that other layouts can be placed over the ImageView以便其他布局可以放置在ImageView

Romain Guy's image with rounded corner Romain Guy 的圆角图像

Use a custom Drawable that draws a rounded rectangle using Canvas.drawRoundRect().使用使用 Canvas.drawRoundRect() 绘制圆角矩形的自定义 Drawable。 The trick is to use a Paint with a BitmapShader to fill the rounded rectangle with a texture instead of a simple color.诀窍是使用带有 BitmapShader 的 Paint 用纹理而不是简单的颜色填充圆角矩形。

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/ 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样本可以在@ https://docs.google.com/file/d/0B3dxhm5xm1sia2NfM3VKTXNjUnc/edit?pli=1下载

Here's another link这是另一个链接

How to make an ImageView with rounded corners? 如何制作带有圆角的 ImageView?

Another link另一个链接

http://ruibm.com/?p=184 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.您可以使用 Android Support Library v4 中的RoundedBitmapDrawable 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您可以按照以下步骤操作:在依赖项中的 gradle 上

implementation 'com.makeramen:roundedimageview:2.3.0'

on your xml file在你的 xml 文件上

<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/ 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.我遇到了同样的问题,我只是在 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.上面对库 'com.makeramen:roundedimageview:2.3.0' 的建议对我不起作用,因为我实际上想将相对布局的背景设置为带圆角的图像。

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.使用 cardview 不起作用,使用图像作为相对布局中的第一个视图并操纵角的圆度也不起作用。

Creating a rounded corner in photoshop did the trick.在 Photoshop 中创建一个圆角就成功了。

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

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