I try to draw a line with the width depending on the density.
The problem is that I can not make setStrokeWidth() method paint the width I want.
Here is the code:
...
private void init(){
paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
}
@Override
protected void onDraw(Canvas canvas){
canvas.drawLine(0, 0, getWidth(), 0, paint);
}
...
The above code displays a line with 2px width both on mdpi and hdpi screens.
How can I make paint to draw the line with the specified width?
Where does 1.5 scale factor come from?
You are drawing at the absolute top of the canvas, making only half the line visible, that is why it ends up 2 px wide. To see the entire line, move it down a bit.
To create a density dependent value you can use the following code: