Android layout background xml texture

梦想的初衷 提交于 2019-12-04 20:06:00

I called this res/drawable/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"
/>

I use it like:

android:background="@drawable/bg_tile"

Obiously, I prepared a tile to be mirrored horizontally and vertically (most tiles make a funny effect like that) and called it res/drawable-a-dpi-resolution/tile.png

a-dpi-resolution being ldpi, mdpi, ..., xxxhdpi

If you want to use a composite background (i.e. a gradient and a tiling bitmap over it - but the bitmap has to be somehow transparent, or it will cover the gradient), you can make a LayerList background (so that different "layers" are dran one ontop of another)

To do that, you'll need to add another background file, i.e.: res/drawable/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>

and finally the LayerList, which sticks them together (res/drawable/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>

Note that the first item is drawn first. Next items are drawn ontop of each other, being the last one the "uppermost".

So, finally you would use this like:

android:background="@drawable/bg"

Nice!

to make your image corners rounded use following function:

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

in onCreate do this:

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!