Issue
I have created a custom class which extends Drawable
. I'm trying to get the dimensions of the Drawable by using onSizeChanged()
as follow
public class Circle extends Drawable {
...
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
}
}
But I'm getting an error saying that "Method does not override from the superclass". What do I do to fix it?
Thanks a lot to Michael Spitsin for this answer. To get the dimensions of the layout which the drawable is attached to, use the following code
@Override
protected void onBoundsChange(Rect bounds) {
mHeight = bounds.height();
mWidth = bounds.width();
}
Solution
If we go to View source code and in Drawable source code, we will see next things:
In View.setBackgroundDrawable(Drawable) if we pass not null, than field mBackground
(is is responsible to store background drawable) is updated.
If we will try to find usages of method Drawable.onBoundsChanged()
then we will see that it is mainly used in Drawable.setBounds
method. And if we will find usages of it, we will see next snippet in View.class:
private void drawBackground(Canvas canvas) {
final Drawable background = mBackground;
if (background == null) {
return;
}
if (mBackgroundSizeChanged) {
background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
mBackgroundSizeChanged = false;
rebuildOutline();
}
//draw background through background.draw(canvas)
}
Thus, using of onBoundsChanged
callback is accepted for your task.
Answered By - Mikhail Spitsin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.