簡體   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