下面列出了怎么用android.graphics.drawable.shapes.RectShape的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Set a hue gradient progress drawable for a seek bar.
*
* @param seekBar The seek bar to set the hue gradient.
*/
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void setHueDrawable(@NonNull SeekBar seekBar) {
if (DynamicSdkUtils.is21()) {
seekBar.setProgressTintList(null);
}
LinearGradient gradient =
new LinearGradient(0.0f, 0.0f, (float) seekBar.getWidth(), 0.0f,
new int[] { 0xFFFF0000, 0xFFFFFF00, 0xFF00FF00,
0xFF00FFFF, 0xFF0000FF, 0xFFFF00FF, 0xFFFF0000 },
null, Shader.TileMode.CLAMP);
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.getPaint().setShader(gradient);
Rect bounds = seekBar.getProgressDrawable().getBounds();
bounds.inset(0, (int) (bounds.height() * 0.45f));
seekBar.setProgressDrawable(shape);
seekBar.getProgressDrawable().setBounds(bounds);
}
public static Drawable drawable(final int[] colorBoxes, final float[] position, final GradientAngle gradientAngle) {
ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
AngleCoordinate ac = AngleCoordinate.getAngleCoordinate(gradientAngle, width, height);
LinearGradient linearGradient = new LinearGradient(ac.x1, ac.y1, ac.x2, ac.y2,
colorBoxes,
position,
Shader.TileMode.REPEAT);
return linearGradient;
}
};
PaintDrawable paint = new PaintDrawable();
paint.setShape(new RectShape());
paint.setShaderFactory(shaderFactory);
return paint;
}
public BorderDrawable(String shapeType, final int borderColor, final int borderWidth)
{
super();
final Shape shape = shapeType.equals(RECT) ? new RectShape() : new OvalShape();
final ShapeDrawable transparentShape = new ShapeDrawable(shape);
transparentShape.getPaint().setColor(0x00000000);// Transparent
final GradientDrawable shapeDrawable = new GradientDrawable();
shapeDrawable.setShape(shapeType.equals(RECT) ? GradientDrawable.RECTANGLE : GradientDrawable.OVAL);
shapeDrawable.setStroke(borderWidth, borderColor);
addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused, -android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] { android.R.attr.state_enabled, -android.R.attr.state_focused, android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused, android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] {}, transparentShape);
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
final int strokeWidth = mHeaderProgressBar.getResources()
.getDimensionPixelSize(R.dimen.ptr_progress_bar_stroke_width);
mHeaderProgressBar.setIndeterminateDrawable(
new SmoothProgressDrawable.Builder(mHeaderProgressBar.getContext())
.color(mProgressDrawableColor)
.width(strokeWidth)
.build());
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
public BorderDrawable(String shapeType, final int borderColor, final int borderWidth)
{
super();
final Shape shape = shapeType.equals(RECT) ? new RectShape() : new OvalShape();
final ShapeDrawable transparentShape = new ShapeDrawable(shape);
transparentShape.getPaint().setColor(0x00000000);// Transparent
final GradientDrawable shapeDrawable = new GradientDrawable();
shapeDrawable.setShape(shapeType.equals(RECT) ? GradientDrawable.RECTANGLE : GradientDrawable.OVAL);
shapeDrawable.setStroke(borderWidth, borderColor);
addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused, -android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] { android.R.attr.state_enabled, -android.R.attr.state_focused, android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused, android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] {}, transparentShape);
}
private Drawable getCropDrawable(int fillColor, int fillAlpha, int strokeColor, int strokeAlpha, int strokeWidth) {
ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
sd1.getPaint().setColor(strokeColor);
sd1.getPaint().setStyle(Style.STROKE);
sd1.getPaint().setStrokeWidth(strokeWidth);
sd1.setAlpha(255);
ShapeDrawable sd2 = new ShapeDrawable(new RectShape());
sd2.getPaint().setColor(fillColor);
sd2.getPaint().setStyle(Style.FILL);
sd2.setAlpha(fillAlpha);
Drawable[] layers = new Drawable[2];
layers[0] = sd1;
layers[1] = sd2;
LayerDrawable composite = new LayerDrawable(layers);
return composite;
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
final int strokeWidth = mHeaderProgressBar.getResources()
.getDimensionPixelSize(R.dimen.ptr_progress_bar_stroke_width);
mHeaderProgressBar.setIndeterminateDrawable(
new SmoothProgressDrawable.Builder(mHeaderProgressBar.getContext())
.color(mProgressDrawableColor)
.width(strokeWidth)
.build());
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
public BorderDrawable(String shapeType, final int borderColor, final int borderWidth)
{
super();
final Shape shape = shapeType.equals(RECT) ? new RectShape() : new OvalShape();
final ShapeDrawable transparentShape = new ShapeDrawable(shape);
transparentShape.getPaint().setColor(0x00000000);// Transparent
final GradientDrawable shapeDrawable = new GradientDrawable();
shapeDrawable.setShape(shapeType.equals(RECT) ? GradientDrawable.RECTANGLE : GradientDrawable.OVAL);
shapeDrawable.setStroke(borderWidth, borderColor);
addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused, -android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] { android.R.attr.state_enabled, -android.R.attr.state_focused, android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused, android.R.attr.state_pressed }, shapeDrawable);
addState(new int[] {}, transparentShape);
}
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private void addDividerItemDecoration(@ColorInt int color, int orientation,
int paddingLeft, int paddingTop,
int paddingRight, int paddingBottom) {
DividerItemDecoration decor = new DividerItemDecoration(getContext(), orientation);
if (color != 0) {
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
shapeDrawable.setIntrinsicHeight(Utils.dpToPx(getContext(), 1));
shapeDrawable.setIntrinsicWidth(Utils.dpToPx(getContext(), 1));
shapeDrawable.getPaint().setColor(color);
InsetDrawable insetDrawable = new InsetDrawable(shapeDrawable, paddingLeft, paddingTop, paddingRight, paddingBottom);
decor.setDrawable(insetDrawable);
}
decor.setShowLastDivider(showLastDivider);
addItemDecoration(decor);
}
private void setShape() {
ShapeDrawable drawable = new ShapeDrawable();
// Set shape of drawable.
switch (shape) {
case Component.BUTTON_SHAPE_ROUNDED:
drawable.setShape(new RoundRectShape(ROUNDED_CORNERS_ARRAY, null, null));
break;
case Component.BUTTON_SHAPE_RECT:
drawable.setShape(new RectShape());
break;
case Component.BUTTON_SHAPE_OVAL:
drawable.setShape(new OvalShape());
break;
default:
throw new IllegalArgumentException();
}
// Set drawable to the background of the button.
if (!AppInventorCompatActivity.isClassicMode() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewUtil.setBackgroundDrawable(view, new RippleDrawable(createRippleState(), drawable, drawable));
} else {
ViewUtil.setBackgroundDrawable(view, drawable);
}
if (backgroundColor == Component.COLOR_NONE) {
view.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.CLEAR);
}
else if (backgroundColor == Component.COLOR_DEFAULT) {
view.getBackground().setColorFilter(SHAPED_DEFAULT_BACKGROUND_COLOR, PorterDuff.Mode.SRC_ATOP);
}
else {
view.getBackground().setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
}
view.invalidate();
}
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private Builder() {
text = "";
color = Color.GRAY;
borderColor = getDarkerShade(color);
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.DEFAULT;
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private void setCardStyling() {
final boolean isLightBg = ColorUtils.isLightColor(containerBackgroundColor);
if (isLightBg) {
cardBorderColor = getResources().getColor(R.color.tw__ad_light_card_border_color);
} else {
cardBorderColor = getResources().getColor(R.color.tw__ad_dark_card_border_color);
}
final ShapeDrawable bgDrawable = new ShapeDrawable(new RectShape());
bgDrawable.getPaint().setColor(cardBackgroundColor);
final ShapeDrawable borderDrawable = new ShapeDrawable(new RectShape());
borderDrawable.getPaint().setColor(cardBorderColor);
final Drawable[] layers = new Drawable[2];
layers[0] = borderDrawable;
layers[1] = bgDrawable;
final LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, 1, 0, 1, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
cardLayout.setBackground(layerDrawable);
} else {
cardLayout.setBackgroundDrawable(layerDrawable);
}
}
/**
* @param overlayController The overlay on which to show the scan bars
* @param service The service that will dispatch the gestures
*/
public PointScanManager(OverlayController overlayController, AccessibilityService service) {
this.overlayController = overlayController;
this.service = service;
lineDrawable = new ShapeDrawable(new RectShape());
isPerformingCustomSwipe = false;
SwitchAccessPreferenceUtils.registerSwitchAccessPreferenceChangedListener(service, this);
overlayController.addMenuListener(this);
}
public Drawable getStripeDrawable() {
ShapeDrawable bg = new ShapeDrawable(new RectShape());
int[] pixels = new int[indentWidth];
for (int i = 0; i < indentWidth / 3; i++) {
pixels[i] = colorBg;
pixels[i + indentWidth / 3] = colorBg;
pixels[i + indentWidth / 3 * 2] = colorOrange;
}
Bitmap bm = Bitmap.createBitmap(pixels, indentWidth, 1, Bitmap.Config.ARGB_8888);
Shader shader = new BitmapShader(bm, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
bg.getPaint().setShader(shader);
return bg;
}
private static ShapeDrawable createRectShape(int width, int height, int color) {
ShapeDrawable shape = new ShapeDrawable(new RectShape());
shape.setIntrinsicHeight(height);
shape.setIntrinsicWidth(width);
shape.getPaint().setColor(color);
return shape;
}
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private void updateHeaderState(){
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(page < maxPage ? nextPageColor : refreshColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
pfbProgressbar.setProgressDrawable(clipDrawable);
pfbTitle.setText(page < maxPage ? R.string.pull_bottom_nextpage : R.string.pull_to_refresh_pull_label);
}
private Builder() {
text = "";
color = Color.GRAY;
textColor = Color.WHITE;
borderThickness = 0;
width = -1;
height = -1;
shape = new RectShape();
font = Typeface.create("sans-serif-light", Typeface.NORMAL);
fontSize = -1;
isBold = false;
toUpperCase = false;
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private void applyProgressBarSettings() {
if (mHeaderProgressBar != null) {
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(mProgressDrawableColor);
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
mHeaderProgressBar.setProgressDrawable(clipDrawable);
}
}
private LayerDrawable getLayerList(boolean isPressed) {
float[] radii = new float[8];
for (int i = 0; i < radii.length; i++) {
radii[i] = corners;
}
ShapeDrawable shapeShadow = new ShapeDrawable(new RectShape());
shapeShadow.getPaint().setColor(shadowColor);
shapeShadow.setShape(new RoundRectShape(radii, null, null));
ShapeDrawable shapeBackground = new ShapeDrawable(new RectShape());
shapeBackground.getPaint().setColor(backgroundColor);
shapeBackground.setShape(new RoundRectShape(radii, null, null));
LayerDrawable composite = new LayerDrawable(new Drawable[]{shapeShadow, shapeBackground});
if (isPressed) {
composite.setLayerInset(0, 0, (int) (normalHeight - pressedHeight), 0, 0);
composite.setLayerInset(1, 0, (int) (normalHeight - pressedHeight), 0, (int) pressedHeight);
} else {
composite.setLayerInset(0, 0, 0, 0, 0);
composite.setLayerInset(1, 0, 0, 0, (int) normalHeight);
}
return composite;
}
private ProgressBar createProgressBar() {
ProgressBar pb = (ProgressBar) View.inflate(mActivity, R.layout.actionbar_progressbar, null);
ShapeDrawable shape = new ShapeDrawable();
shape.setShape(new RectShape());
shape.getPaint().setColor(Color.parseColor("#FF33B5E5"));
ClipDrawable clipDrawable = new ClipDrawable(shape, Gravity.CENTER, ClipDrawable.HORIZONTAL);
pb.setProgressDrawable(clipDrawable);
return pb;
}
/**
* Get the tooltip shape
*
* @return
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private Shape getTooltipShape() {
Resources r = this.getContext().getResources();
final int pointHeightPx = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, POINTER_HEIGHT,
r.getDisplayMetrics());
final int pointedHeightPx = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, POINTER_WIDE_HEIGHT,
r.getDisplayMetrics());
final int pointStartPx = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, POINTER_START,
r.getDisplayMetrics());
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mContentHolder
.getLayoutParams();
mToolTipTV.setY(pointHeightPx);
params.height = mToolTipTV.getHeight() + pointHeightPx;
final Path rectPath = new Path();
final Path rectBorderPath = new Path();
// Create the rectangular shape
Shape shape = new RectShape() {
@Override
protected void onResize(float w, float h) {
createShapePath(rectPath, rectBorderPath, w, h, pointHeightPx,
pointedHeightPx, pointStartPx);
}
@Override
public void draw(Canvas canvas, Paint paint) {
drawOnCanvas(canvas, paint, rectPath, rectBorderPath);
}
};
return shape;
}
public static OrreryDrawable Create() {
ShapeDrawable space = new ShapeDrawable(new RectShape());
space.getPaint().setColor(Color.BLACK);
space.setIntrinsicHeight(SPACE_HEIGHT);
space.setIntrinsicWidth(SPACE_HEIGHT);
ShapeDrawable sun = new ShapeDrawable(new OvalShape());
sun.getPaint().setColor(Color.YELLOW);
sun.setIntrinsicHeight(RADIUS_SUN * 2);
sun.setIntrinsicWidth(RADIUS_SUN * 2);
ShapeDrawable earth = new ShapeDrawable(new OvalShape());
earth.getPaint().setColor(Color.BLUE);
earth.setIntrinsicHeight(RADIUS_EARTH * 2);
earth.setIntrinsicWidth(RADIUS_EARTH * 2);
ShapeDrawable moon = new ShapeDrawable(new OvalShape());
moon.getPaint().setColor(Color.LTGRAY);
moon.setIntrinsicHeight(RADIUS_MOON * 2);
moon.setIntrinsicWidth(RADIUS_MOON * 2);
Drawable[] bodies = { space, sun, earth, moon };
OrreryDrawable myOrrery = new OrreryDrawable(bodies);
myOrrery.setEarthPosition(0);
myOrrery.setMoonPosition(0);
myOrrery.setLayerInset(SPACE_ID, 0, 0, 0, 0);
myOrrery.setLayerInset(SUN_ID, SPACE_HEIGHT / 2 - RADIUS_SUN,
SPACE_HEIGHT / 2 - RADIUS_SUN, SPACE_HEIGHT / 2 - RADIUS_SUN,
SPACE_HEIGHT / 2 - RADIUS_SUN);
return myOrrery;
}