How to have a Fullscreen Background Image (with center-crop) that doesn't Resize How to have a Fullscreen Background Image (with center-crop) that doesn't Resize android android

How to have a Fullscreen Background Image (with center-crop) that doesn't Resize


try this LayerDrawable (res/drawable/backlayer.xml):

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">    <item>        <shape>        <solid android:color="#f00" />        </shape>    </item><item>        <bitmap        android:gravity="top" android:src="@drawable/back1">        </bitmap>    </item></layer-list>

and set it to your top level layout: android:background="@drawable/backlayer"

UPDATE: try this BitmapDrawadle, set it to top level layout (setBackgroundDrawable()), if simpleMapping == true is good enough you can remove "else" branch:

class D extends BitmapDrawable {    private Matrix mMatrix = new Matrix();    private int moldHeight;    public D(Resources res, Bitmap bitmap) {        super(res, bitmap);    }    @Override    protected void onBoundsChange(Rect bounds) {        if (bounds.height() > moldHeight) {            moldHeight = bounds.height();            Bitmap b = getBitmap();            RectF src = new RectF(0, 0, b.getWidth(), b.getHeight());            RectF dst;            // if simpleMapping is good enough then remove "else" branch and            // declare "dst" as:            // RectF dst = new RectF(bounds);            boolean simpleMapping = true;            if (simpleMapping) {                dst = new RectF(bounds);            } else {                float x = bounds.exactCenterX();                dst = new RectF(x, 0, x, bounds.height());                float scale = bounds.height() / src.height();                float dx = scale * src.width() / 2;                dst.inset(-dx, 0);            }            mMatrix.setRectToRect(src, dst, ScaleToFit.CENTER);        }    }    @Override    public void draw(Canvas canvas) {        canvas.drawColor(0xaa00ff00);        canvas.drawBitmap(getBitmap(), mMatrix, null);    }}


The solution in the answer is the best, to use it:

Resources res = getActivity().getResources();Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.some_image);BackgroundBitmapDrawable background = new BackgroundBitmapDrawable(res, bitmap);view.setBackgroundDrawable(background);

or

view.setBackground(background);

for API 16 and above.


Put everything in a FrameLayout, with first child an imageview which is going to match it's parent (the frameLayout) and you configure it as you want your background, and in front of the image view you put whatever you want (LinearLayout I suppose)