动画数字变化

发布于 2024-09-29 21:34:28 字数 299 浏览 7 评论 0原文

我想构建一个组件,能够以旧模拟汽车计数器的风格显示最多 5 位的整数,并以动画方式显示数字变化。

看起来像这样也许...

alt text

我尝试过搜索这种示例,但找不到到目前为止的任何事情。

您认为实现这一目标的最佳方法是什么?

我查看了 iPhone 闹钟时间选择器,据我所知,只有固定的背景,它们将数字推入或推出视图。但在这种情况下我如何放置数字并将它们引用到特定值?

tnx。

i want to build a component that will be able to show a integer number max 5 digits in the style of the old analog car counters and animate the digit change.

that looks something like this maybe...

alt text

i have tried searching for this kinda of examples but i couldn't find anything so far.

in your opinion what is the best approach to achieve this?

i looked at the iphone alarm time picker and as far as i can tell there is only a fixed background and they push the numbers in or out the view. but how do i place the digits in this case and reference them to a particular value?

tnx.

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

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

发布评论

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

评论(1

迟到的我 2024-10-06 21:34:28

您可以尝试创建自己的视图,扩展视图并重写 onDraw()。
在这里,您可以使用位图中的数字行,并根据您希望显示的数字编辑它们的位置。
设置新数字后不要忘记调用 invalidate() 来重绘视图。

我将粘贴一个包含项目开始的示例。
位图编号是一个垂直图像,编号为 1-9(以及 0&.)

class TickerView extends View { .. 


public void setDouble(double d) {
        value = d;
        invalidate();
    }
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int startx = 0;
        int starty = 0;
        DecimalFormat df = new DecimalFormat("#0.00");
        String str = df.format(value);
        String original = Double.toString(value);

        Bitmap nums = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.numbers);

        for (int i = 0; i < str.length(); i++) {
            int num = 0;
            try {
                num = Integer.parseInt(str.charAt(i) + "");
            } catch (Exception e) {
                num = 10;
            }
            int numbefore = 0;
            try {
                numbefore = Integer.parseInt(original.charAt(i -1) + "");
            } catch (Exception e) {
                numbefore = 0;
            }

            canvas.drawBitmap(nums, startx + (i * 40), (starty + 40)
                    - (num * 50) + (numbefore), paintY);

        }
        paintY.setStrokeWidth(10);
        canvas.drawLine(startx, starty+36, startx + (str.length() * 40), starty+36,
                paintY);
        canvas.drawLine(startx, starty + 90, startx + (str.length() * 40),
                starty + 90, paintY);
        invalidate();
    }


}

You can try to create your own view, extending view and overriding onDraw().
Here you can use rows of numbers in bitmaps and editing their position based on the number you wish to show.
Dont forget to call invalidate() after setting new numbers to redraw the view.

I will paste an example containing a start for your project.
The bitmap number is a vertical image with numbers from 1-9 (and 0&.)

Ex.

class TickerView extends View { .. 


public void setDouble(double d) {
        value = d;
        invalidate();
    }
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int startx = 0;
        int starty = 0;
        DecimalFormat df = new DecimalFormat("#0.00");
        String str = df.format(value);
        String original = Double.toString(value);

        Bitmap nums = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.numbers);

        for (int i = 0; i < str.length(); i++) {
            int num = 0;
            try {
                num = Integer.parseInt(str.charAt(i) + "");
            } catch (Exception e) {
                num = 10;
            }
            int numbefore = 0;
            try {
                numbefore = Integer.parseInt(original.charAt(i -1) + "");
            } catch (Exception e) {
                numbefore = 0;
            }

            canvas.drawBitmap(nums, startx + (i * 40), (starty + 40)
                    - (num * 50) + (numbefore), paintY);

        }
        paintY.setStrokeWidth(10);
        canvas.drawLine(startx, starty+36, startx + (str.length() * 40), starty+36,
                paintY);
        canvas.drawLine(startx, starty + 90, startx + (str.length() * 40),
                starty + 90, paintY);
        invalidate();
    }


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