我应该使用带有重复背景的LinearLayout还是SVG ImageView来填充整行?

问题描述 投票:0回答:1

Linked questions

以下问题无法解决我的问题,如下所述。

Context

我用的是ConstraintLayout。我的目的是显示一个包含按钮的SVG图像:

  1. 它的宽度是屏幕宽度的100% - 必须重复
  2. 它的高度定义为小部件底部和按钮底部之间的空间 - 必须重复

The problem

每次我试图显示这个图像时,要么绘制得非常严重,要么严重裁剪或模糊。

一个例子是:

enter image description here

它应该如下所示:

enter image description here

Many tests

  1. 我试图使用ImageView属性src:我使用了每种比例类型,有和没有允许设置自定义比例的属性
  2. 我试图使用背景而不是ImageViewsrc
  3. 我试图使用带有背景可绘制文件的RelativeLayout重复:所以我没有使用SVG图像但是它的JPEG版本,甚至这种方式导致了不好的结果
  4. Nota for 3:我真的想要使用SVG图像而不是位图,因为它符合分辨率。

My question

因此,鉴于所有这些解释(参见:部分背景)并给出上述插图,您将如何继续显示此图像?

android android-imageview android-background androidsvg
1个回答
0
投票

使用java代码

    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

© www.soinside.com 2019 - 2024. All rights reserved.