banner



How Do You Draw Volume In The Human Form Pinrest

Have you ever wanted to reduce your Android application'south size or brand it look more than interesting? If yes, then you should try out ShapeDrawables.

Outset, we will go over the advantages and disadvantages of the ShapeDrawables. Then we will create some Drawables that could be used in your app and lastly for the m finale we will try to replicate a gradient as can be seen in the Spotify app/website.

Why should y'all use ShapeDrawables?

When you want to use PNG or JPEG images in your application, y'all have to provide multiple copies of the same epitome for different screen densities. That, of class, clutters your app with copies of the same image. Yes, sometimes that is the path nosotros have to choose because we can't use Drawables for every single case, just we tin dramatically reduce our application's size if nosotros can use Drawables instead. ShapeDrawables are a series of commands that tell how to draw something on the screen. That is why they can be resized and stretched as much every bit we want, without losing whatsoever quality. We can recolor and manipulate them even when the app is running and use the aforementioned ShapeDrawable multiple times in our app. Since ShapeDrawables are a subclass of the Drawable abstract form, we can use them in methods where a Drawable is expected. Click for the documentation of the ShapeDrawable.

Are in that location any disadvantages?

Of class, just similar I have mentioned earlier we can't use them in every case. I take said before that ShapeDrawable grade is a bracket of the Drawable abstract class. There are other subclasses too and every i of them has its ain use example. You can click hither to check other Drawable types and figure out which 1 is correct for your instance. Some other event is that they took a bit longer to draw than a Bitmap since there is a lot of parsing and drawing going on behind the scenes. Just I call up that is not a huge problem if your Drawables are elementary.

My opinion is that you should use Drawables (ShapeDrawables) wherever you can, because they are easy to modify and they don't take much infinite.

Let's offset coding

Kickoff let's have a wait at a unproblematic example and then we will recreate a gradient equally tin be seen in the Spotify app/website.

Create a simple slope ShapeDrawable in XML

Start create a new drawable resources file.

Correct click on res/drawable > New > Drawable resource file > give your file a name > use shape as a root chemical element > click Ok

Shape root element defines that this is a ShapeDrawable.

This is how the offset case looks similar:

shape_drawable_example_1.xml

This is the code for the outset example:

Some useful attributes that you can utilize when defining a shape:

1.) Shape type

You tin can specify the type of a shape using android:shape XML attribute in the shape tag. If y'all 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 tin can round rectangle'southward corners. You can do that within of the corners tag. You tin can specify the radius for all of the corners using android:radius. Here is an example:

          android:radius="21dp"                  

You can, of course, use the value from dimens resource file. If yous want to be a bit more experimental, you can use a different radius for each corner. Y'all can do that using android:topLeftRadius, android:topRightRadius, android:bottomLeftRadius and android:bottomRightRadius. Hither is an example:

          android:bottomLeftRadius="10dp"        

iii.) Gradient or solid color

If you desire to use a solid color, you should utilise a solid tag and then inside of that tag you can specify the color using android:color. Here is an case:

          android:color=@color/your_color_name        

All the gradient attributes have to be in a slope tag. You tin specify your start, center and terminate color using android:startColor, android:centerColor and android:endColor. Hither is an example:

          android:startColor=@color/your_color_name
android:centerColor=@colour/your_color_name
android:endColor=@color/your_color_name

If you don't specify the blazon of the slope, the default linear is called. Other types are radial and sweep. Here is how to specify the gradient type:

          android:type="radial"        

You can fifty-fifty change the angle of the gradient. For instance, if you desire your slope to get from bottom left to top correct, you accept to prepare your angle to android:angle="45" (note that the angle has to exist a multiple of 45).

4.) Specify size

If y'all want, you can specify the size of the shape. Recollect that yous can change the size later, for example when you apply the ShapeDrawable in a ImageView. You can change the size within the size tag. android:layout_height and android:layout_width are used to practice that. You probably know these two:

          android:layout_height="40dp"
android:layout_width="10dp"

5.) Stroke (outline around the shape)

Sometimes you desire an outline around your shape and to do that you lot can use the stroke tag. Y'all can specify the width and color of the outline using android:width and android:color. Here is an example:

          android:width="2dp"
android:color=@color/your_beautiful_color

Y'all can even accept dashes equally an outline around your shape. To get that effect you have to use these two attributes: android:dashGap, android:dashWidth. Hither is an example:

          android:dashGap="1dp"
android:dashWidth="4dp"

Other attributes that I oasis't mentioned can be found in the documentation here .

Let'due south employ our shape in a View

After yous are happy with your shape, you can apply it in a View, for instance. This is how you could utilise 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:background="@drawable/my_custom_circle"
android:text="@string/hello_world" />

Instead of using android:background attribute, you can use:

          android:src="@drawable/my_custom_circle"        

This is how you tin do the aforementioned thing using Coffee

          ImageView myImageView = (ImageView) findViewById(R.id.my_image_view);          imageView.setImageResource(R.drawable.my_custom_circle);        

Modify shapes using Java

Now you lot know how to define shapes using XML and how to employ them in Views. But there has to be a way to define and change them using Java as well, right? Sure, but I recommend defining shapes using XML, if you tin can, because information technology is much easier to visualize and check your progress. If yous have used XML to define your shape, you can use getDrawable method in Java to get the reference to your shape. This method volition render a Drawable. Note that y'all can manipulate your shapes even when your app is running.

          Drawable drawable = getDrawable(R.drawable.button_shape);        

Then y'all can cast your Drawable into a GradientDrawable, for example.

          GradientDrawable gradientDrawable = (GradientDrawable) drawable;        

At this signal you are set to offset modifying your GradientDrawable. Here are a few examples:

          gradientDrawable.setColor(ContextCompat.getColor(this, R.color.colorPrimary));
gradientDrawable.setShape(GradientDrawable.OVAL);
gradientDrawable.setStroke(12, Colour.CYAN);

There are much more than methods to endeavour out and I want you to do that past clicking here .

Define shapes using Java

This is how you can create and apply shapes simply using Java. Link for more information nigh that.

          RoundRectShape roundRectShape = new RoundRectShape(new bladder[]{
10, 10, 10, 10,
10, 10, 10, ten}, 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 lot tin utilise myImageView.setImageDrawable(shapeDrawable);

At this betoken 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 pace. Allow'due south recreate this Spotify's gradient using our new skills.

This is the original image of the layout that nosotros will recreate:

Original prototype

This is the code for the gradient background:

This is the code for the rounded rectangle effectually the get access button:

Now let's utilize these shapes in our layout. This is the final lawmaking for the layout:

Unfortunately, our font and margins aren't perfect, but I think we did it! It looks dainty.

Our recreation

Now it is your plough. I have shown you lot just a unproblematic example and I want you to starting time creating your own shapes and gradients.

If this postal service was helpful, please click the clap 👏 button below a few times to testify your support!

If you want to follow me on social media:

Source: https://medium.com/android-news/android-shape-drawables-tutorial-17fbece6fef5

Posted by: meiergrased.blogspot.com

0 Response to "How Do You Draw Volume In The Human Form Pinrest"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel