【问题标题】:How to disable multi-touch in drawing using flutter如何使用颤振在绘图中禁用多点触控
【发布时间】:2019-02-02 15:29:26
【问题描述】:

这个问题纯粹是基于GestureDetectorflutter。

例如: 在应用程序中,实现了GestureDetector 类,因此默认支持多点触控,现在我们需要禁用多点触控,那么最好的方法是什么?否则在绘图应用程序中使用GestureDetector in flutter 会导致多点触控问题。

那么如何在手势检测器中禁用多点触控?

【问题讨论】:

标签: dart flutter


【解决方案1】:

我遇到了同样的问题,但我通过测量两点之间的距离来解决它。

The rule of how to measure the distance between two points

// Convert a rule to the code
double distanceBetweenTwoPoints(double x1,double y1 ,double x2, double y2){
double x = x1 - x2;
       x = x * x; 
double y = y1 - y2;
       y = y * y;

double result = x + y;
return sqrt(result);
}

首先,声明两个变量及其值

// These two variables are to save the previous points
var fingerPostionY = 0.0,fingerPostionX = 0.0;

然后在onPanUpdate方法里面,我取了两个点来计算它们之间的距离。之后我做了一个对比,如果距离大(比如50)那么手指很多,所以我忽略它,否则就只有一根手指在屏幕上。

onPanUpdate: (details) {
        if (fingerPostionY < 1.0){
          // assigen for the first time to compare
          fingerPostionY = details.globalPosition.dy;
          fingerPostionX = details.globalPosition.dx;
        }else{
          // they use a lot of fingers
          double distance = distanceBetweenTwoPoints(details.globalPosition.dx,details.globalPosition.dy,
                                                fingerPostionX,fingerPostionY);
          // the distance between two fingers must be above 50
          // to disable multi touch
          if(distance > 50)
            return;
          
          // update to use it in the next comparison
          fingerPostionY = details.globalPosition.dy;
          fingerPostionX = details.globalPosition.dx;
        } 

          // the code of drawing
        setState(() {
          RenderBox renderBox = context.findRenderObject();
          points.add(TouchPoints(
              points: renderBox.globalToLocal(details.globalPosition),
              paint: Paint()
                ..strokeCap = strokeType
                ..isAntiAlias = true
                ..color = activeColor.withOpacity(opacity)
                ..strokeWidth = strokeWidth));
        });
      },

重要提示:

  1. onPanEnd 方法中,你必须写下这一行,因为它 表示手指现在已经竖起来了

    fingerPostionY = 0.0;

  2. 绘图代码中仍有一些性能问题尚未解决


编辑:

我使用 path 提高了性能。

你可以在 GitHub 上看到我的代码: free painting on flutter

【讨论】:

    猜你喜欢
    • 2019-01-13
    • 1970-01-01
    • 2010-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    相关资源
    最近更新 更多