2

I have next code in my custom view that extends EditText:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

                int count = getLineCount();
                Canvas cvs= new Canvas();
                Drawable dr = this.getBackground();
                Rect r = mRect;
                Paint paint = mPaint;
                mTextPaint.setTextSize(this.getTextSize());
                Paint PaintText = mTextPaint;

                for (int i = 0; i < count; i++) {
                    int baseline = getLineBounds(i, r);
                    cvs.drawText(Integer.toString(i+1), r.left, baseline + 1, PaintText);
                    cvs.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint); 
                }
                cvs.drawLine(PaintText.measureText(Integer.toString(count)), this.getTop(), PaintText.measureText(Integer.toString(count)), this.getBottom(), paint);
                dr.setBounds(0, 0, this.getRight(), this.getBottom());
                dr.draw(cvs);
                this.setBackgroundDrawable(dr);
            }

Why is there nothing on backgroind of this view?

3 Answers 3

5
protected void onDraw(Canvas canvas) {
    Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
    int count = getLineCount();
    Canvas cvs= new Canvas(bitmap);
    cvs.drawColor(Color.rgb(245, 245, 245));
    Rect r = mRect;
    Paint paint = mPaint;
    mTextPaint.setTextSize(this.getTextSize());
    Paint PaintText = mTextPaint;

    for (int i = 0; i < count; i++) {
        int baseline = getLineBounds(i, r);
        cvs.drawText(Integer.toString(i+1), 2, baseline + 1, PaintText);
        cvs.drawLine(2, baseline + 1, r.right, baseline + 1, paint); 
    }

    cvs.drawLine(PaintText.measureText(Integer.toString(count))+3, this.getTop(), PaintText.measureText(Integer.toString(count))+3, this.getBottom(), paint);

    this.setBackgroundDrawable(new BitmapDrawable(getContext().getResources(), bitmap));
    this.setPadding((int) (PaintText.measureText(Integer.toString(count))+7),3,3,3); 
    super.onDraw(canvas);
}
Sign up to request clarification or add additional context in comments.

Comments

3

I think you're trying to do something that can be done easier. However, your code doesn't work because:

  1. You don't set a Bitmap for your Canvas object.
  2. I think that you want to move content of the Canvas to the Drawable but in fact your code performs an opposite action. Actually, you draw the Drawable on the Canvas object. Try this:
    Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(),
        Bitmap.Config.ARGB_8888);
    Canvas cvs = new Canvas(bitmap);
    // Your drawing stuff without dr.setBounds() and dr.draw()
    this.setBackgroundDrawable(
        new BitmapDrawable(getContext().getResources(), bitmap));

8 Comments

Thank you so much, but now I have 2 problems: 1. Its black color of this bitmap. 2. android:padding acts on this background ( . I just want to set left padding for text in this view - i.imgur.com/1doN8.png
Use Canvas.drawColor() to change color of Bitmap. Can you please describe your goal in detail?
I doing custom edittext with linenumbering, but the problem is that user's text is superimposed on top draw the text, as you can see on the picture.
So as far as I understand, padding of the EditText changes when new background drawable is set.
I just want to set padding only for text.
|
1

You do not need to create a new Canvas object. onDraw creates one for you and sets it in its own bitmap. Just use the name of the canvas specified in the parameters (in this case, canvas).

onDraw is called everytime your view is created or invalidate() is called. And in your onDraw() method, you are creating a NEW canvas object. If you are using this code for anything graphically intense (like a game) then you are leaking memory. Even if you only draw your view once, this is not the best way to do so. Use the canvas provided in the onDraw() parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.