用的drawLine = Paint.StrokeWidth 1 Android的问题 [英] drawLine problem with Paint.StrokeWidth = 1 in Android

查看:1519
本文介绍了用的drawLine = Paint.StrokeWidth 1 Android的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我打了一个讨厌的错误。问题在于,几乎水平线
有轻微的梯度,并用油漆与StrokeWidth = 1不
绘制,例如:

I think I hit a nasty bug. The problem is that nearly horizontal lines with a slight gradient and using a Paint with StrokeWidth = 1 are not plotted, for example:

public class MyControl extends View {

   public MyControl(Context context) {
           super(context);
           // TODO Auto-generated constructor stub
   }

   @Override
   protected void onDraw(Canvas canvas)
   {
           super.onDraw(canvas);

       Paint pen = new Paint();
       pen.setColor(Color.RED);
       pen.setStrokeWidth(1);
       pen.setStyle(Paint.Style.STROKE);

           canvas.drawLine(100, 100, 200, 90, pen); //not painted
           canvas.drawLine(100, 100, 200, 100, pen);
           canvas.drawLine(100, 100, 200, 110, pen); //not painted
           canvas.drawLine(100, 100, 200, 120, pen); //not painted
           canvas.drawLine(100, 100, 200, 130, pen);

           pen.Color = Color.MAGENTA;
           pen.setStrokeWidth(2);

           canvas.drawLine(100, 200, 200, 190, pen);
           canvas.drawLine(100, 200, 200, 200, pen);
           canvas.drawLine(100, 200, 200, 210, pen);
           canvas.drawLine(100, 200, 200, 220, pen);
           canvas.drawLine(100, 200, 200, 230, pen);
   }

}

和使用MyControl类是这样的:

And using MyControl class this way:

public class prova extends Activity {
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);

           MyControl ctrl = new MyControl(this);
           setContentView(ctrl);
   }

}

设置StrokeWidth为0或> 1的所有行绘制。

Setting StrokeWidth to 0 or > 1 all lines are plotted.

任何人都可以带来一些光在这,或者我应该提出这个问题作为一个 Android的问题

Can anyone bring some light on this or should I submit this issue as an Android Issue?

在此先感谢!

推荐答案

通过strokeWidth设置为0,你说与android的发丝宽度(这通常是一个1px的任何设备上)来绘制。如果您在LDPI设备设置描边宽度为1的值,然后缩放,即这将是0.75 * 1 = 0.75px。因此,该行可能不会呈现在所有。设置ANTI_ALIAS_FLAG到您的油漆设备可能会有所帮助:

By setting strokeWidth to 0 you say android to draw with a hairline width (which is usually one 1px on any device). If you set stroke width to 1 the value is then scaled, i.e. on ldpi devices it would be 0.75 * 1 = 0.75px. So the line might be not rendered at all. Setting ANTI_ALIAS_FLAG to your paint device might help:

Paint pen = new Paint(Paint.ANTI_ALIAS_FLAG);

另外,你可以计算出笔画宽度的电流密度:

Alternatively you can calculate the stroke width for current density:

pen.setStrokeWidth(1 / getResources().getDisplayMetrics().density);

这篇关于用的drawLine = Paint.StrokeWidth 1 Android的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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