Canvas.drawLines显示脱节段 [英] Canvas.drawLines displaying disjointed segments

查看:864
本文介绍了Canvas.drawLines显示脱节段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
  <一href="http://stackoverflow.com/questions/8287949/android-how-to-draw-a-smooth-line-following-your-finger">Android如何画一个流畅的线条下手指

我是新来的Andr​​oid和Java编程和已经玩了一下周围使用移动应用程序的开发。我最近创建的视图,它简单地保持绘制线条步骤与用户的手指动作。不过,我一直有与Canvas.drawLines方法有些麻烦。

I'm new to Android and java programming and have been playing around a bit with mobile app development. I recently created a View which simply keeps draws lines in step with the user's finger movements. However, I've been having some trouble with the Canvas.drawLines method.

而不是得到一组连续的所有点,我越来越虚线模式与段之间的休息间行的:

Instead of getting a continuous set of lines between all points I am getting more of a dashed pattern with breaks between segments:

下面是数据和简单的code重现该问题(点数据是从真实手指轻扫采取)的样本集:

Here is a sample set of data and simple code to reproduce the issue (point data was taken from a real finger swipe):

public class SomeView extends android.view.View 
{
    private float[] _data = { 292.36545f, 104.37576f, 285.3567f, 112.39249f, 274.34293f, 113.39456f, 254.3179f, 115.39874f, 248.3104f, 116.40082f, 228.28535f, 118.405f, 214.26784f, 119.407104f, 211.26408f, 119.407104f, 204.25533f, 120.40918f, 202.25282f, 120.40918f, 201.25157f, 121.411255f, 199.24907f, 124.41754f, 197.24657f, 125.41962f, 196.24532f, 130.43005f, 195.24406f, 139.44885f, 197.24657f, 144.45929f };
    private Paint _paint;

    public SomeView( Context c, AttributeSet attrs)
    {
        super( c, attrs );
        _paint = new Paint();
        _paint.setColor(Color.BLUE);
        _paint.setStrokeWidth(6);   
    }   

    @Override
    public void onDraw(Canvas canvas)
    {       
        canvas.drawLines( _data, _paint);
    }           
}

绘制出每个点覆盖它之上,我意识到,这个问题实际上是每一个点没有被连接,只有每两个点的线之后。代替连接点1至点2到点3等的,该方法被连接1〜2,然后为3〜4,如下所示:

After plotting out each point and overlaying it atop the line I realized that the problem is actually that each point is not being connected, only every two points. Instead of connecting point 1 to point 2 to point 3 and so on, the method is connecting 1 to 2 and then 3 to 4 as shown below:

我可以的几乎的得到了我想要再次绘制调用 drawLines 和提供偏移这段时间让其他对被拉到一起作为好了,但是这似乎效率不高,一般笨重的我,该行仍然不是完全平滑的(得稍微波涛汹涌的角落)。

I can almost get what I want by drawing calling drawLines again and providing an offset this time so that the other pairs are drawn together as well, but this seems inefficient and generally clunky to me, and the line still isn't completely smooth (gets slightly choppy on corners).

所以,我的问题是;什么我做错了,我怎么能画给点意见的一些数字的简单,流畅的线条?哎呀,忘了点,如果有更好的方法来跟踪用户的手指用一条线,我所有的耳朵。先谢谢了。

So, my question is; what am I doing wrong and how can I draw a simple, smooth line given some number of points? Heck, forget the points, if there is a better way to trace the user's finger with a line I am all ears. Thanks in advance.

推荐答案

你得到的是在<一个被指定的到底是什么href="http://developer.android.com/reference/android/graphics/Canvas.html"><$c$c>drawLines文档。

What you get is exactly what is specified in the drawLines documentation.

做你想做的是将构建一个的一种方法路径 从这些数据,并使用 drawPath ,而不是 drawLines 。是这样的:

One way of doing what you want would be to construct a Path from that data, and use drawPath rather than drawLines. Something like:

Path _path = new Path();
_path.moveTo(_data[0], _data[1]);
for (int i=2; i<_data.length; i+=2) {
  _path.lineTo(_data[i], _data[i+1]);
}

然后绘制它:

Then draw it with:

_canvas.drawPath(_path, _paint)

路径文档:

Path类封装化合物(多轮廓)组成的直线段,二次曲线和三次曲线的几何路径。它可以与canvas.drawPath(路径,油漆),或者填充或抚摸得出(根据油漆的风格

The Path class encapsulates compound (multiple contour) geometric paths consisting of straight line segments, quadratic curves, and cubic curves. It can be drawn with canvas.drawPath(path, paint), either filled or stroked (based on the paint's Style)

所以,你可能需要更改你是油漆的风格,以得到正确的效果。

So you might have to change you're paint's style to get the right effect.

这篇关于Canvas.drawLines显示脱节段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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