开发者

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...

  1. Create class ShadowBitmapDrawable which extends BitmapDrawable
  2. Within this class, somehow alter the BitmapDrawable's Paint object with Paint's setShadowLayer()开发者_StackOverflow社区 method.
  3. In my custom ImageButton class, call setImageDrawable(Drawable d), and pass it my ShadowBitmapDrawable 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

P.S. I have a bad feeling I already know the answer I'm going to get, and I'm not going to like it. Thought I'd post the question anyways and hope for the best.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜