繁体   English   中英

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

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

我正在尝试为我的 LinearLayout 创建一个具有圆角图像的背景。 我看过很多例子如何做到这一点,但并不完全是我想要的。 在大多数情况下,我见过人们使用填充来创建它,但是当我这样做时,它会绘制一种边框,我不想要任何边框,只想要圆角

    <?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>

您可以尝试使用ImageView 在图像视图集中

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

现在使用FrameLayout的图像视图。 以便其他布局可以放置在ImageView

Romain Guy 的圆角图像

使用使用 Canvas.drawRoundRect() 绘制圆角矩形的自定义 Drawable。 诀窍是使用带有 BitmapShader 的 Paint 用纹理而不是简单的颜色填充圆角矩形。

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

样本可以在@ https://docs.google.com/file/d/0B3dxhm5xm1sia2NfM3VKTXNjUnc/edit?pli=1下载

这是另一个链接

如何制作带有圆角的 ImageView?

另一个链接

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

您可以使用 Android Support Library v4 中的RoundedBitmapDrawable 您只需要创建一个实例并设置角半径:

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

您可以按照以下步骤操作:在依赖项中的 gradle 上

implementation 'com.makeramen:roundedimageview:2.3.0'

在你的 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"/>

我使用了这个博客中的一个例子,这对我有帮助。 希望这对你有用

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

我遇到了同样的问题,我只是在 Photoshop 中创建了一个带圆角的图像。 这不是涉及代码或可绘制对象的答案。

上面对库 'com.makeramen:roundedimageview:2.3.0' 的建议对我不起作用,因为我实际上想将相对布局的背景设置为带圆角的图像。

使用 cardview 不起作用,使用图像作为相对布局中的第一个视图并操纵角的圆度也不起作用。

在 Photoshop 中创建一个圆角就成功了。

暂无
暂无

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

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