下面列出了怎么用android.graphics.drawable.ShapeDrawable的API类实例代码及写法,或者点击链接到github查看源代码。
private ShapeHolder addBall(float x, float y) {
OvalShape circle = new OvalShape();
circle.resize(BALL_SIZE, BALL_SIZE);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x);
shapeHolder.setY(y);
int red = (int)(100 + Math.random() * 155);
int green = (int)(100 + Math.random() * 155);
int blue = (int)(100 + Math.random() * 155);
int color = 0xff000000 | red << 16 | green << 8 | blue;
Paint paint = drawable.getPaint();
int darkColor = 0xff000000 | red/4 << 16 | green/4 << 8 | blue/4;
RadialGradient gradient = new RadialGradient(37.5f, 12.5f,
50f, color, darkColor, Shader.TileMode.CLAMP);
paint.setShader(gradient);
shapeHolder.setPaint(paint);
balls.add(shapeHolder);
return shapeHolder;
}
private Drawable createCircleDrawable(int color, float strokeWidth) {
int alpha = Color.alpha(color);
int opaqueColor = opaque(color);
ShapeDrawable fillDrawable = new ShapeDrawable(new OvalShape());
final Paint paint = fillDrawable.getPaint();
paint.setAntiAlias(true);
paint.setColor(opaqueColor);
Drawable[] layers = {
fillDrawable,
createInnerStrokesDrawable(opaqueColor, strokeWidth)
};
LayerDrawable drawable = alpha == 255 || !mStrokeVisible
? new LayerDrawable(layers)
: new TranslucentLayerDrawable(alpha, layers);
int halfStrokeWidth = (int) (strokeWidth / 2f);
drawable.setLayerInset(1, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);
return drawable;
}
private static Drawable getBackground(int normalStateColor,
int pressedStateColor) {
StateListDrawable background = new StateListDrawable();
int c = SizeUtil.dp10;
float[] r = new float[] {c, c, c, c, c, c, c, c};
RoundRectShape rr = new RoundRectShape(r, null, null);
ShapeDrawable cd = new ShapeDrawable();
cd.setShape(rr);
cd.getPaint().setColor(pressedStateColor);
background.addState(new int[] {android.R.attr.state_pressed,
android.R.attr.state_focused}, cd);
background.addState(new int[] {-android.R.attr.state_pressed,
android.R.attr.state_focused}, cd);
background.addState(new int[] {android.R.attr.state_pressed,
-android.R.attr.state_focused}, cd);
ShapeDrawable cd1 = new ShapeDrawable();
cd1.setShape(rr);
cd1.getPaint().setColor(normalStateColor);
background.addState(new int[] {-android.R.attr.state_pressed,
-android.R.attr.state_focused}, cd1);
return background;
}
/**
* 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);
}
private void buildColorPickerView(View view, int colorCode) {
view.setVisibility(View.VISIBLE);
ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
biggerCircle.setIntrinsicHeight(20);
biggerCircle.setIntrinsicWidth(20);
biggerCircle.setBounds(new Rect(0, 0, 20, 20));
biggerCircle.getPaint().setColor(colorCode);
ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
smallerCircle.setIntrinsicHeight(5);
smallerCircle.setIntrinsicWidth(5);
smallerCircle.setBounds(new Rect(0, 0, 5, 5));
smallerCircle.getPaint().setColor(Color.WHITE);
smallerCircle.setPadding(10, 10, 10, 10);
Drawable[] drawables = {smallerCircle, biggerCircle};
LayerDrawable layerDrawable = new LayerDrawable(drawables);
view.setBackgroundDrawable(layerDrawable);
}
private ShapeDrawable a()
{
int i1 = a(8);
float af[] = new float[8];
af[0] = i1;
af[1] = i1;
af[2] = i1;
af[3] = i1;
af[4] = i1;
af[5] = i1;
af[6] = i1;
af[7] = i1;
ShapeDrawable shapedrawable = new ShapeDrawable(new RoundRectShape(af, null, null));
shapedrawable.getPaint().setColor(n);
return shapedrawable;
}
private Drawable createCircleDrawable(int color, float strokeWidth) {
int alpha = Color.alpha(color);
int opaqueColor = opaque(color);
ShapeDrawable fillDrawable = new ShapeDrawable(new OvalShape());
final Paint paint = fillDrawable.getPaint();
paint.setAntiAlias(true);
paint.setColor(opaqueColor);
Drawable[] layers = {
fillDrawable,
createInnerStrokesDrawable(opaqueColor, strokeWidth)
};
LayerDrawable drawable = alpha == 255 || !mStrokeVisible
? new LayerDrawable(layers)
: new TranslucentLayerDrawable(alpha, layers);
int halfStrokeWidth = (int) (strokeWidth / 2f);
drawable.setLayerInset(1, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);
return drawable;
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
ShapeDrawable drawable = new ShapeDrawable(new DirectionShape(3));
Paint paint = drawable.getPaint();
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.DirectionView);
int color = a.getColor(R.styleable.DirectionView_iLineColor, Color.GRAY);
int size = a.getDimensionPixelOffset(R.styleable.DirectionView_iLineSize, (int) (getResources().getDisplayMetrics().density * 2));
a.recycle();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStrokeWidth(size);
paint.setColor(color);
setBackground(drawable);
}
private Drawable createCircleDrawable(int color, float strokeWidth) {
int alpha = Color.alpha(color);
int opaqueColor = opaque(color);
ShapeDrawable fillDrawable = new ShapeDrawable(new OvalShape());
final Paint paint = fillDrawable.getPaint();
paint.setAntiAlias(true);
paint.setColor(opaqueColor);
Drawable[] layers = {
fillDrawable,
createInnerStrokesDrawable(opaqueColor, strokeWidth)
};
LayerDrawable drawable = alpha == 255 || !mStrokeVisible
? new LayerDrawable(layers)
: new TranslucentLayerDrawable(alpha, layers);
int halfStrokeWidth = (int) (strokeWidth / 2f);
drawable.setLayerInset(1, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);
return drawable;
}
private void createMovingItem() {
OvalShape circle = new OvalShape();
ShapeDrawable drawable = new ShapeDrawable(circle);
movingItem = new ShapeHolder(drawable);
Paint paint = drawable.getPaint();
paint.setColor(mIndicatorSelectedBackground);
paint.setAntiAlias(true);
switch (mIndicatorMode) {
case INSIDE:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
break;
case OUTSIDE:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
break;
case SOLO:
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
break;
}
movingItem.setPaint(paint);
}
/**
* Sets the background color of the drawable
**/
public static void setDrawableColor(Context context, Drawable drawable, @AttrRes int colorAttrRes) {
int colorRes = getResourceId(context, colorAttrRes);
int color = ContextCompat.getColor(context, colorRes);
if (drawable instanceof ShapeDrawable) {
((ShapeDrawable) drawable).getPaint().setColor(color);
} else if (drawable instanceof GradientDrawable) {
((GradientDrawable) drawable).setColor(color);
} else if (drawable instanceof ColorDrawable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
((ColorDrawable) drawable).setColor(color);
}
} else if (drawable instanceof RotateDrawable) {
setDrawableColor(context, ((RotateDrawable) drawable).getDrawable(), colorAttrRes);
}
}
private ShapeHolder addBall(float x, float y) {
OvalShape circle = new OvalShape();
circle.resize(50f, 50f);
ShapeDrawable drawable = new ShapeDrawable(circle);
ShapeHolder shapeHolder = new ShapeHolder(drawable);
shapeHolder.setX(x - 25f);
shapeHolder.setY(y - 25f);
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
int color = 0xff000000 | red << 16 | green << 8 | blue;
Paint paint = drawable.getPaint(); // new
// Paint(Paint.ANTI_ALIAS_FLAG);
int darkColor = 0xff000000 | red / 4 << 16 | green / 4 << 8 | blue
/ 4;
RadialGradient gradient = new RadialGradient(37.5f, 12.5f, 50f,
color, darkColor, Shader.TileMode.CLAMP);
paint.setShader(gradient);
shapeHolder.setPaint(paint);
balls.add(shapeHolder);
return shapeHolder;
}
private void setUp(final double diameter) {
PtrLocalDisplay.init(mParent.getContext());
final int shadowYOffset = PtrLocalDisplay.dp2px(Y_OFFSET);
final int shadowXOffset = PtrLocalDisplay.dp2px(X_OFFSET);
int mShadowRadius = PtrLocalDisplay.dp2px(SHADOW_RADIUS);
OvalShape oval = new OvalShadow(mShadowRadius, (int) diameter);
mShadow = new ShapeDrawable(oval);
if (Build.VERSION.SDK_INT >= 11) {
mParent.setLayerType(View.LAYER_TYPE_SOFTWARE, mShadow.getPaint());
}
mShadow.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset, KEY_SHADOW_COLOR);
}
public static ShapeDrawable generateBackgroundDrawable(int color) {
Shape shape = new OvalShape();
ShapeDrawable sd = new ShapeDrawable(shape);
Paint paint = sd.getPaint();
paint.setColor(color);
paint.setStyle(Paint.Style.FILL);
return sd;
}
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 LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
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;
}
@Override
public void onBindSwipeViewHolder(RecyclerView.ViewHolder swipeViewHolder, int i) {
SampleViewHolder sampleViewHolder = (SampleViewHolder) swipeViewHolder;
ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(mContext.getResources().getColor(colors[((int) (Math.random() * (colors.length - 1)))]));
sampleViewHolder.avatarView.setBackgroundDrawable(drawable);
sampleViewHolder.textView.setText(mDataset.get(i));
}
public LineChartView(Context context, List<Point> points, LineChartStyle lineChartStyle) {
super(context);
this.points = points;
this.lineChartStyle = lineChartStyle;
paint.setAntiAlias(true);
yLabelDrawable = new ShapeDrawable();
xLabelDrawable = new ShapeDrawable();
chartDrawable = new ShapeDrawable();
updateDrawables();
}
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;
}
/**
* Update the background color of the mBgCircle image view.
*/
public void setBackgroundColorResource(int colorRes) {
if (getBackground() instanceof ShapeDrawable) {
final Resources res = getResources();
((ShapeDrawable) getBackground()).getPaint().setColor(res.getColor(colorRes));
}
}
/**
* @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);
}
private Drawable createInnerStrokesDrawable(final int color, float strokeWidth) {
if (!mStrokeVisible) {
return new ColorDrawable(Color.TRANSPARENT);
}
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
final int bottomStrokeColor = darkenColor(color);
final int bottomStrokeColorHalfTransparent = halfTransparent(bottomStrokeColor);
final int topStrokeColor = lightenColor(color);
final int topStrokeColorHalfTransparent = halfTransparent(topStrokeColor);
final Paint paint = shapeDrawable.getPaint();
paint.setAntiAlias(true);
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Style.STROKE);
shapeDrawable.setShaderFactory(new ShaderFactory() {
@Override
public Shader resize(int width, int height) {
return new LinearGradient(width / 2, 0, width / 2, height,
new int[] { topStrokeColor, topStrokeColorHalfTransparent, color, bottomStrokeColorHalfTransparent, bottomStrokeColor },
new float[] { 0f, 0.2f, 0.5f, 0.8f, 1f },
TileMode.CLAMP
);
}
});
return shapeDrawable;
}
public static ShapeDrawable generateCornerStrokeDrawable(int color, float width, int topLeftCorner, int topRightCorner, int bottomRightCorner, int bottomLeftCorner) {
Shape shape = new RoundRectShape(new float[]{topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner}, null, null);
ShapeDrawable sd = new ShapeDrawable(shape);
sd.getPaint().setColor(color);
sd.getPaint().setStyle(Paint.Style.STROKE);
sd.getPaint().setAntiAlias(true);
sd.getPaint().setStrokeWidth(width);
return sd;
}
public static ShapeDrawable generateCornerShapeDrawable(int color, int topLeftCorner, int topRightCorner, int bottomRightCorner, int bottomLeftCorner) {
Shape shape = new RoundRectShape(new float[]{topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner}, null, null);
ShapeDrawable sd = new ShapeDrawable(shape);
sd.getPaint().setColor(color);
sd.getPaint().setStyle(Paint.Style.FILL);
return sd;
}
public static ShapeDrawable generateCornerStrokeDrawable(int color, float width, int topLeftCorner, int topRightCorner, int bottomRightCorner, int bottomLeftCorner) {
Shape shape = new RoundRectShape(new float[]{topLeftCorner, topLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner}, null, null);
ShapeDrawable sd = new ShapeDrawable(shape);
sd.getPaint().setColor(color);
sd.getPaint().setStyle(Paint.Style.STROKE);
sd.getPaint().setAntiAlias(true);
sd.getPaint().setStrokeWidth(width);
return sd;
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}
private static ShapeDrawable createBackground(int color, float radius){
float[] outerR = new float[] { radius, radius, radius, radius, radius, radius, radius, radius };
RoundRectShape roundRectShape = new RoundRectShape(outerR, null, null);
ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);
shapeDrawable.getPaint().setColor(color);
shapeDrawable.getPaint().setStyle(Paint.Style.FILL);
return shapeDrawable;
}
public void setBackground(float dipRadius, int badgeColor) {
int radius = dip2Px(dipRadius);
float[] radiusArray = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
RoundRectShape roundRect = new RoundRectShape(radiusArray, null, null);
ShapeDrawable bgDrawable = new ShapeDrawable(roundRect);
bgDrawable.getPaint().setColor(badgeColor);
setBackground(bgDrawable);
}
private LayerDrawable createDrawable(int radius, int topColor, int bottomColor) {
float[] outerRadius = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
//Top
RoundRectShape topRoundRect = new RoundRectShape(outerRadius, null, null);
ShapeDrawable topShapeDrawable = new ShapeDrawable(topRoundRect);
topShapeDrawable.getPaint().setColor(topColor);
//Bottom
RoundRectShape roundRectShape = new RoundRectShape(outerRadius, null, null);
ShapeDrawable bottomShapeDrawable = new ShapeDrawable(roundRectShape);
bottomShapeDrawable.getPaint().setColor(bottomColor);
//Create array
Drawable[] drawArray = {bottomShapeDrawable, topShapeDrawable};
LayerDrawable layerDrawable = new LayerDrawable(drawArray);
//Set shadow height
if (isShadowEnabled && topColor != Color.TRANSPARENT) {
//unpressed drawable
layerDrawable.setLayerInset(0, 0, 0, 0, 0); /*index, left, top, right, bottom*/
} else {
//pressed drawable
layerDrawable.setLayerInset(0, 0, mShadowHeight, 0, 0); /*index, left, top, right, bottom*/
}
layerDrawable.setLayerInset(1, 0, 0, 0, mShadowHeight); /*index, left, top, right, bottom*/
return layerDrawable;
}