What draws on what and what is the minimum?

enonod

Well-Known Member
Licensed User
Longtime User
I apologise in advance for this epistle and my questionable mental state. I sat a long time before pressing Submit.

I have read quite a few posts during this, my early learning, and have come to the conclusion that I have a concept difficulty and that possibly I am not alone. One normally learns by stringing together concepts and mental images drawn from the normal world. I am struck by the definite feeling that I have landed on a different planet.

I have trouble with the 'concept descriptions' involved here. It would be nice to have a diagramatic rendition of these concepts of what connects to what, which draws on which and which of them is necessary and which desirable (and for what reason it is desirable) to achieve the required result.

I believe my problem to be caused by the names that have been given to 'things'.
i.e. An activity cannot be touched but an Activity can. When I read of a Canvas and drawing my mind tells me that I would draw ON a canvas not WITH it and I would draw With a pen. I always believed when using a Delphi Canvas that I was indeed drawing ON it and WITH a pen.

Unless I have it wrong we are to believe that we draw ON a Bitmap WITH a Canvas (one would assume a BitmapDrawable - the other one would assume being for display of an image that exists already) .

The above (again if I am correct) is absurd to my English thinking mind.
I thus find it difficult to read the Help and understand by concept alone, I must look at other's code to find out the 'truth' and then twist my mental concept. If they have it wrong also, the code may work but have non required items, which I will not pick up.
I wonder if other's have this same problem. The Help etc. is written by somebody that 'knows' and to them the concept is obvious even if 'odd'.

Please tell me I am not alone on this planet. Often reading 'you could have done this instead' or 'you don't need that' suggests it is well populated.

So; in order to draw a line on the screen...
1. Can I simply add a Canvas (there I go; wrong concept) or a bitmap(Drawable or otherwise?) or both on top of each other like an overlay?
2. Do I HAVE to initialize all objects; because I have seen code that doesn't?
3. Do I need a Panel on which to place the above or can I do without if there are no other objects? The code I have seen uses one but is it necessary or because somebody didn't know either, or because Panels bring something else to the feast?
Sorry, sorry, for this long explanation of my plight. What have I obviously failed to read??
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code draws a line on the activity:
B4X:
   Dim canvas1 As Canvas
   canvas1.Initialize(Activity)
   canvas1.DrawLine(0, 0, 200dip, 200dip, Colors.Red, 10dip)
   Activity.Invalidate
Basic4android names in most cases are similar to the Java names. I agree that 'canvas' is a bit confusing. Canvas is actually a drawing object (like pen).
 
Upvote 0

DarkMann

Member
Licensed User
Longtime User
I think it helps to try and visualize it slightly differently.

I see the canvas.initialize(view) statement as creating a working canvas out of the view. You then have drawing tools that can draw on this created canvas (like in Erel's example above).

I guess what I am saying is that i think of it as:-

OBJECT - the view in question.
ABILITY - you can use it as a canvas to draw on.
TOOLKIT - the canvas methods that you can apply.

Anyway, hope that helps.

David
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
I thank you both for your replies. Please do not be offended, (I really am sincere) but I think my point has been made.
Canvas is actually a drawing object (like pen).
...draw on this created canvas
From Beginner's Guide... current view's background is copied to the new bitmap and the canvas is set
to draw on the new bitmap.

My other questions (at the bottom of the post) are genuine and I hope somebody can put me straight on those.
Perhaps a short explanation in plain English that distinguishes between these items. The I think I will be on my way.
Thank you
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Some complementary explanations:

To draw in Android, we need two objects:

1) we need a support, in Android a bitmap, this can be either:
a copy of the background bitmap of one of the following views:
- the Activity
- an ImageView
- a Panel
or
- a mutable bitmap (initialized with Bitmap1.InitializeMutable)
either of these bitmaps becomes the Canvas.Bitmap property.

2) we need tools to draw with, in Android the Canvas object.

Finaly, we draw with the canvas tools on the canvas bitmap, which is a copy of another bitmap.

Answers to your questions:
1. Can I simply add a Canvas (there I go; wrong concept) or a bitmap(Drawable or otherwise?) or both on top of each other like an overlay?
No, you can't, you need a support object, a view or a mutable bitmap.
You can use different views as overlays, show them or hide them.
You need one view (bitmap) and one canvas for each layer.
This is shown in the SimpleDrawFunctions program in the Beginner's Guide.

2. Do I HAVE to initialize all objects; because I have seen code that doesn't?
All views you add by code MUST be initialized.
All views you add in the Designer MUST NOT be initialized.
All objects (views and canvases) must be declared with Dim, if you want or need to acces them.
The canvases must be Initialized with the target view or bitmap.

3. Do I need a Panel on which to place the above or can I do without if there are no other objects?
If you have just one image you can draw on the Activity, no need for additional views.
The choice between a Panel or an ImageView depends on what you want to do. The Panel view supports more events than the ImageView.

I hope this clarifies a bit more the subject.
Sorry but English is not my mother tongue.

Best regards.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you Klaus for your clear explanation in clear English and for the time you have taken to write it.
we draw with the canvas tools on the canvas bitmap,
The above is now perfectly clarified and makes sense.
Everything else seems clear with one exception.
...which is a copy of another bitmap

This has slipped out through the other ear!

I have assumed from your description that I have an existing bitmap of a view, say a Panel, which is blank. It is attached to the Canvas and becomes a Property of it. Are you saying that this bitmap is a COPY of the Panel bitmap and the two will become different as soon as I draw but before I invalidate, and then they will become the same after I invalidate thereby creating a buffer system?? (Which would answer another question I posted.)
In which case it would 'appear' that I have in effect eventually drawn on the Panel bitmap, which now goes through the cycle of becoming the current bitmap again (but now not blank) ready for me to draw again.
Have I got it or have you pulled your hair out.
Thanks
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I don't think that the canvas bitmap is a real copy (duplicate) of the views bitmap, but that the canvas and the view reference the same bitmap. When we draw on that bitmap it is directly modified, but the screen is not automaticaly updated. Only when invalidating the view the corresponding area of the screen is updated.
That's how I see it, but I could be wrong.

Best regards.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are 100% correct.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you for that Klaus. Your view seems to be correct and confirmed by Erel's answer to my post on buffering, which between you has been made clear now.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…