简体   繁体   English

如何在 Android 的 LayerDrawable 中调整大小和 position Drawables

[英]how to resize and position Drawables inside a LayerDrawable in Android

I have a LayerDrawable, which should display 5 Bitmaps.我有一个 LayerDrawable,它应该显示 5 个位图。 1 background and 4 Icons (which can be different for every LayerDrawable) like a Grid. 1 个背景和 4 个图标(每个 LayerDrawable 可能不同),如网格。 Unfortunately these Icons don't have the same width than height.不幸的是,这些图标的宽度与高度不同。 My Problem is to resize them, while keeping their ratio and place them inside their Position (1-4 Icons, 5 background):我的问题是调整它们的大小,同时保持它们的比例并将它们放在 Position (1-4 个图标,5 个背景)中:

==================
|      |  |      |
|   1  |  |   2  |
|------.5 .------|
|------.  .------|
|   3  |  |   4  |
|      |  |      |
==================

If I resize these Bitmaps, give them Gravity.CENTER they won't fit anymore inside the Bounds of the Background Rectangle.如果我调整这些位图的大小,给它们Gravity.CENTER ,它们将不再适合背景矩形的边界。 If I leave Gravity.CENTER they seem to get rescaled as soon as I call layerDrawable.setLayerInset(numlayer, left, top, right, bottom) .如果我离开Gravity.CENTER ,他们似乎会在我调用layerDrawable.setLayerInset(numlayer, left, top, right, bottom)立即重新缩放。 What is the right way to do it?正确的方法是什么? How can I keep their ratio while placing them correctly?如何在正确放置它们的同时保持它们的比例? What I also couldn't understand so far is why getIntrinsicWidth/Height from BitmapDrawable is returning something else (smaller) than getWidth/Height from original Bitmap.到目前为止我还无法理解的是为什么来自 BitmapDrawable 的 getIntrinsicWidth/Height 返回的东西比来自原始 Bitmap 的 getWidth/Height 更小。
What I tried:我尝试了什么:
1. Getting them from XML, putting a 'dummy-drawable' inside and replace it with result described above. 1. 从 XML 获取它们,在里面放一个“dummy-drawable”并用上述结果替换它。 2. Create LayerDrawable inside my Program passing all drawables inside. 2.在我的程序中创建LayerDrawable,传递里面的所有drawable。 It seems that setLayerInset has no affect, or I only see the Background (which was Element 0 inside my Drawable Array)似乎 setLayerInset 没有影响,或者我只看到背景(这是我的可绘制数组中的元素 0)
3. Extending LayerDrawable and try to keep track on anything myself without reasonable results 3. 扩展 LayerDrawable 并尝试在没有合理结果的情况下自己跟踪任何事情

The BitmapDrawable , dy default, fills its bounds with the given Bitmap . BitmapDrawable , dy 默认值,用给定的Bitmap填充其边界。 If you start using Gravity , the Bitmap will be position according to the given Gravity .如果您开始使用Gravity ,根据给定的GravityBitmap将是 position 。 I suppose in your case, the best choice is to create your own Drawable .我想在你的情况下,最好的选择是创建你自己的Drawable

Concerning the getIntrinsic[Height|Width] methods, they return the actual size of your underlying Bitmap which may be different if the system resized it it while loading it (an hdpi image loaded on an mdpi screen for instance).关于 getIntrinsic[Height|Width] 方法,它们返回底层Bitmap的实际大小,如果系统在加载它时调整它的大小(例如在 mdpi 屏幕上加载的 hdpi 图像),它可能会有所不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM