1

I wrote a custom text view(it doesn't matter what kind of view actually) that extends from View. I added padding to this view in my XML document and read these padding and pass it to super to apply these padding. And in my onDraw and onMeasure I also took these padding into consideration and everything works just fine.

EXCEPT, if I scroll this view via the method View.scrollTo(), the padding no longer works. By saying no longer works, I mean the content that drawn on canvas doesn't respect the view's padding, like the images shown below: enter image description here

enter image description here

I want to know if there's any workaround on this?(PS: don't tell me to use TextView instead of making my own. I'm doing this for some reason, the only problem I want to solve here is just the padding stuff, not some brilliant alternatives, thanks!)

EDIT: my xml

<com.example.custom.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_area"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/text_bg"
    android:padding="10dp"/>

and in my constructor:

public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // by calling super, the super class will take care of the paddings
        // internal, and after this, I just have to get the paddings by
        // getPaddingTop(),getPaddingLeft(),getPeddingRight(),getPaddingBottom() etc.
        }

and in my onMeasure:

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
}

and in my onDraw:

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.
}
gone
  • 813
  • 7
  • 19

0 Answers0