Android自定义视图';滚动时填充
我编写了一个自定义文本视图(实际上是什么类型的视图并不重要),它扩展自Android自定义视图';滚动时填充,android,view,padding,Android,View,Padding,我编写了一个自定义文本视图(实际上是什么类型的视图并不重要),它扩展自视图。我在XML文档中向该视图添加了填充,并读取这些填充,然后将其传递给super,以应用这些填充。在我的onDraw和onMeasure中,我也考虑了这些填充,一切正常 除了,如果我通过方法view.scrollTo()滚动此视图,填充将不再有效。所谓“不再有效”,是指在画布上绘制的内容不考虑视图的填充,如下图所示: 我想知道这方面是否有解决办法?(PS:不要告诉我使用TextView而不是自己制作。我之所以这样做是出于
视图
。我在XML文档中向该视图添加了填充,并读取这些填充,然后将其传递给super
,以应用这些填充。在我的onDraw
和onMeasure
中,我也考虑了这些填充,一切正常
除了,如果我通过方法view.scrollTo()
滚动此视图,填充将不再有效。所谓“不再有效”,是指在画布上绘制的内容不考虑视图的填充,如下图所示:
我想知道这方面是否有解决办法?(PS:不要告诉我使用TextView
而不是自己制作。我之所以这样做是出于某种原因,我想解决的唯一问题是填充内容,而不是一些出色的替代品,谢谢!)
编辑:
我的xml
在我看来:
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
......
setMeasuredDimension(getPaddingLeft()+contentWidth+getPaddingRight()
,getPaddingTop()+contentHeight+getPaddingBottom());
//whereas the contentWidth and contentHeight is determined by the widthMeasureSpec
// and heightMeasureSpec and some certain logic inside the view.
// I don't think this will cause the view's content to exceed the conten area
}
在我的脑海里:
public void onDraw(Canvas canvas){
Path path = new Path();
for(String eachLine: text){
path.moveTo(startX,startY);
path.lineTo(endX,endY);
canvas.drawTextOnPath(eachLine,path,0,0,painter);
...
}
....
//the startX,startY,endX, endY has already took the padding into consideration.
//*NOTE: The only solution that I can think of is that I control this
//drawing logic according to the padding. But this approach still won't fix the
//problem, for example: what if I scroll half line height? How do I draw the
//half of the line? So there must be some other approach that I don't know.
}
发布您的xmlcode@SathishKumarJ
如果这很重要。发布完整的xmlcode@SathishKumarJ我已经添加了一些代码,如果还不够的话,我将添加更多细节code@SathishKumarJ
如果这很重要。发布完整的xmlcode@SathishKumarJ我已经添加了一些代码,如果还不够,我将添加更多细节。
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
......
setMeasuredDimension(getPaddingLeft()+contentWidth+getPaddingRight()
,getPaddingTop()+contentHeight+getPaddingBottom());
//whereas the contentWidth and contentHeight is determined by the widthMeasureSpec
// and heightMeasureSpec and some certain logic inside the view.
// I don't think this will cause the view's content to exceed the conten area
}
public void onDraw(Canvas canvas){
Path path = new Path();
for(String eachLine: text){
path.moveTo(startX,startY);
path.lineTo(endX,endY);
canvas.drawTextOnPath(eachLine,path,0,0,painter);
...
}
....
//the startX,startY,endX, endY has already took the padding into consideration.
//*NOTE: The only solution that I can think of is that I control this
//drawing logic according to the padding. But this approach still won't fix the
//problem, for example: what if I scroll half line height? How do I draw the
//half of the line? So there must be some other approach that I don't know.
}