Android:运行图的滚动条

发布于 2024-11-15 23:41:19 字数 1030 浏览 5 评论 0原文

我有一个自定义视图,它绘制一个运行图 - 某个幅度与时间的关系。现在我想为此实现一个自定义滚动条,以便我可以查看屏幕外的过去数据。数据可供我使用。我只需要用户选择 %offset 。

任何有关实施的帮助/建议都会很有帮助。

我的自定义视图的 onDraw 方法中的代码片段

public void onDraw(Canvas canvas) {

    int totalpts = data.size();
    scale = getWidth() / (float) maxpoints;
    List<Data> display = new ArrayList<Data>();
    int initial = 1;
    if (totalpts > maxpoints) {
        initial = totalpts - maxpoints;

            display = data.subList(initial, data.size() - 1);

    } else {

            display = data;

    }


    int size = display.size();
    Data start = null;
    float x1 = 0, x2 = 0, x = 0;

    if (size > 1) {
        x1 = getWidth();
        start = display.get(display.size() - 1);
        for (int i = display.size() - 1; i >= 0; i--) {
            Data stop = display.get(i);
            x = x1;
            x1 -= (stop.x * scale / 1000);

            canvas.drawLine(x, start.Y, x1, stop.Y, paint1);

            start = stop;
        }
    }
}

I've a custom view which draws a running graph - some magnitude versus time. Now I want to implement a custom scroll bar for this so that I can view past data which are offscreen. The data is available to me. I just need the %offset selection by the user.

Any help/suggestions on implementation would be v helpful.

Code Snippet from my Custom view's onDraw method

public void onDraw(Canvas canvas) {

    int totalpts = data.size();
    scale = getWidth() / (float) maxpoints;
    List<Data> display = new ArrayList<Data>();
    int initial = 1;
    if (totalpts > maxpoints) {
        initial = totalpts - maxpoints;

            display = data.subList(initial, data.size() - 1);

    } else {

            display = data;

    }


    int size = display.size();
    Data start = null;
    float x1 = 0, x2 = 0, x = 0;

    if (size > 1) {
        x1 = getWidth();
        start = display.get(display.size() - 1);
        for (int i = display.size() - 1; i >= 0; i--) {
            Data stop = display.get(i);
            x = x1;
            x1 -= (stop.x * scale / 1000);

            canvas.drawLine(x, start.Y, x1, stop.Y, paint1);

            start = stop;
        }
    }
}

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

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

发布评论

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

评论(1

狂之美人 2024-11-22 23:41:19

尝试将自定义控件放入 Horizo​​natalScrollView 中(假设您希望它滚动水平),否则使用 ScrollView),设置宽度控件为“WRAP_CONTENT”,HizontalScrollView为“FILL_PARENT”。如果没有看到自定义视图的代码,就很难知道是否需要对宽度计算进行一些修改才能使其正常工作。

Try putting your custom control inside a HorizonatalScrollView (assuming you want it to scroll horizontally), use ScrollView otherwise), setting the width of your control to "WRAP_CONTENT", and the HoizontalScrollView to "FILL_PARENT". Without seeing the code for your custom view, it's difficult to know whether you might need to do some tinkering with the width calculation to get this working.

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