Welcome back.
And this video, we're going to prepare our brush size selector and we're going to see how to use display
metrics because what we want to do is we want to be able to set the size of our brush.
And in order to achieve that, well, we need to use dimensions because this will look different on
different devices and we want to make sure that it's going to look more or less the same.
So inside of our drawing view, let's go ahead and create a new function, and I'm going to put it right
here just before the inner class.
So at the very bottom, I'm going to now go ahead and create a new function, and I'm going to make
it public.
Or I don't add the private key word because I want to be able to use it in the main activity.
So that's why I'm not going to add anything before it, no access modifier.
So I'm going to call this method set size for brush.
And what it's going to require is going to be the new size that I want to use, which will be of type
float because values on the screen are using floats.
So now I'm going to set the brush size, which is this value that we created here, which is a variable
for stroke brush size to draw on the canvas.
And we set that to zero and made a float out of it.
OK.
And now we're going to use that and overwrite it with this new size.
But unfortunately, I kind of just say new size because I want to take the screen dimension into consideration.
In order to achieve that, I can use the typed value class, which contains the applied dimension method.
And this requires a couple of values.
So an integer, a float and the metrics.
So the unit that we want to use, then the value as well as the metrics.
So if you want to know more about it, apply dimensions you can see here unit float display metrics.
So let's go over.
So let's go ahead and use the typed value dot complex unit, the IP as the.
First of all, you hear, and that will be our unit.
OK, so the unit that I want to use is in fact this aesthetic value complex, you know, Dippy and this
will then allow me to make sure that it's going to take their density pixels into consideration, then
it needs to have the value.
So the new value that we are going to pass to it, which will be our new size.
And then finally, it needs to know the metrics.
So here you see display metrics.
OK, so where do I get them from?
Well, I can get them from resources that display metrics.
Okay, so this will basically do the trick and will set the new brush size to be a value based on our
new size that we're passing, but adjusted to the metrics of our display, so adjusted to the size of
our display.
So it's going to be proportionate to the size of the screen because this line here has this certain
thickness, but on a bigger screen it will look different, right?
We want to adjust this thickness to the screen that is, the game is running up and then I need to set
that for my draw paint.
So my am draw paint stroke with now needs to be used while using our hand brush size that we just set
up.
So now inside of our drawing view, we are setting up the brush size and that is here.
OK, so we can get rid of this.
So inside of the set up drawing method, we are setting up the brush size and we don't need that anymore
because we are going to set that in our main activity.
So let's go over to the main activity and actually called this method that will allow us to set the
brush size based on the dimensions that we're using.
And therefore, I'm going to require the drawing view that I want to use.
So here I will need to get the drawing view.
Using private VAR drawing view is how I'm going to call it.
It will be of type drawing view, which is our class that we just created, and I'm going to make it
another bowl and set it to null at this point.
And then we can go ahead and set the drawing view to be found by its I.D., our Dart ID Dot drawing
view, which is this simple drawing view that we created because you see we are using our own view,
so we're not using a text view or button view or whatever.
We're creating our very own view, which is destroying view that we're created right and we're inheriting
from view or this glasses and everything from you.
So that's why we can use it in XML.
But in order to now access it in our main activity, we still need to use find view by ID or data binding.
What if worked alternatively as well.
And now we can set the size for the brush, let's say 20 to float.
And this will.
Give us the brush size of 20, which will look slightly differently.
So let's check it out, and you see it's a lot thicker because now it's taking the size of the screen
into consideration.
And also because they have the size here.
Now, for my drawing view, I don't need to set that in here.
So I commented this line out because otherwise I would override it, and that's not necessary.
All right.
So that's pretty much the preparation for the next video because what we want to do is you want to be
able to set the size for the brush.
And in the next video, we're going to be able to select the brush size from a custom dialogue that
we're going to create.
So let's check that out in the next video.