Android的画线跟随你的手指 [英] Android drawing a line to follow your finger

查看:837
本文介绍了Android的画线跟随你的手指的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是绘制,将遵循我的手指一条线。我创建 自定义视图,和我有一个的onTouchEvent()的作品。

What I want to do is to draw a line that will follow my finger. I've created a custom view, and I have an onTouchEvent() that works.

我可以得出在的OnDraw()方法静态行没有太多的麻烦。

I can draw a static line in the onDraw() method without much trouble.

我真的不知道如何让线画为我的手指,虽然移动

I'm not really sure how to get the line to draw as my finger moves though.

  public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            Log.e(TAG, " - DOWN -");
            Log.e(TAG, " getX: " + event.getX());
            break;
        }
        case MotionEvent.ACTION_UP: {
            Log.e(TAG, " - UP -");
            Log.e(TAG, " getX: " + event.getX());
            break;
        }
        }
        return true;
    }

任谁已经做了一段时间可以给提示你们?

Any hints you guys who have been doing it a while can give?

我是否需要对的onTouchEvent()设置坐标和不断视图无效 这样的小线段画?

Do I need to set coordinates on the onTouchEvent() and constantly invalidate the view so the little line segments draw?

在最后我只是希望能够在屏幕上用我的手指基本上涂鸦的 这个实验。

In the end I just want to be able to basically doodle on the screen using my finger for this experiment.

推荐答案

你只是跟踪向上和向下的事件。跟踪ACTION_MOVE事件了。要注意的是,将连续跟踪,即使人的手指没有明显移动。您的code应该是这样的:

You're only tracking the up and down events. Track the ACTION_MOVE event too. Beware that it will track continuously, even if the person's finger isn't apparently moving. Your code should go something like this:

ACTION_DOWN:储存位置

ACTION_DOWN: Store position.

ACTION_MOVE:如果位置与存储的位置不同,那么得出的存储位置为当前位置的线,并更新存储位置为当前

ACTION_MOVE: If position is different from stored position then draw a line from stored position to current position, and update stored position to current.

ACTION_UP:停止

ACTION_UP: Stop.

在ACTION_MOVE位,它可能是一个好主意,检查如果位置为至少2或3个像素距离存储位置。如果你要存储所有的积点,所以你可以做一些与数据后,那么也许增加了10个像素,这样你就不会结束,几百点的简单的一行。

In the ACTION_MOVE bit, it might be a good idea to check if the position is at least 2 or 3 pixels away from the stored position. If you're going to store all of the plot points, so you can do something with the data later, then maybe increase that to 10 pixels so you don't end up with hundreds of points for a simple line.

这篇关于Android的画线跟随你的手指的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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