Creating a custom view: how can I extend a class and access the base class's private member variables?
As an exercise, I am trying to extend ImageButton
to add some features I think would be useful. One, in particular, is a drow shadow. I've hit the proverbial wall with this one.
It seems to me, that a class extending BitmapDrawable
is necessary. This class contains a Paint
object used to draw the bitmap to the screen. If I had access to this Paint
object... all I would have to do is call it's setShadowLayer()
method... but, alas, it is private
. There is a public
method, getPaint()
that will return the paint object, but any modifications to it would be useless unless there were a corresponding setPaint()
method. (There isn't.)
Currently, my thought process looks something like the following...
- Create class
ShadowBitmapDrawable
whichextends BitmapDrawable
- Within this class, somehow alter the
BitmapDrawable
'sPaint
object withPaint
'ssetShadowLayer()开发者_StackOverflow社区
method. - In my custom
ImageButton
class, callsetImageDrawable(Drawable d)
, and pass it myShadowBitmapDrawable
object.
Step 2 is the road block. What can I do to change BitmapDrawable
's Paint
object? Note that I added my thought process only as an indicator of where I am in this problem. I am open to other suggestions.
Here are some references:
- BitmapDrawable source (extends
Drawable
to draw bitmaps) - ImageView source (base class of
ImageButton
) - Drawable source
What can I do to change BitmapDrawable's Paint object?
Clone BitmapDrawable
into your project, refactor it into your own package, make the data member protected
(or provide a protected
setter or something), use your modified BitmapDrawable
, and test the heck out of it.
精彩评论