有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我如何在安卓中放慢速度?

我的意思是我想慢慢地抽绳。 我写这段代码是为了用ondraw方法画线

.
.
.
.
caneta.setARGB(255, 255, 0,0);caneta.setStrokeWidth(10); 
canvas.drawLine(0, ys * 1/2, this.getWidth(), ys * 1/2, caneta);
.
.
.

我是怎么慢慢做到的


共 (2) 个答案

  1. # 1 楼答案

    在每个onDraw方法调用中,根据所需的速度递增地绘制一部分线。例如,如果你想要一个缓慢的绘图,在每次调用中增加5个像素的大小,直到达到整行

    private float linePercent = 0;
    protected void onDraw (Canvas canvas){
    
        float lineX = this.getWidth() * linePercent;
        canvas.drawLine(0, ys * 1/2, lineX, ys * 1/2, caneta);
        linePercent += 0.05f;
        if(linePercent >= 1){
            linePercent = 0;
        }
    }
    

    在后台线程中,在视图上安排一个invalidate

  2. # 2 楼答案

    这几乎就像gameloop的工作原理:

    -每X毫秒使画布失效一次(使用循环和Thread.sleep())

    -每次循环后增加X/Y坐标

    -再次处理onDraw()中的新坐标

    例如:

    private int x1, x2;
        private int y1, y2;
        private View v;
    
        public void start()
        {
            for (int i = 0; i <= 250; i++)
            {
                 v.invalidate();
    
                 x2 += 1;
    
                try
                {
                    Thread.sleep(50);
                }
                catch (InterruptedException e)
                {
                }
            }
        }
    

    在现有的视图类中,已经有了onDraw方法

     protected void onDraw(Canvas canvas)
            {
            //draw your line here using your X and Y member
                    canvas.drawLine(x1, y1, x2, y2, caneta);
            }