How To Draw Circle In Xml In Android Studio
Android Shape Drawables Tutorial
Have yous ever wanted to reduce your Android application'south size or get in look more interesting? If yeah, so you should try out ShapeDrawables.
First, we will become over the advantages and disadvantages of the ShapeDrawables. And so we will create some Drawables that could be used in your app and lastly for the thousand finale we will try to replicate a gradient as can be seen in the Spotify app/website.
Why should you use ShapeDrawables?
When you desire to employ PNG or JPEG images in your awarding, you accept to provide multiple copies of the aforementioned epitome for unlike screen densities. That, of course, clutters your app with copies of the same epitome. Yes, sometimes that is the path we have to cull considering we can't use Drawables for every single case, simply we tin dramatically reduce our application's size if we can use Drawables instead. ShapeDrawables are a series of commands that tell how to draw something on the screen. That is why they tin can be resized and stretched every bit much as we want, without losing whatever quality. We tin can recolor and dispense them even when the app is running and employ the same ShapeDrawable multiple times in our app. Since ShapeDrawables are a bracket of the Drawable abstruse course, we tin can use them in methods where a Drawable is expected. Click for the documentation of the ShapeDrawable.
Are in that location any disadvantages?
Of course, just like I have mentioned before we can't use them in every case. I have said before that ShapeDrawable form is a subclass of the Drawable abstract form. There are other subclasses likewise and every ane of them has its own utilise case. You can click here to check other Drawable types and figure out which one is right for your example. Another event is that they took a bit longer to describe than a Bitmap since there is a lot of parsing and drawing going on backside the scenes. Merely I think that is non a huge problem if your Drawables are elementary.
My opinion is that you should use Drawables (ShapeDrawables) wherever you can, because they are piece of cake to modify and they don't take much space.
Let's start coding
First let's take a look at a simple example and and then nosotros will recreate a slope as can be seen in the Spotify app/website.
Create a uncomplicated slope ShapeDrawable in XML
Start create a new drawable resources file.
Right click on res/drawable > New > Drawable resource file > give your file a name > apply shape every bit a root element > click Ok
Shape root element defines that this is a ShapeDrawable.
This is how the first instance looks similar:
This is the lawmaking for the first case:
Some useful attributes that you lot can use when defining a shape:
i.) Shape type
You tin specify the type of a shape using android:shape XML aspect in the shape tag. If you don't specify the shape, the default rectangle type is selected. Other available shapes are oval, line and ring. Here is an example:
android:shape= "oval" 2.) Rounded corners
Since our shape is a rectangle, nosotros can circular rectangle's corners. Y'all can practice that inside of the corners tag. You can specify the radius for all of the corners using android:radius. Hither is an example:
android:radius="21dp" You tin, of course, utilize the value from dimens resource file. If you want to be a bit more than experimental, you tin can use a different radius for each corner. You tin practice that using android:topLeftRadius, android:topRightRadius, android:bottomLeftRadius and android:bottomRightRadius. Here is an example:
android:bottomLeftRadius="10dp" iii.) Gradient or solid color
If y'all want to apply a solid color, you should use a solid tag and then within of that tag you can specify the color using android:color. Here is an example:
android:color=@color/your_color_name All the slope attributes take to be in a gradient tag. Yous can specify your outset, center and end colour using android:startColor, android:centerColor and android:endColor. Here is an example:
android:startColor=@color/your_color_name
android:centerColor=@color/your_color_name
android:endColor=@colour/your_color_name If yous don't specify the type of the gradient, the default linear is chosen. Other types are radial and sweep. Here is how to specify the gradient type:
android:blazon="radial" Yous tin even change the angle of the gradient. For example, if you lot want your slope to get from bottom left to top right, you accept to set your angle to android:angle="45" (note that the angle has to exist a multiple of 45).
4.) Specify size
If you want, you can specify the size of the shape. Recall that you tin can change the size later, for case when you use the ShapeDrawable in a ImageView. Yous can change the size inside the size tag. android:layout_height and android:layout_width are used to exercise that. You probably know these two:
android:layout_height="40dp"
android:layout_width="10dp" 5.) Stroke (outline around the shape)
Sometimes you want an outline around your shape and to do that you can employ the stroke tag. Y'all can specify the width and colour of the outline using android:width and android:color. Here is an case:
android:width="2dp"
android:color=@color/your_beautiful_color You can even have dashes every bit an outline around your shape. To get that event you take to utilize these two attributes: android:dashGap, android:dashWidth. Here is an example:
android:dashGap="1dp"
android:dashWidth="4dp" Other attributes that I haven't mentioned can be found in the documentation hither .
Allow'southward use our shape in a View
After you are happy with your shape, yous tin can use it in a View, for example. This is how you could use a circle shape in an ImageView using XML.
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:groundwork="@drawable/my_custom_circle"
android:text="@string/hello_world" /> Instead of using android:background attribute, you lot can use:
android:src="@drawable/my_custom_circle" This is how you can do the aforementioned thing using Coffee
ImageView myImageView = (ImageView) findViewById(R.id.my_image_view); imageView.setImageResource(R.drawable.my_custom_circle);
Alter shapes using Java
At present you lot know how to define shapes using XML and how to use them in Views. But there has to be a way to define and alter them using Java as well, correct? Certain, but I recommend defining shapes using XML, if you can, because information technology is much easier to visualize and check your progress. If y'all have used XML to define your shape, you can use getDrawable method in Java to go the reference to your shape. This method will return a Drawable. Note that yous can manipulate your shapes even when your app is running.
Drawable drawable = getDrawable(R.drawable.button_shape); And so you lot tin can cast your Drawable into a GradientDrawable, for example.
GradientDrawable gradientDrawable = (GradientDrawable) drawable; At this point you lot are ready to showtime modifying your GradientDrawable. Hither are a few examples:
gradientDrawable.setColor(ContextCompat.getColor(this, R.colour.colorPrimary));
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setStroke(12, Color.CYAN); There are much more methods to try out and I want you to do that by clicking here .
Define shapes using Coffee
This is how yous can create and use shapes just using Java. Link for more information virtually that.
RoundRectShape roundRectShape = new RoundRectShape(new float[]{
10, 10, 10, 10,
10, ten, 10, 10}, nix, nothing); ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
shapeDrawable.getPaint().setColor(Color.parseColor("#FFFFFF")); ImageView myImageView = findViewById(R.id.my_image_view);
myImageView.setBackground(shapeDrawable);
// or you lot tin can utilise myImageView.setImageDrawable(shapeDrawable);
At this point you know how to create and use Drawables using Coffee and/or XML and what they are used for.
At present it is time for the last step. Let's recreate this Spotify's gradient using our new skills.
This is the original image of the layout that we will recreate:
This is the code for the gradient background:
This is the code for the rounded rectangle effectually the get access button:
Now let's use these shapes in our layout. This is the terminal code for the layout:
Unfortunately, our font and margins aren't perfect, but I think nosotros did it! It looks nice.
Now information technology is your turn. I take shown you but a simple example and I desire yous to first creating your ain shapes and gradients.
If this mail was helpful, delight click the clap 👏 button below a few times to bear witness your support!
If you want to follow me on social media:
Source: https://medium.com/android-news/android-shape-drawables-tutorial-17fbece6fef5
Posted by: hernandezsuccans.blogspot.com

0 Response to "How To Draw Circle In Xml In Android Studio"
Post a Comment