2011-06-01 52 views
1

我在Android上的图层列表有问题。我想做一堆简单的图像。我可以做到这一点下面的代码:旋转正在裁剪图层列表项目

<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> 

但是当我运行它,结果被裁剪的顶部和底部,下面的图像上看到:

cropped_image

我还有一个疑问:如果我在<item>上放置一个ID,我如何在View上检索它,以便我可以在代码中更改位图? documentation *表示我必须使用View.findViewByID,但我想获得BitmapDrawable,并且我无法将视图投射到Drawable!

我也尝试使用Canvas.drawBitmap在CustomView上编码相同的东西,但它看起来非常难看,如果有人能指出一个很好的解决方案,我会很感激。

在此先感谢

回答

0

什么是你将在图层列表?什么是使用它作为drawable的XML?你有没有尝试添加一些顶部和底部填充到包含图层可绘制的视图?


至于从代码中访问该位,则需要首先得到LayerDrawable,然后用其getDrawable或findDrawableByLayerId方法。

例如,如果你使用的位图项层作为其ID的视图的背景是“容器”,你可以这样做:你的回答

// 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); 
+0

谢谢,但我不干试图使用图层列表并在自定义视图上实现相同的视觉效果,直接在画布上绘制位图。 – 2011-06-27 14:43:35