2013-05-22 86 views
1

我正在嘗試爲我的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> 

回答

3

羅曼蓋伊的形象與圓潤的邊角

使用,吸引了使用Canvas.drawRoundRect()圓角矩形的自定義繪製對象。訣竅是使用帶有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

這裏是另一個鏈接

How to make an ImageView with rounded corners?

另一個鏈接

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

你可以嘗試使用ImageView。在圖像視圖集

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

現在使用圖像視圖在FrameLayout。這樣其他的佈局可放置在ImageView

1

您可以使用Android支持庫v4中的RoundedBitmapDrawable。所有你需要的是創建一個實例並設置圓角半徑:

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