作为练习,我尝试扩展 ImageButton
以添加一些我认为有用的功能。其中一个尤其是卓尔阴影。我已经用这个撞到了众所周知的墙。
在我看来,扩展 BitmapDrawable 的类是必要的。此类包含一个 Paint
对象,用于将位图绘制到屏幕上。如果我有权访问这个 Paint
对象...我所要做的就是调用它的 setShadowLayer()
方法...但是,唉,它是 私人的。有一个 public
方法 getPaint()
将返回绘制对象,但对其进行任何修改都是无用的,除非有相应的 setPaint()< /代码> 方法。 (没有。)
目前,我的思考过程如下所示...
- 创建扩展 BitmapDrawable 的类
ShadowBitmapDrawable
- 在此类中,以某种方式更改
BitmapDrawable
的 Paint
对象与 Paint
的 setShadowLayer()
方法。
- 在我的自定义
ImageButton
类中,调用 setImageDrawable(Drawable d)
,并将其传递给我的 ShadowBitmapDrawable
对象。
第 2 步是路障。我可以做什么来更改 BitmapDrawable
的 Paint
对象?请注意,我添加我的思维过程只是为了表明我在这个问题中所处的位置。我愿意接受其他建议。
以下是一些参考:
PS我有一种不好的预感,我已经知道我会得到的答案,但我不会喜欢它。我想无论如何都会发布这个问题并希望得到最好的结果。
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
which extends BitmapDrawable
- Within this class, somehow alter the
BitmapDrawable
's Paint
object with Paint
's setShadowLayer()
method.
- 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:
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.
发布评论
评论(1)
将 BitmapDrawable 克隆到您的项目中,将其重构到您自己的包中,使数据成员受保护(或提供受保护的 setter 或其他东西),使用修改后的
BitmapDrawable
,并测试它的效果。Clone
BitmapDrawable
into your project, refactor it into your own package, make the data memberprotected
(or provide aprotected
setter or something), use your modifiedBitmapDrawable
, and test the heck out of it.