android - No padding-effect in custom ImageView -
i've got issue assigning padding-values custom imageview. please note, i'm not extending imageview
view
:
public class customimageview extends view
the image loaded assets folder via path. source- , target-rect
calculated in onlayout(...)
, bitmap assigned in ondraw(...)
.
good news: image displayed ;-)
bad news: assigning padding-values setpadding(...)
has no effect on image (problem solve!). tested "normal" imageview
-object , worked desired. unfortunately task extend custom class view
, not imageview
. if knows how solve - , i'm not first 1 problem ;-) - let me know!
here's ondraw
-method, no magic:
@override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (bitmap != null) canvas.drawbitmap(bitmap, sourcerect, targetrect, paint); }
when setting paddings view, should considered during drawing. can achieve this, creating bitmapdrawable
variable in custom view, , initialize , draw according padding, example.
private bitmapdrawable mdrawable; public void setbitmap(bitmap bitmap) { mdrawable = new bitmapdrawable(getresources(), bitmap); invalidate(); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if (mdrawable != null) { int paddingleft = getpaddingleft(); int paddingtop = getpaddingtop(); int paddingright = getpaddingright(); int paddingbottom = getpaddingbottom(); int contentwidth = getwidth() - paddingleft - paddingright; int contentheight = getheight() - paddingtop - paddingbottom; mdrawable.setbounds(paddingleft, paddingtop, paddingleft + contentwidth, paddingtop + contentheight); mdrawable.draw(canvas); } }
Comments
Post a Comment