I'm having a problem with layer-list on Android. I want to do a simple pile of images. I can achieve that with the following code:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="0" android:toDegrees="0">
<bitmap
android:src="@drawable/teste1"
android:gravity="center" />
</rotate>
</item>
<item>
<rotate
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="-9" android:toDegrees="-9">
<bitmap
android:src="@drawable/teste3"
android:gravity="center" />
</rotate>
</item>
<item>
<rotate
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="9" android:toDegrees="9">
<bitmap
android:src="@drawable/teste2"
android:gravity="center" />
</rotate>
</item>
</layer-list>
But when I run it, the result is cropped on the top and on the bottom, as seen on the following image:
I have another doubt: if I put an ID on the <item>
how do I retrieve it on my View so I can change the bitmap in code? The documentation* says I have to use an View.findViewByID, but I want to get the BitmapDrawable, and I can't cast a View to a Drawable!
I also tried coding the same thing开发者_JAVA百科 on a CustomView, with Canvas.drawBitmap, but it's looking very ugly, if someone can point out a good solution using that I would appreciate too.
Thanks in advance
What are you putting that layer list in? What's the XML that uses it as a drawable? Have you tried adding some top and bottom padding to the view that contains your layer drawable?
As for accessing the bitmap from code, you need to get the LayerDrawable first, then use its getDrawable or findDrawableByLayerId methods.
For example, if you're using a layer of bitmap items as the background of a view whose id is 'container' you could do this:
// get the layer list being used as the background of 'container'
View view = findViewById(R.id.container);
LayerDrawable layer = (LayerDrawable)view.getBackground();
// get the first layer's BitmapDrawable by index
BitmapDrawable bg = (BitmapDrawable)layer.getDrawable(0);
// get the BitmapDrawable of the layer whose id is 'teste2'
BitmapDrawable bgTeste2 = (BitmapDrawable)layer.findDrawableByLayerId(R.id.teste2);
// do whatever you want with the drawable
bg.setAlpha(60);
精彩评论