2014-01-23 67 views
0

我搜索了很多,但沒有找到任何東西。我想爲佈局創建背景,但不是用顏色或漸變填充它,而是要用紋理填充它並重復它。我試過這個,但沒有奏效。你能告訴我一個解決方案嗎?Android佈局背景xml紋理

card_texture.xml

<?xml version="1.0" encoding="utf-8"?> 
    <bitmap 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:src="@drawable/paper_texture" 
     android:tileMode="repeat" 
    /> 

card_shape.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape 

    android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android"> 

    <solid android:color = "@android:color/transparent"/> 

    <stroke android:width="2dp" 
     android:color="@color/text_color"/> 

    <corners 
     android:radius="20dp" 
     /> 

</shape> 

card_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/card_texture" > 
     <corners 
      android:radius = "20dp"/> 

    </item> 
    <item android:drawable="@drawable/card_shape" /> 
</layer-list> 

,這是我如何使用它在我的佈局android:background="@drawable/card_background"

我得到這樣的東西。但我想在角落的紋理(用紅色標出)是透明的,以適合黑色邊框

screenshot

回答

2

我叫這個RES /繪製/ bg_tile.xml

<?xml version="1.0" encoding="utf-8"?> 
<bitmap 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/tile" 
    android:tileMode="mirror" 
    android:dither="true" 
    android:antialias="true" 
/> 

我使用它像:

android:background="@drawable/bg_tile" 

Obiously,我製備的瓷磚被水平和垂直鏡像(離最ES作出這樣一個有趣的效果),並把它稱爲RES /繪製-A-dpi的分辨率/ tile.png

一個dpi的分辨率是LDPI,MDPI,...,xxxhdpi

如果你想使用複合背景(即一個漸變和一個平鋪位圖 - 但位圖必須是某種透明的,或者它將覆蓋漸變),你可以製作一個LayerList背景(以便不同的「圖層」在另一個的頂部)。

要做到這一點,你需要添加其他的背景文件,即:RES /繪製/ bg_radial.xml

<shape 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" 
    > 
    <gradient 
     android:startColor="#@color/bar_int_md" 
     android:centerColor="#@color/bar_mid_md" 
     android:endColor="#@color/bar_ext_md" 
     android:gradientRadius="480" 
     android:type="radial" 
    /> 
</shape> 

終於LayerList,這粘在一起(RES /繪製/ BG。 xml):

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/bg_radial" /> 
    <item android:drawable="@drawable/bg_tile" /> 
</layer-list> 

請注意,首先繪製第一項。接下來的項目在彼此的頂部繪製,作爲最後一個「最上面」。

所以,最後你可以使用此類似:

android:background="@drawable/bg" 

不錯!

+0

凡使用它?我有一個佈局,其中有'background =「@ drawable/theXML_that_I_have_written」',另外我需要圓角和筆劃 – user3199577

+0

我以爲你想用一張圖片... –

+0

我編輯了我的答案,使其更加普遍化,畢竟,它應該給你從這裏做出任何你想要的東西的基礎 - 背景現在沒有更多的祕密給你了! ;) –

1

,讓您的圖像邊角圓潤使用以下功能:

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

onCreate做到這一點:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.menu) 
    View rl = (View) findViewById(R.id.root);//get the reference of the layout that you set as background 

    LayerDrawable layer = (LayerDrawable) rl.getBackground(); 

    BitmapDrawable bg = (BitmapDrawable) layer.getDrawable(1); 


    Drawable d =new BitmapDrawable(getRoundedCornerBitmap(bg.getBitmap(),10));//pass corner radius 
    layer.setDrawableByLayerId(1, d); 
    rl.setBackgroundDrawable(d); 

} 
+0

謝謝你的回答,但沒有辦法在XML中做到這一點,而不是編程方式 – user3199577

+0

你可以有圓角的圖像,但你仍然需要裁剪你的圖像,我不認爲會有辦法在XML中實現這一點 –

+0

我認爲我不能有一個圓角的圖像,因爲背景紋理應該重複,以適應屏幕 – user3199577