绘制以下用户触摸多个路径 [英] Draw multiple paths following to user touch

查看:177
本文介绍了绘制以下用户触摸多个路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我用下面的触摸用户手指清明上河图路径试验。我第一次提到本教程它奋力但有一个明显的问题,它不连接触摸点。于是,我找到<一href=\"http://stackoverflow.com/questions/8287949/android-how-to-draw-a-smooth-line-following-your-finger\">this直接提到了同样的教程和解决问题。我用处理三次样条函数的部分该链接johncarl的解决方案,并发现它非常有用。不过,我注意到,当你从屏幕上移开手指(ACTION_UP),然后再次回落(ACTION_DOWN)的地方,它的最后一个点连接到这个新的点,改变路径位。

So, I'm experimenting with "painting" paths that follow the users finger on touch. I first referred to this tutorial which worked yet had an obvious problem that it wouldn't connect the points of touch. So, I found this question that referred directly to the same tutorial and problem. I used johncarl's solution on this link on the part dealing with cubic splines and found it very useful. However, I noticed that when you remove your finger from the screen (ACTION_UP) and then place it back down again (ACTION_DOWN), it connects the last point to this new point and changes the path a bit.

现在,我的问题是,你会怎么能够当用户删除有手指,并把它背在屏幕上开始一个新的路径?我的意思是,新的点将无法连接到最后一个点。

Now, my question is, how would you be able to start a new path once the user removes there finger and places it back on the screen? What I mean is that the new point will not connect to the last point.

我已经尝试了一些东西,我最新的尝试是这样的:

I've tried a few things, my newest attempt being this:

    else if (event.getAction() == MotionEvent.ACTION_UP){
        for (int i = 0; i <= points.size() - 1; i++){
            points.remove(i);
        }
    } 

以上code是一种添加到在onTouch()方法中的if语句。不过,这似乎并没有做太多的。如果你能提供任何帮助这将是极大的AP preciated,谢谢!

The above code was an add on to the if statement in the onTouch() method. Though, this doesn't seem to do much at all. If you could provide any help it would be greatly appreciated, thanks!

推荐答案

一段时间的反复试验后,我发现了如何开始一个新的道路。其实,我可以使用相同的路径。在code我放在问题将只是还ArrayList中删除所有的值(我认为将是第一步,开始一个新的路径)。虽然,出于某种原因,它甚至没有这样做。我发现不工作,但是,仅仅使用clear()方法,像这样:

After awhile of trial and error, I found out how to start a "new" path. In fact, I could just use the same path. The code I placed in the question would just have removed all the values in the arraylist (what I thought would be the first step to start a new path). Though, for some reason, it was not even doing that. What I found does work, however, is just using the clear() method, like so:

points.clear();

不过,我发现,这并没有解决我的问题。它不仅能消除pviously绘制路径$ P $。我需要做的是使用moveTo()方法,这将允许在路径中断创建一个看起来好像有多个路径。所以,每次我删除了我的手指从屏幕(ACTION_UP)的时候,我加入了'点'ArrayList的大小作为另一个ArrayList的一个值:

Though, as I found out, this does not solve my problem. It only removes the previously drawn path. What I needed to do was use the moveTo() method which will allow breaks in the path creating a look as if there were multiple paths. So, every time I removed my finger from the screen (ACTION_UP), I added the size of the 'points' arraylist as a value in another arraylist:

startValues.add(points.size());

这会给我的指标值在那里我会调用moveTo()方法。现在,在的onDraw()方法中添加以下code(看看这个问题联系我要求看一下所有我所用的code):

This would give me the index values where I would call the moveTo() method. Now, in the onDraw() method I added the following code (look at links in the question I asked to see all the code I was using):

    boolean first = true;
    for(int i = 0; i < points.size(); i++){
        Point point = points.get(i);

        for (int index = 0; index <= startValues.size() - 1; index++){
            if (i == startValues.get(index)){
                lineChange = true;
                endNumber = (startValues.get(index) - 1);
                break;
            }
        }

        if(first || lineChange){
            first = false;
            lineChange = false;
            path.moveTo(point.x, point.y);
        }else{
            Point prev = points.get(i - 1);
            path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y);
        }

    }

    canvas.drawPath(path, paint);

这途中会有在第一个点之前的路径和从那里我举起从屏幕上我的手指每一点休息,直到我把它回来了。这解决了最初的问题,我当我问这个问题,但它导致了一些其他的。例如,一个线在更换了手指的改变在屏幕上的开始和结束点。我会问,在另外一个问题,如果我不能找到解决办法我自己。我希望这是一个面临类似的问题有用的人!

This way there will be breaks in the path before the first point and on every point from where I lifted up my finger from the screen until I placed it back down again. This solves the initial problem I had when I asked this question but it had led to some other ones. Such as, the beginning and end points of a line changing upon replacement of my finger onto the screen. I will ask about that in another question if I can't find the solution my self. I hope this is useful for anyone faced with a similar problem!

这篇关于绘制以下用户触摸多个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆