80,351
社区成员
发帖
与我相关
我的任务
分享
public class WriteText extends EditText {
private static final int MARGIN = 10;
private Rect mRect;
private Paint mPaint;
public WriteText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0xFF80ADB7);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect r = mRect;
Paint paint = mPaint;
Layout layout = getLayout();
if (!canvas.getClipBounds(r)) {
return;
}
float startX = r.left + MARGIN, stopX = r.right - MARGIN;
int count = layout.getLineCount();
int lineHeight = getLineHeight();
int height = getHeight() - getPaddingBottom() - getPaddingTop();
int n = height % lineHeight == 0 ? height / lineHeight : height / lineHeight + 1;
if (count < n) {
count = n;
}
for (int i = 1; i <= count; i++) {
/*int baseline = layout.getLineBaseline(i);
int bottom = layout.getLineBottom(i);
startY = stopY = (baseline + bottom + 10) / 2;
canvas.drawLine(startX, startY, stopX, stopY, paint);*/
int y = lineHeight * i;
canvas.drawLine(startX, y , stopX, y, paint);
}
}
}
public static class LinedEditText extends EditText {
private Rect mRect;
private Paint mPaint;
// we need this constructor for LayoutInflater
public LinedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(0x800000FF);
}
@Override
protected void onDraw(Canvas canvas) {
int count = getLineCount();
Rect r = mRect;
Paint paint = mPaint;
for (int i = 0; i < count; i++) {
int baseline = getLineBounds(i, r);
// 这里画的是一条水平线
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1,
paint);
}
super.onDraw(canvas);
}
}