GradientDrawable
Gradient Drawable works the same as our xmlshape, supporting gradient, rounded corners, etc.
Look at the construction method first.
orientation specifies the direction of the gradient (the following eight kinds). The color of the gradient is specified by the colors array. Each value in the array is a color.mDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR, new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF });
/** * Controlling the direction of the gradient relative to the renderable boundary */ public enum Orientation { /** Draw gradients from top to bottom*/ TOP_BOTTOM, /** draw the gradient from the top-right to the bottom-left */ TR_BL, /** Draw gradients from right to left */ RIGHT_LEFT, /** Draw a gradient from the lower right corner to the upper left corner */ BR_TL, /** Draw gradients from bottom to top */ BOTTOM_TOP, /** Draw a gradient from the lower left corner to the upper right corner */ BL_TR, /** Draw gradients from left to right */ LEFT_RIGHT, /** Draw a gradient from the upper left corner to the lower right corner */ TL_BR, }
Three gradient modes, Liner,Radial and Sweep, are used to show the different uses of Gradient Drawable by specifying the radius of four corners and rounds of a rectangle.
@Override protected void onDraw(Canvas canvas) { mDrawable.setBounds(mRect); float r = 16; canvas.save(); canvas.translate(10, 10); mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); setCornerRadii(mDrawable, r, r, 0, 0); mDrawable.draw(canvas); canvas.restore(); canvas.save(); canvas.translate(10 + mRect.width() + 10, 10); mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT); setCornerRadii(mDrawable, 0, 0, r, r); mDrawable.draw(canvas); canvas.restore(); canvas.translate(0, mRect.height() + 10); canvas.save(); canvas.translate(10, 10); mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT); setCornerRadii(mDrawable, 0, r, r, 0); mDrawable.draw(canvas); canvas.restore(); canvas.save(); canvas.translate(10 + mRect.width() + 10, 10); mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); setCornerRadii(mDrawable, r, 0, 0, r); mDrawable.draw(canvas); canvas.restore(); canvas.translate(0, mRect.height() + 10); canvas.save(); canvas.translate(10, 10); mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT); setCornerRadii(mDrawable, r, 0, r, 0); mDrawable.draw(canvas); canvas.restore(); canvas.save(); canvas.translate(10 + mRect.width() + 10, 10); mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT); setCornerRadii(mDrawable, 0, r, 0, r); mDrawable.draw(canvas); canvas.restore(); }
It is also allowed to set four corners of a rectangle as rounded corners. The radius of each corner can be set separately:
public void setCornerRadii(float[] radii)
Radii arrays specify the radii of four rounded corners, each of which can specify [X_Radius,Y_Radius]. The order of the four rounded corners is upper left, upper right, lower right and lower left. If X_Radius,Y_Radius is 0 or right angle.
static void setCornerRadii(GradientDrawable drawable, float r0, float r1, float r2, float r3) { drawable.setCornerRadii(new float[] { r0, r0, r1, r1, r2, r2, r3, r3 }); }