How to get current canvas?

35,809

Solution 1

You can't. The canvas is managed by the system and is passed to your onDraw(). I don't understand why you'd need it outside of there. Just redeclare setPoints like this

public void setPoints(Canvas canvas, Float xP, Float Yp)

You can keep a cache of the previous drawings (or store the previous points)

Solution 2

Try declaring canvas2 as a public variable in the DrawView class.

Solution 3

You draw your circles in onDraw(). That's the way View is supposed to work (technically it's actually in the draw() method but we'll overlook that). In setPoints(), set the points of the circle in variables within the class scope, call invalidate(), then draw the circle like that in onDraw(). If you follow this method, you're following the class flow that the view was designed for.

Share:
35,809
Leo
Author by

Leo

Updated on June 17, 2020

Comments

  • Leo
    Leo almost 4 years

    I have DrawView. If I touch this view it draws small circles. I wont to draw circles but not to touch view - with help function "setPoints". What I do:

    package com.samples;
    import ...
    
    public class DrawView extends View {
        ArrayList<Point> points = new ArrayList<Point>();
    
        Paint paint = new Paint();
    
        private int pSize = 5;
        private int pColor = Color.BLACK;
    
        public DrawView(Context context, AttributeSet attrs) {
    
            super(context, attrs);
    
            setFocusable(true);
            setFocusableInTouchMode(true);
    
            this.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    v.setOnTouchListener(this);
                        Point point = new Point();
                        point.x = event.getX();
                        point.y = event.getY();
                        points.add(point); 
                        invalidate();
                    }
                    return true;
                }
            });
            requestFocus();
        }
    
        @Override
        public void onDraw(Canvas canvas) { 
            for (Point point : points) {
                canvas.drawCircle(point.x, point.y, pSize, paint);
            }
        }
    
        public void setPoints(Float xP, Float yP)
        {
            Point point = new Point();
            point.x = xP;
            point.y = yP;
            points.add(point);
            postInvalidate();
        }
    }
    
    class Point {
        float x, y;
    
        @Override
        public String toString() {
            return x + ", " + y;
        }
    }
    

    Please tell me, how get canvas out setPoints function?

    Update: Wow, it's really interesting problem. My DrawView contains in HorizontalScrollView. Because if I set in this DrawView right coordinates, no one knows where are drawable circles.

  • Leo
    Leo about 12 years
    hmm.. public void setPoints(Float xP, Float Yp) { canvas2.drawCircle(xP, yP, 5, paint); invalidate(); } so?
  • Leo
    Leo about 12 years
    Points are not pproblem, I keep it. About "Canvas canvas": What can i doing if never call onDraw?
  • Leo
    Leo about 12 years
    Problem is only in Canvas call.
  • DeeV
    DeeV about 12 years
    No. canvas2.drawCircle(xP, yP, 5, paint); goes in onDraw(). You save the values for xP, yP then call invalidate() in setPoints()
  • Raffaele
    Raffaele about 12 years
    You seem not to know how Android draw Views. When the system detects that a widget needs to (re)paint, it knocks on your widget's door and it responds by onDraw(Canvas). So you are guaranteed that this method will be called each and every time is needed. One way to trigger the repaint is by calling invalidate() (or postInvalidate). So you never have to call onDraw yourself, but be sure that Android will call it when needed. You need to supply the callback. Also, please specify what you mean with "I want to draw circles but not to touch view"
  • Leo
    Leo about 12 years
    Can you give me link or keyword?
  • Raffaele
    Raffaele about 12 years
    The documentation is sparse. 2D drawing guide, View API