I've got an issue assigning padding-values to my custom ImageView. Please note, that I'm not extending ImageView
but View
:
public class CustomImageView extends View
The image is loaded from the assets folder via path. Source- and Target-Rect
are calculated in onLayout(...)
and the bitmap is assigned in onDraw(...)
.
Good news: The Image is displayed ;-)
Bad News: Assigning padding-values by setPadding(...)
has no effect on the image (problem to solve!). I tested this with a "normal" ImageView
-Object and it worked like desired. But unfortunately the task is to extend my custom class from View
, not ImageView
. So if anybody knows how to solve this - and I'm definitely not the first one with this problem ;-) - let me know!
Here's the onDraw
-Method, no magic:
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (bitmap != null)
canvas.drawBitmap(bitmap, sourceRect, targetRect, paint);
}
When you are setting paddings to view, they should be considered during the drawing. You can achieve this, by creating a
BitmapDrawable
variable in your custom view, and then initialize it and draw according to padding, for example.