How To Draw A Rectangle With App Inventor 2
Android Shape Drawables Tutorial
Have you e'er wanted to reduce your Android application's size or make it look more interesting? If yes, and then yous should try out ShapeDrawables.
First, we will go over the advantages and disadvantages of the ShapeDrawables. Then we volition create some Drawables that could be used in your app and lastly for the grand finale we volition try to replicate a gradient as can be seen in the Spotify app/website.
Why should you use ShapeDrawables?
When y'all desire to use PNG or JPEG images in your application, y'all have to provide multiple copies of the same image for different screen densities. That, of form, clutters your app with copies of the same paradigm. Yes, sometimes that is the path nosotros have to choose because we can't use Drawables for every single instance, merely we can dramatically reduce our application'due south size if we tin can employ Drawables instead. ShapeDrawables are a series of commands that tell how to draw something on the screen. That is why they tin be resized and stretched as much every bit we want, without losing whatsoever quality. We can recolor and dispense them even when the app is running and use the same ShapeDrawable multiple times in our app. Since ShapeDrawables are a subclass of the Drawable abstract class, we tin use them in methods where a Drawable is expected. Click for the documentation of the ShapeDrawable.
Are at that place any disadvantages?
Of course, just like I have mentioned before nosotros can't use them in every case. I accept said before that ShapeDrawable class is a subclass of the Drawable abstruse class. There are other subclasses as well and every one of them has its own employ case. You tin click here to check other Drawable types and figure out which one is right for your case. Another upshot is that they took a chip longer to describe than a Bitmap since there is a lot of parsing and drawing going on behind the scenes. But I retrieve that is not a huge problem if your Drawables are simple.
My opinion is that yous should employ Drawables (ShapeDrawables) wherever you tin, because they are like shooting fish in a barrel to modify and they don't have much space.
Allow'south start coding
Kickoff let's have a look at a uncomplicated instance and then we will recreate a gradient as can be seen in the Spotify app/website.
Create a simple gradient ShapeDrawable in XML
Showtime create a new drawable resource file.
Correct click on res/drawable > New > Drawable resource file > give your file a name > use shape as a root element > click Ok
Shape root chemical element defines that this is a ShapeDrawable.
This is how the first example looks like:
This is the code for the first example:
Some useful attributes that you lot tin use when defining a shape:
1.) Shape blazon
You can specify the type of a shape using android:shape XML attribute in the shape tag. If you don't specify the shape, the default rectangle type is selected. Other available shapes are oval, line and band. Hither is an example:
android:shape= "oval" 2.) Rounded corners
Since our shape is a rectangle, we can round rectangle's corners. You tin practise that inside of the corners tag. You lot can specify the radius for all of the corners using android:radius. Here is an instance:
android:radius="21dp" You can, of course, use the value from dimens resources file. If you want to exist a bit more experimental, you can utilise a different radius for each corner. You lot tin can do that using android:topLeftRadius, android:topRightRadius, android:bottomLeftRadius and android:bottomRightRadius. Hither is an example:
android:bottomLeftRadius="10dp" 3.) Slope or solid color
If you lot desire to use a solid color, you should use a solid tag so within of that tag you tin specify the color using android:color. Here is an example:
android:color=@colour/your_color_name All the gradient attributes have to be in a gradient tag. You lot tin specify your first, center and end color using android:startColor, android:centerColor and android:endColor. Hither is an case:
android:startColor=@color/your_color_name
android:centerColor=@colour/your_color_name
android:endColor=@color/your_color_name If yous don't specify the type of the slope, the default linear is chosen. Other types are radial and sweep. Hither is how to specify the slope type:
android:blazon="radial" Yous tin even change the angle of the slope. For example, if you desire your gradient to become from bottom left to peak correct, y'all have to ready your angle to android:angle="45" (note that the bending has to exist a multiple of 45).
4.) Specify size
If you want, you can specify the size of the shape. Retrieve that yous tin change the size later on, for example when you use the ShapeDrawable in a ImageView. You can change the size inside the size tag. android:layout_height and android:layout_width are used to do that. Y'all probably know these two:
android:layout_height="40dp"
android:layout_width="10dp" 5.) Stroke (outline around the shape)
Sometimes you lot want an outline around your shape and to practice that you can utilise the stroke tag. You can specify the width and color of the outline using android:width and android:color. Here is an case:
android:width="2dp"
android:colour=@color/your_beautiful_color You tin can even accept dashes equally an outline around your shape. To get that effect you lot have to use these two attributes: android:dashGap, android:dashWidth. Here is an case:
android:dashGap="1dp"
android:dashWidth="4dp" Other attributes that I haven't mentioned tin be plant in the documentation here .
Permit's use our shape in a View
After yous are happy with your shape, you can use it in a View, for instance. This is how y'all could utilize 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 aspect, you lot tin utilise:
android:src="@drawable/my_custom_circle" This is how you can practice the same thing using Java
ImageView myImageView = (ImageView) findViewById(R.id.my_image_view); imageView.setImageResource(R.drawable.my_custom_circle);
Modify shapes using Coffee
At present you know how to define shapes using XML and how to utilise them in Views. Simply at that place has to be a way to ascertain and modify them using Coffee too, right? Sure, but I recommend defining shapes using XML, if yous can, because information technology is much easier to visualize and cheque your progress. If you accept used XML to define your shape, you can use getDrawable method in Java to get the reference to your shape. This method will return a Drawable. Notation that you lot can manipulate your shapes even when your app is running.
Drawable drawable = getDrawable(R.drawable.button_shape); Then y'all tin bandage your Drawable into a GradientDrawable, for example.
GradientDrawable gradientDrawable = (GradientDrawable) drawable; At this bespeak you are prepare to start modifying your GradientDrawable. Here are a few examples:
gradientDrawable.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setStroke(12, Color.CYAN); There are much more methods to effort out and I want you to do that by clicking here .
Define shapes using Coffee
This is how you can create and use shapes merely using Java. Link for more information about that.
RoundRectShape roundRectShape = new RoundRectShape(new bladder[]{
10, x, ten, 10,
10, 10, 10, x}, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
shapeDrawable.getPaint().setColor(Colour.parseColor("#FFFFFF")); ImageView myImageView = findViewById(R.id.my_image_view);
myImageView.setBackground(shapeDrawable);
// or you can use myImageView.setImageDrawable(shapeDrawable);
At this signal you know how to create and use Drawables using Java and/or XML and what they are used for.
Now it is time for the concluding step. Allow'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 around the become access button:
At present allow's use these shapes in our layout. This is the final lawmaking for the layout:
Unfortunately, our font and margins aren't perfect, simply I call up we did it! Information technology looks prissy.
Now information technology is your turn. I have shown y'all just a simple example and I want yous to start creating your ain shapes and gradients.
If this postal service was helpful, please click the clap 👏 push button below a few times to show your support!
If you want to follow me on social media:
Source: https://medium.com/android-news/android-shape-drawables-tutorial-17fbece6fef5
Posted by: browncorgentor.blogspot.com

0 Response to "How To Draw A Rectangle With App Inventor 2"
Post a Comment