Issue
After creating the drawing below I've noticed that for some reason, some of the black lines appear faintly whereas some of them are clearly visible. What code can be used to ensure that the width of the black lines in-between the grey boxes is exactly 1dp and the width of the red rectangles is exactly 5dp?
public class RectangleTextView extends View {
private final Paint mBackPaint = new Paint();
private final Paint mRedPaint = new Paint();
private int mSideRectWidth = 10;
public RectangleTextView(Context context, AttributeSet attrs) {
super(context, attrs);
mBackPaint.setColor(Color.BLACK);
mRedPaint.setColor(Color.RED);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getWidth() == 0)
return;
//draw grey boxes
setBackgroundColor(Color.parseColor("#808080"));
int boxWidth = getWidth() / 7;
//draw black line
for (int i = 0; i < 7; i++) {
canvas.drawLine(mSideRectWidth + boxWidth * i, 0, mSideRectWidth + boxWidth * i, getHeight(), mBackPaint);
}
//draw left end rectangle
canvas.drawRect(0, 0, mSideRectWidth, getHeight(), mRedPaint);
//draw right end rectangle
canvas.drawRect(getWidth() - mSideRectWidth, 0, getWidth(), getHeight(), mRedPaint);
}
}
Solution
Is that the entire way in which you initialize the Paint
object? I don't know the default values used by the default constructor. I usually set them explicitly: take off the ANTI_ALIAS_FLAG
, set the style to Paint.Style.STROKE
and set the desired stroke width
. Some of the values that you need might be the default ones, I just don't know.
When dealing with thin lines, turning off the ANTI_ALIAS_FLAG
might be quite important.
Answered By - N.T.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.