简体   繁体   中英

how to Draw points or line in the spaces before text in every line - in Java

I need some help to make this idea work, what I want is to draw pints or line before every space in every line in edittext onDraw method. for example i want the text or code or any content to have look like this:

_____for (int i=0; i< count; i++) {
___________some code here
____________}

or look like this:

.........for (int i=0; i< count; i++) {
............some code here
..............}

I tested and tried to do it, and it works but I have a small problem, that is when the cursor position or text is in the center of line, I get no line drawing or the line is to short then the space between start of line and the start of text.

picture of my problem:

[:[enter image description here][1]][1] [1]: https.//i.stack.imgur.com/ly5du.png

the Code I'm using is:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class customEditText extends androidx.appcompat.widget.AppCompatEditText {
    private Paint mPaint = new Paint();
    private int position;

    public customEditText(Context context) {
        super(context);
        initPaint();
    }

    public customEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public customEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaint();
    }

    private void initPaint() {
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(0x80000000);
    }
    @Override
    protected void onSelectionChanged(int selStart, int selEnd) {
        position = selStart;
    }

    @Override protected void onDraw(Canvas canvas) {

        int left = getLeft();
        int paddingLeft = getPaddingLeft();

        for (int i = 0; i < getLineCount(); i++) {


            int ii = i;

            try {
                Matcher m = Pattern.compile("^\\s+|\n\\s+").matcher(getText().toString().split("\n")[ii]);
                if (m.find()) {
                    int baseline = getLayout().getLineBaseline(ii);

                    float x = getLayout().getPrimaryHorizontal(m.end());

                    canvas.drawLine(left + paddingLeft, baseline, x, baseline, mPaint);
                }
            } catch (Exception r){
                r.printStackTrace();
            }

            }

        super.onDraw(canvas);
    }
}

The method getPrimaryHorizontal() gives you

the location where a new character would be inserted in the paragraph's primary direction

This means you'll get a pixel value, to be measured from the View 's left border.

If you type just one space, x may be smaller than left + paddingLeft . So the line will in fact be drawn from startY to startX . If you add more spaces, the difference between x and left + paddingLeft will change, causing the line first to vanish and then to reappear, but now really drawn from startX to startY .

Since getLeft() only gives you the distance of the View 's left border to its parent ViewGroup 's left border, you don't need to add the value of to the starting point of the line.

But you may want to keep getPaddingLeft() . If you do, you should add it to both the startX value and the startY value:

 canvas.drawLine(paddingLeft, baseline, paddingLeft + x, baseline, mPaint);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM