4 Replies - 419 Views - Last Post: 10 July 2017 - 11:04 PM

#1 radon5k  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 29-June 17

How to properly redraw a canvas without losing it from different class

Posted 10 July 2017 - 12:50 PM

Hello everyone,
Essentially what I have is a program that initially draws circles on the screen. I'm calling it CircleView and it extends View. Here is how the onDraw method looks:

    protected void onDraw(final Canvas canvas) {
        super.onDraw(canvas);
        dividerX = width / 11;
        dividerY = height / 10;
        radius = width / 60;
        starter = dividerX % width;
        for (int i = starter; i < width - starter; i = i + dividerX) {
            for (int j = starter; j < (width - starter); j = j + dividerX) {
                canvas.drawCircle(i, j, radius + (radius / 3), secondPaint);
                canvas.drawCircle(i, j, radius, drawPaint);
                //canvas.drawCircle(i, j, radius+1, secondPaint);
                point = new Point(i, j);
                square = new RectF(point.x - sizeOfRect, point.y - sizeOfRect, point.x + sizeOfRect, point.y + sizeOfRect);
                points.put(square, point);
            }
        }
    }


And in my Main Activity Layout I have this:
<com.masesk.leopardgecko.CircleView
    android:id="@+id/circleMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Anything from here works properly, however, in my program I need to keep the following canvas, and additionally add more drawings to it. I'm handling my input from user and data in my MainActivity.Java class and it currently gets reference to the draw view like this:
main = (CircleView)findViewById(R.id.circleMain) ;

Now main is my reference to my draw view.
Next, I'm trying to draw something on top of this canvas doing this:
                            Bitmap map = Bitmap.createBitmap(main.getWidth(), main.getHeight(), Bitmap.Config.ARGB_8888);
                            Log.d(TAG, Integer.toString(main.getWidth()));
                            Canvas can = new Canvas(map);
                            can.drawLine(firstPoint.x, firstPoint.y, secondPoint.x, secondPoint.y, paint);
                            main.draw(can);
                            main.invalidate();

But nothing happens. I have read the Android docs for custom drawing but it doesn't have an example of the following scenario. In addition, is this the convention? I have read online that I should handle all my drawing in my Circle View but I don't know how to achieve that either. Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: How to properly redraw a canvas without losing it from different class

#2 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 271
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: How to properly redraw a canvas without losing it from different class

Posted 10 July 2017 - 03:03 PM

The canvas that you're drawing to and the canvas that onDraw uses is not the same canvas

If you're drawing lines based on users touching the view, why not override onTouchEvent inside your view?

Check out my github in my signature.
There you will find both a simple line chart, a pie chart and a compare imageview.
They all make use of drawing on canvas and handling touch inputs.
All the code should be well commented, but start with CompareImageView or AndroidPieChart, the AndroidLineChart is a bit more complicated
as it has zoom and scrolling (somewhat crude implementations).
Was This Post Helpful? 0
  • +
  • -

#3 radon5k  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 29-June 17

Re: How to properly redraw a canvas without losing it from different class

Posted 10 July 2017 - 07:21 PM

View PostEndLessMind, on 10 July 2017 - 03:03 PM, said:

The canvas that you're drawing to and the canvas that onDraw uses is not the same canvas

If you're drawing lines based on users touching the view, why not override onTouchEvent inside your view?

Check out my github in my signature.
There you will find both a simple line chart, a pie chart and a compare imageview.
They all make use of drawing on canvas and handling touch inputs.
All the code should be well commented, but start with CompareImageView or AndroidPieChart, the AndroidLineChart is a bit more complicated
as it has zoom and scrolling (somewhat crude implementations).

In that case how do I access the canvas from inside of my view and inside of another method such as onTouchEvent? Because I tried to manually store it and that did not work.
Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 271
  • View blog
  • Posts: 1,250
  • Joined: 13-March 09

Re: How to properly redraw a canvas without losing it from different class

Posted 10 July 2017 - 10:13 PM

You don't.
When the onDraw is done, the canvas is disposed to free up memory.
If you where to look at my line chart code, you'll see that I storage the touch coordinates
and then call invalidate()

So basically:

@Override
public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_UP) {
       isTouching = false;
  } else if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
     if (event.getPointerCount() == 2) { //2 fingers
         isTouching = true;
         firstPoint = new Point(event.getX(0), event.getY(0));
         secPoint = new Point(event.getX(1), event.getY(1));
         invalidate();
     }
return true;
}

@Override
protected void onDraw(Canvas canvas) {
   if (isTouching) {
     //Draw line between firstPoint and secPoint
   }
} 


Was This Post Helpful? 1
  • +
  • -

#5 radon5k  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 29-June 17

Re: How to properly redraw a canvas without losing it from different class

Posted 10 July 2017 - 11:04 PM

Understood. Just wanted to understand the convention and the proper way to handle this. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1