记录下最近遇到的一个疑难杂症,TextView的onDrawnDraw绘制无效。当时也定位了好久,最后在stackoverflow上找到了答案。这里先附上stackoverflow的地址。
https://stackoverflow.com/questions/25501185/can-you-explain-the-behavior-of-textview-gravity-singleline-and-canvas
可以看到是因为同时设置了gravity=center和singleLine=true属性,导致TextView的mScrollX发生了偏移,所以onDrawnDraw绘制的内容就看不到了。
看下stackoverflow提供的解决方案,就是对canvas做偏移。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// translate the canvas before drawing onto it, fixing the position
canvas.translate(getScrollX(), 0);
canvas.drawLine(1, 1, 1, TEXT_VIEW_HEIGHT_PX - 1, borderPaint);
canvas.drawLine(1, 1, TEXT_VIEW_WIDTH_PX - 1, 1, borderPaint);
canvas.drawLine(TEXT_VIEW_WIDTH_PX - 1, 1, TEXT_VIEW_WIDTH_PX - 1,
TEXT_VIEW_HEIGHT_PX - 1, borderPaint);
canvas.drawLine(1, TEXT_VIEW_HEIGHT_PX - 1, TEXT_VIEW_WIDTH_PX - 1,
TEXT_VIEW_HEIGHT_PX - 1, borderPaint);
}
网友评论