简体   繁体   English

及时在画布上移动点,在Android中加快速度(java)

[英]Moving point on canvas in time, speed in Android (java)

let's say I have a canvas with a point at coords [0,0] (in pixels probably) and I want to move the point to [0,200] in a specific timeframe (5s eg) at a constant/accelerating speed. 假设我有一个画布,其坐标为[0,0](可能以像素为单位),并且我想在特定时间范围内(例如5s)以恒定/加速的速度将其移至[0,200]。 Is there a function for that? 有功能吗?

I can't just Point.set(X,Y). 我不能只是Point.set(X,Y)。 I know I probably have to use a loop but I have never worked with a loop that can run every let's say 0.05s and redraw it. 我知道我可能必须使用一个循环,但是我从未使用过可以运行例如0.05s并重新绘制的循环。

Is there some delay function for the loop? 循环有一些延迟功能吗? I can easily calculated the coords where the point will be at time T but I have no idea how to prevent it finishing in an instant. 我可以轻松地计算出该点在时间T处的坐标,但是我不知道如何阻止它在瞬间完成。

I basically want to create an animation in canvas but pretty customizable considering not constant speed etc. 我基本上想在画布上创建动画,但是考虑到不是恒定的速度等,因此非常可定制。

Thank you for your responses. 谢谢你的回复。 If something is not clear, I'll gladly explain it. 如果不清楚,我会很乐意解释。

One possible solution is to have many images and set them in time for build the animation. 一种可能的解决方案是拥有许多图像并及时设置它们以构建动画。 There is an example of one of my apps. 有一个我的应用程序的例子。 You should use a Handler to set the image and one thread to control the animation. 您应该使用处理程序来设置图像,并使用一个线程来控制动画。 You can see the source code here: https://github.com/andresuarezz26/ocenglow/tree/master/AndroidApp/animationExample 您可以在此处查看源代码: https : //github.com/andresuarezz26/ocenglow/tree/master/AndroidApp/animationExample

/**
 * Exceution thread that control the animation
 * @author usuario
 *
 */
private class TickClass extends TimerTask {
    @Override
    public void run() {

        // TODO Auto-generated method stub
        handler.sendEmptyMessage(_index);
        if(isLeft){
            if (_index <= 30) {
                _index++;
                iteraciones++;

            } else {
                _index = 0;
                handler.sendEmptyMessage(_index);
                this.cancel();
                isMoving=false;

            }
        }else{
            if (_index <= 63) {
                _index++;
                iteraciones++;

            } else {
                _index = 32;
                handler.sendEmptyMessage(_index);
                this.cancel();
                isMoving=false;

            }

        }
    }
}

/**
 * This class handdle the animation´s images
 * @author usuario
 *
 */
private class MyHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        super.handleMessage(msg);

        try {
            Bitmap bmp = BitmapFactory.decodeStream(MainActivity.this
                    .getAssets().open("jelly"+ _index + ".png"));
            _imagView.setImageBitmap(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.v("Exception in Handler ", e.getMessage());
        }
    }
}

You can do this with a Canvas but I would recommend using a SurfaceView . 您可以使用Canvas进行此操作,但我建议您使用SurfaceView

With a Canvas you could use a Handler or a TimerTask to cause the view to Invalidate and redraw but that just seems messy. 使用Canvas您可以使用HandlerTimerTask使视图Invalidate并重新绘制,但这似乎很混乱。

With a SurfaceView you actually control the drawing thread. 使用SurfaceView您实际上可以控制绘图线程。 So if you want to draw every 1 second or every 1/8th of a second you can easily do so. 因此,如果您想每1秒钟或每1/8秒钟绘制一次,则可以轻松进行绘制。 The setup for a SurfaceView takes a few extra moments but for what you want to do i would suggest using it. SurfaceView的设置需要花费一些额外的时间,但是对于您要执行的操作,我建议您使用它。

Some tutorials 一些教程

Tutorial 1 教程1

Tutorial 2 教程2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM