简体   繁体   English

Android-线条图问题

[英]Android - Line drawing issue

I'm writing a small app that shows a score, and I'm having an issue when I draw the lines of the staves. 我正在编写一个显示乐谱的小应用程序,当我绘制五线谱时,我遇到了一个问题。 At same point, the lines are not drawn properly. 在同一点,线条绘制不正确。 Here's an image to ilustrate what I'm saying: 这是一张说明我在说什么的图片:

http://imageshack.us/photo/my-images/850/y1xk.png/ http://imageshack.us/photo/my-images/850/y1xk.png/

This is the code: 这是代码:

int x = canvas.getWidth();
int y = 0;
int x_margin = 40;
int y_margin = 60;

for (int i=1; i < 51; i++) {
    canvas.drawLine(x_margin, y + y_margin, x - x_margin, y + y_margin, mLinePaint);

    if (i % 5 == 0) {

        if ((i % 10 != 0) && (tracks == 2)) {
            canvas.drawLine(x_margin, y + y_margin, x_margin, y + y_margin + 40, mLinePaint);
            canvas.drawLine(x - x_margin, y + y_margin, x - x_margin, y + y_margin + 40, mLinePaint);

            y += 40;
        }

        else {
            y += 60;
        }

    }
    else {

        canvas.drawLine(x_margin, y + y_margin, x_margin, y + y_margin + 15, mLinePaint);
        canvas.drawLine(x - x_margin, y + y_margin, x - x_margin, y + y_margin + 15, mLinePaint);

        y += 15;
    }
}

Does anyone know why is this happening? 有人知道为什么会这样吗?

Thanks in advance. 提前致谢。

It's happening because there is a slight difference in the y at the beginning and the end of your line, make sure both y values are exactly the same. 发生这种情况是因为行的开头和结尾的y略有不同,请确保两个y值完全相同。

You may also try 您也可以尝试

youCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.ANTI_ALIAS_FLAG,Paint.DITHER_FLAG));

Have you checked to make sure it isn't the emulator. 您是否检查过以确保它不是模拟器? It may work fine if you run it on your phone. 如果您在手机上运行它,则可能会正常工作。

I guess this is related to the conversion between the mathematical coordinates and the "real" pixel coordinates. 我想这与数学坐标和“真实”像素坐标之间的转换有关。

I would recommend to draw the line twice or more to verify the error happens always in the same points. 我建议画线两次或更多次以验证错误总是在同一点发生。

I would also test it on a real device, the issue is maybe related to the conversion of screen coordinates between android and the PC screen... 我也会在真实设备上测试它,这个问题可能与android和PC屏幕之间的屏幕坐标转换有关...

Good luck, this bug is not funny 祝你好运,这个错误不好笑

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

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