绘制图形,看不到结果图像

发布于 2024-10-16 04:03:27 字数 995 浏览 3 评论 0原文

我正在尝试在 Android 中绘制图表。我希望图表缩放到当前屏幕大小,因此我没有在常量中显式设置它们,而是获取旨在包含图表的 LinearLayout 的大小。但是,存在一个问题,即无法在 Activity 的 onCreate() 中获取尺寸,因此我使用自定义 LinearLayout 并重写了 onSizeChanged()。我将其包含在布局中:

view class="com.nnevod.loggraph.graph$GraphDisplayLayout"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:background="@color/White"

我省略了尖括号。

在重写的 onSizeChanged() 中,读取视图的尺寸,然后按照许多图形示例中所述完成所有操作:创建位图,传递给图形绘制类,创建 ImageView ,设置好Bitmap,然后添加到自定义的LinearLayout中。

问题是,图表的图像不可见。但是,如果我尝试使用 HierarchyViewer 检查它,图像就会变得可见。如果我将代码从 onSizeChanged() 剪切粘贴到 Activity 的 onCreate()(不使用预设尺寸而不是测量尺寸),则所有内容都会显示良好。

所以,我的问题是我做错了什么?我假设我在自定义 onSizeChanged() 中使用了错误的上下文,或者无法从那里更新视图,并且我应该以某种方式将测量的尺寸传递给活动并在某些活动的方法中附加位图。 尽管在上下文部分,我尝试使用所有可用的上下文,包括使用活动的上下文,但无济于事。

如果需要,我将提供更多代码。

I am trying to draw a graph in Android. I want the graph to scale to current screen size, so instead of setting them explicitly in the constants, I get the size of an LinearLayout which is intended to contain the graph. However, there is a problem that it's not possible to get sizes in activity's onCreate(), so I use a custom LinearLayout with overridden onSizeChanged(). I include it into layout with:

view class="com.nnevod.loggraph.graph$GraphDisplayLayout"
    android:layout_height="fill_parent"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:background="@color/White"

I've omitted angular brackets.

In the overridden onSizeChanged(), dimensions of the view are read, then everything is done as described in many graphing examples: a Bitmap is created, passed to a graph-drawing class, an ImageView is created, is set with the Bitmap, and then added to the custom LinearLayout.

Problem is, the graph's image is not visible. However, if I try to inspect it with HierarchyViewer, the image becomes visible. If I cut-paste the code from onSizeChanged() to activity's onCreate(), sans using preset dimensions instead of measured ones, everything is displayed nicely.

So, my question is what I'm doing wrong? I've had assumptions that either I'm using a wrong context in custom onSizeChanged(), or it is not possible to update the view from there, and I should somehow pass the measured dimensions to the activity and do the attachment of the Bitmap in some of activity's methods.
Though on the context part, I've tried using all available contexts, including using activity's context, to no avail.

If needed, I'll provide more code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灰色世界里的红玫瑰 2024-10-23 04:03:27

我曾经遇到过类似的问题,我被告知层次结构查看器所做的是 requestLayout()。所以,我被告知,我可能忘记在某个地方调用它,但我不认为我忘记了。后来我发现类似的情况被报告为错误,这是一种解决方法,找到在某处调用 requestLayout() 的方法。

这很奇怪,因为实际上在某些条件下它是有效的(到处添加一些随机视图,或者更新绘图)。无论如何,最后我更喜欢创建一个自定义视图,直接绘制我需要的内容(并且由于绘制发生在 onMeasure 之后,因此制作自适应绘图没有问题)。

(有趣的是,这又是关于图表的!)

参考(1)(2)

I had a similar problem once, and I was told that what Hierarchy viewer does is a requestLayout(). So, I was told, I probably forgot to call it somewhere, but I don't think I did. Later I've found that a similar situation was reported as a bug, being a workaround finding the way to call requestLayout() somewhere.

It was strange because actually at some conditions the thing worked (adding some random view here and there, or updating the drawing). Anyway, at the end I preferred creating a custom view that simply directly drew what I needed (and as drawing happens after onMeasure, there's no problem in making an adaptive drawing).

(funny it's again about graphs!)

references (1) and (2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文