我想我遇到了一个令人讨厌的错误。问题是几乎是水平线
具有轻微的渐变并使用StrokeWidth = 1的Paint则不
绘制,例如:

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类:
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会绘制所有线条。

任何人都可以对此发表一些看法,还是我应该以Android Issue的形式提交此问题?

提前致谢!

最佳答案

通过将strokeWidth设置为0,您可以说android使用发际线宽度绘制(在任何设备上通常为1px)。如果将笔划宽度设置为1,则将缩放该值,即在ldpi设备上将为0.75 * 1 = 0.75px。因此,该行可能根本不会渲染。将ANTI_ALIAS_FLAG设置为绘画设备可能会有所帮助:

Paint pen = new Paint(Paint.ANTI_ALIAS_FLAG);

或者,您可以计算电流密度的笔划宽度:
pen.setStrokeWidth(1 / getResources().getDisplayMetrics().density);

07-26 01:14