Should I use a LinearLayout with repeated background or a SVG ImageView to fill a whole row?

332 views Asked by At

Linked questions

The below questions couldn't finally solve my problem, described below.

Context

I use a ConstraintLayout. My aim is to show an SVG image which contains a button:

  1. Its width is 100% the screen's width - it must be repeated

  2. Its height is defined to be the space between the bottom side of a widget and the bottom side of the button - it must be repeated

The problem

Each time I tried to show this image, either the drawable was badly scaled, badly cropped or blurred.

An exemple is:

enter image description here

It should look like this:

enter image description here

Many tests

  1. I have tried to use an ImageView with the attribute src: I used every scale type, with and without the attribute that allows to set a custom ratio

  2. I have tried to use an ImageView with a background instead of src

  3. I have tried to use a RelativeLayout with a background drawable file that is repeated: so I didn't use an SVG image but its JPEG version, and even this way has resulted in bad results

  4. Nota for 3.: I'd really want to use an SVG image instead of a bitmap one, because it will be resolutions-compliant.

My question

So, given all these explanations (cf.: part Context) and given the above illustrations, how would you proceed to show this image?

1

There are 1 answers

0
fancyyou On

use java code

    ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#888888")); // bg color
    Drawable vDrawable = AppCompatResources.getDrawable(this, R.drawable.ic_vector_star); // vector drawable
    if (vDrawable != null) {
        Bitmap bitmap = Bitmap.createBitmap(vDrawable.getIntrinsicWidth(), vDrawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        vDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        vDrawable.draw(canvas);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
        bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); // set repeat

        LayerDrawable drawable = new LayerDrawable(new Drawable[]{colorDrawable, bitmapDrawable});
        findViewById(R.id.frameLayout).setBackground(drawable);
    }

example