Shape drawable
When you want to dynamically draw a 2-dimensional image, the shapedrawable object may be your right choice. With shapedrawable, you can draw the original shape and apply it to any style at will.
Shapedrawable is a drawable derived class, so you can use it wherever you want to use drawable-for example, it may be a view background, which is set through setbackgrounddrawable. of course, you can also draw your shape as your own custom view and add it to your layout as you like. because shapedrawable has its own draw () method, you can create a subclass of view and then in view. draw this shapedrawable in the ondraw () method. the following is a simple view derived class, which only draws the shapedrawable as a view:
public class CustomDrawableView extends View { private ShapeDrawable mDrawable; public CustomDrawableView(Context context) { super(context); int x = 10; int y = 10; int width = 300; int height = 50; mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(0xff74AC23); mDrawable.setBounds(x, y, x + width, y + height); } protected void onDraw(Canvas canvas) { mDrawable.draw(canvas); } }
In the constructor, A shapedrawable is defined using ovalshape. then, it sets the border of color and shape. if you do not set the border, the shape will not be drawn. If you do not set the color, it will be painted in black.
Using custom view, you can draw anything. In the above example, we can use a program to paint freely in an activity:
CustomDrawableView mCustomDrawableView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mCustomDrawableView = new CustomDrawableView(this); setContentView(mCustomDrawableView); }
If you want to draw the custom drawable in xmllayout instead of the activity, the customdrawableview class must overwrite the view (context, attributeset) constructor, this method is called when initializing a view from XML. then add a customdrawable element to XML, as shown in the following code:
<com.example.shapedrawable.CustomDrawableView android:layout_width="fill_parent" android:layout_height="wrap_content" />
Shapedrawable class (like android. graphics. other drawable types in the drawable package) allow you to use public methods to define various attributes of drawable. some attributes you want to adjust may have alpha transparency, color consideration, jitter, opacity and color.
You can also use XML to define the original drawable shape. For more information, see the shapedrawables section in the drawable resource documentation.
Ninepatch
A ninepatchdrawable image is a drag-and-drop bitmap. When it is used as the view background, Android automatically scales its size to adapt to the View content. one example is that the standard button uses ninepatch as its own background-extensions and stores them in the Res/drawable/folder of your project.
The border is used to define the tensile and static areas of the image. you can draw one (or more) One-pixel black line on the upper and left borders to indicate a scalable area (other borders must be completely transparent or white ). you can set any number of scalable segments: their relative sizes remain unchanged, so the maximum segments are always the largest.
You can also draw a line between the right border and the bottom border to define an optional printable segment of an image (in fact, it is called a fill line segment ). if a view object sets ninepatch as the background and then specifies its own text, it will stretch itself so that all the text is exactly within the region specified by the right and bottom line segments. if the filled line segment does not exist, Android uses the left and top line segments to define this printable area.
The left and top line segments define which pixels of the image can be copied during stretching; the bottom and right line segments define the areas where the image content should be placed, the locations of these regions are relative.
The following is an example of defining a button using the ninepatch file:
This ninepatch defines an area that can be stretched with the left line segment and the right and bottom line segments define the area that can be drawn. in the first figure, the gray dotted line shows the area to be copied during stretching, and the red box shows the area where the view content should be. if the content is not suitable for this area, the image will be stretched until it can fit.
The 9-patch tool provides a very handy way to create your ninepatch image, which has a WYSIWYG image editor. if the stretched area you define has the risk of generating pixel re-writing, it may even issue a warning.
XML example
The following is an example of layoutxml to demonstrate how to add a ninepatch image to a pair of buttons. (The ninepatch image is saved as res/drawable/my_button_background.9.png)
<Button id="@+id/tiny" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerInParent="true" android:text="Tiny" android:textSize="8sp" android:background="@drawable/my_button_background"/><Button id="@+id/big" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerInParent="true" android:text="Biiiiiiig text!" android:textSize="30sp" android:background="@drawable/my_button_background"/>
Note that the width and height values are set to "wrap_content" so that the button can exactly contain text.
It is two buttons drawn from the ninepatch image shown in XML and. Pay attention to how the width and height of the button change with the text, and how the background image is stretched to adapt to it.