Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using setLayerInset on LayerDrawable with Bitmap gives unexpected results

I'm dynamically placing small dot bitmaps ('hotspots') over a large bitmap image using LayerDrawable. The layer at index 0 contains the large image. The dots' positions on the image are specified to me in terms of percentage of large image. For example, I might have a hotspot that is supposed to be 50% from the left and 75% from the top of the large base image. In order to position the dots over the correct part of the image, I'm using

setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)

and calculating the offsets based on the width and height of the ImageView which contains the LayerDrawable. I'm happy that the calculations are correct and that the right height and width are being retrieved, and expect the dots to be correctly displayed, but they are not. The dots are being skewed along the Y axis and their positions are incorrect (too high and too far to the left). Has anyone encountered a similar problem and found a solution?

like image 767
Brendan Avatar asked Dec 16 '22 12:12

Brendan


1 Answers

I spend a day and a half working on this, trying to make sense of the numbers, and here's what I found:

In the situation I am dealing with, where the first layer contains a BitmapDrawable and I'm setting insets for the subseqent layers, the offset in the call:

setLayerInset(layer, leftOffset, topOffset, rightOffset, bottomOffset)

are relative to the dimensions of the bitmap in the first layer, even if those dimensions are greater than the view in which the DrawableLayer is being placed (e.g. when the Bitmap is being redimensioned automatically by the view).

Bizarre and unexpected, but true. Hope this saves somebody some time.

like image 119
Brendan Avatar answered Jan 12 '23 01:01

Brendan