Android: Layer-List not working?

I am trying to put two oval drawings on top of each other, the first has transparency. Be that as it may, it displays the first oval color with the size of the second oval.

<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:shape="oval"> <size android:width="15dp" android:height="15dp" /> <solid android:color="#35000000"/> </shape> </item> <item> <shape android:shape="oval"> <size android:width="5dp" android:height="5dp" /> <solid android:color="#000000"/> </shape> </item> </layer-list> 

How can I make this work as planned?

EDIT: Here is the parent:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_layerlist" /> </LinearLayout> 
+6
source share
1 answer

Having done a lot of research on different types of XML drawings, it seems that your LayerDrawable (layer-list) will scale ShapeDrawables independently. Then , ImageView scales the LayerDrawable . According to this guide from Google , scaling for both ShapeDrawable and LayerDrawable is troubling. LayerDrawable will check the necessary scaling for each element that you have. They have several solutions:

  • Set gravity to something that does not scale, for example, "center".
  • determine how to draw as a bitmap.
  • Set ImageView to scaleType, which does not scale.

There are two serious problems with this, however ... You can use gravity for bitmap only. And you cannot use ShapeDrawable for bitmap in XML. I tried everything . I would have thought that everything was right. Here is just the thing that fixed it for me.

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/oval1" android:scaleType="center" /> <ImageView android:id="@+id/imageView2" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/oval2" android:scaleType="center" /> </FrameLayout> 

So: I removed LayerDrawable, split Shapes into my own XML, made two ImageViews. (for ease, I stuck them in FrameLayout). This was the only way to stop scaling.


Testing procedure

Tested in Android 2.1, 3.0, 4.0

  • scaleType Image
  • Changed image width and height
  • Divided ShapeDrawables
  • LayerList changed only item with drawable attribute associated with shared shape s
  • Changed LayerList to bitmap referring to split shape s.
  • Reordered shape s

Alternatively, you can do this in code.


+7
source

Source: https://habr.com/ru/post/917746/


All Articles