如何在 Android 中的 LayerDrawable 中调整 Drawable 的大小和位置
我有一个 LayerDrawable,它应该显示 5 个位图。 1 个背景和 4 个图标(每个 LayerDrawable 可能不同),例如网格。不幸的是,这些图标的宽度与高度不同。我的问题是调整它们的大小,同时保持它们的比例并将它们放置在它们的位置内(1-4 个图标,5 个背景):
==================
| | | |
| 1 | | 2 |
|------.5 .------|
|------. .------|
| 3 | | 4 |
| | | |
==================
如果我调整这些位图的大小,给它们 Gravity.CENTER
它们将不适合不再位于背景矩形的边界内。如果我离开 Gravity.CENTER
,一旦我调用 layerDrawable.setLayerInset(numlayer, left, top, right, Bottom)
,它们似乎就会重新缩放。 正确的做法是什么?如何在正确放置它们的同时保持它们的比例? 到目前为止我还无法理解的是为什么 BitmapDrawable 中的 getIntrinsicWidth/Height 返回的内容比原始位图中的 getWidth/Height 返回的东西(更小)。
我尝试过的:
1. 从 XML 中获取它们,将“dummy-drawable”放入其中并将其替换为上述结果。 2. 在我的程序中创建 LayerDrawable,并在其中传递所有可绘制对象。看来 setLayerInset 没有影响,或者我只看到背景(这是我的可绘制数组中的元素 0)
3.扩展LayerDrawable并尝试自己跟踪任何没有合理结果的事情
I have a LayerDrawable, which should display 5 Bitmaps. 1 background and 4 Icons (which can be different for every LayerDrawable) like a Grid. 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):
==================
| | | |
| 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. If I leave Gravity.CENTER
they seem to get rescaled as soon as I call 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.
What I tried:
1. Getting them from XML, putting a 'dummy-drawable' inside and replace it with result described above.
2. Create LayerDrawable inside my Program passing all drawables inside. It seems that setLayerInset has no affect, or I only see the Background (which was Element 0 inside my Drawable Array)
3. Extending LayerDrawable and try to keep track on anything myself without reasonable results
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,
BitmapDrawable
使用给定的Bitmap
填充其边界。如果您开始使用Gravity
,Bitmap
将根据给定的Gravity
定位。我想在你的情况下,最好的选择是创建你自己的Drawable
。关于 getIntrinsic[Height|Width] 方法,它们返回底层位图的实际大小,如果系统在加载时调整其大小(例如在 mdpi 屏幕上加载的 hdpi 图像),则该大小可能会有所不同)。
The
BitmapDrawable
, dy default, fills its bounds with the givenBitmap
. If you start usingGravity
, theBitmap
will be position according to the givenGravity
. I suppose in your case, the best choice is to create your ownDrawable
.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).