首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >可绘制:如果主机视图更改其大小,则触发OnBoundsChange

可绘制:如果主机视图更改其大小,则触发OnBoundsChange
EN

Stack Overflow用户
提问于 2014-04-05 03:09:25
回答 1查看 1K关注 0票数 1

我正在使用svg-android库来处理应用程序中的SVG。长话短说,这个库从SVG生成一个(向量) PictureDrawable

不幸的是,Android无法使用硬件加速将drawPicture()转换成画布,因此要实现加速,我必须将PictureDrawable内部的图片转换为位图

问题是,如果在创建位图之后,可绘制主机的大小发生了变化,那么我必须重新调整到新的分辨率。

我已经超越了:

代码语言:javascript
复制
@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    if (Conf.LOG_ON) Log.d(TAG, "OnBoundsChange "+bounds);
    createBitmapWithNewBounds();
}

当我手动调用setBounds时,它可以工作,但是当可绘制的主机大小发生变化时,onBoundsChange()不会自动被调用(我也不知道是否应该调用)。

那么,是否有任何方法可以检测到它的主机大小发生了变化:

  • 我可以触发对setBounds()的手动调用吗?
  • 自动调用onBoundsChange()
EN

回答 1

Stack Overflow用户

发布于 2016-02-10 18:33:18

您尝试过重写protected boolean onStateChanged(int[] state)吗?

我冒昧地将源代码和附带的文档包括在这里:/** * Override this in your subclass to change appearance if you recognize the * specified state. * * @return Returns true if the state change has caused the appearance of * the Drawable to change (that is, it needs to be drawn), else false * if it looks the same and there is no need to redraw it since its * last state. */ protected boolean onStateChange(int[] state) { return false; }

根据可绘制的源,默认实现是指示状态没有更改,正如您可以从返回值中看到的那样。因为可绘图不是扩展视图,这一定是他们要求一个layout()的模拟。因此,我认为如果简单地重写onStateChanged()以返回true,则应该会看到对onBoundsChanged()的调用。显然,天真的return true不是正确的解决方案,因为它依赖于传入的int[] state,但这应该会使您走上正确的轨道。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22876096

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档