Java Code Examples for android.graphics.drawable.GradientDrawable#setColors()

The following examples show how to use android.graphics.drawable.GradientDrawable#setColors() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DrawableHelper.java    From monero-wallet-android-app with MIT License 6 votes vote down vote up
/**
 * 创建一张渐变图片,支持韵脚。
 *
 * @param startColor 渐变开始色
 * @param endColor   渐变结束色
 * @param radius     圆角大小
 * @param centerX    渐变中心点 X 轴坐标
 * @param centerY    渐变中心点 Y 轴坐标
 * @return 返回所创建的渐变图片。
 */
@TargetApi(16)
public static GradientDrawable getCircleGradientDrawable(@ColorInt int startColor,
                                                         @ColorInt int endColor, int radius,
                                                         @FloatRange(from = 0f, to = 1f) float centerX,
                                                         @FloatRange(from = 0f, to = 1f) float centerY) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColors(new int[]{
            startColor,
            endColor
    });
    gradientDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
    gradientDrawable.setGradientRadius(radius);
    gradientDrawable.setGradientCenter(centerX, centerY);
    return gradientDrawable;
}
 
Example 2
Source File: DrawableHelper.java    From monero-wallet-android-app with MIT License 6 votes vote down vote up
/**
 * 创建一张圆角矩形的线性渐变图片
 *
 * @param startColor  开始颜色
 * @param endColor    结束颜色
 * @param radius      圆角大小
 * @param orientation 渐变方向
 * @return
 */
public static GradientDrawable getGradientDrawable(@ColorInt int startColor,
                                                   @ColorInt int endColor,
                                                   int radius,
                                                   GradientDrawable.Orientation orientation) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setColors(new int[]{
            startColor,
            endColor
    });
    gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    gradientDrawable.setOrientation(orientation);
    gradientDrawable.setShape(GradientDrawable.RECTANGLE);
    gradientDrawable.setCornerRadius(radius);
    return gradientDrawable;
}
 
Example 3
Source File: ImagePickerActivity.java    From titanium-imagepicker with Apache License 2.0 6 votes vote down vote up
private void drawBackground(View v, int color, boolean isCircle) {
		if (isCircle) {
        GradientDrawable gd = new GradientDrawable();
        gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        gd.setColors(new int[]{color, Color.TRANSPARENT });
        gd.setGradientRadius((Defaults.IMAGE_HEIGHT - Defaults.CIRCLE_PADDING)/2);
        v.setBackground(gd);

    } else {
        ShapeDrawable oval = new ShapeDrawable (new OvalShape());
        oval.getPaint().setColor(color);
        v.setBackground(oval);
    }
}
 
Example 4
Source File: ColoringActivity.java    From coloring with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Updates the color of the color picker selected button with the actual color (a gradient from it).
 */
private void updateColorOfColorPickerButton() {
    View view = findViewById(R.id.colorPickerButton);

    // takes the actually selected color
    int color = Library.getInstance().getSelectedColor();
    int[] gradientColors = ColoringUtils.colorSelectionButtonBackgroundGradient(color);

    if (Build.VERSION.SDK_INT < 16) {
        GradientDrawable newGradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, gradientColors);
        newGradientDrawable.setStroke(1, Color.parseColor("#bbbbbb"));
        newGradientDrawable.setCornerRadius(ColoringActivity.this.getResources().getDimension(R.dimen.color_selection_button_corner_radius));
        //noinspection deprecation
        view.setBackgroundDrawable(newGradientDrawable);
    } else {
        GradientDrawable drawable = (GradientDrawable) view.getBackground();
        drawable.mutate();
        drawable.setColors(gradientColors);
    }
}
 
Example 5
Source File: LLand.java    From heads-up with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onDraw(Canvas c) {
    final int w = c.getWidth();
    final int h = c.getHeight();
    final GradientDrawable g = new GradientDrawable();
    g.setOrientation(GradientDrawable.Orientation.LEFT_RIGHT);
    g.setGradientCenter(w * 0.75f, 0);
    g.setColors(new int[] { 0xFFFFFFFF, 0xFFAAAAAA });
    g.setBounds(0, 0, w, h);
    g.draw(c);
    if (!mDrawShadow) return;
    mShadow.reset();
    mShadow.moveTo(0,0);
    mShadow.lineTo(w, 0);
    mShadow.lineTo(w, PARAMS.OBSTACLE_WIDTH/2+w*1.5f);
    mShadow.lineTo(0, PARAMS.OBSTACLE_WIDTH/2);
    mShadow.close();
    c.drawPath(mShadow, mPaint);
}
 
Example 6
Source File: ImagePickerActivity.java    From titanium-imagepicker with Apache License 2.0 5 votes vote down vote up
private void drawBackground(View v, int color, boolean isCircle) {
	if (isCircle) {
        GradientDrawable gd = new GradientDrawable();
        gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        gd.setColors(new int[]{color, Color.TRANSPARENT });
        gd.setGradientRadius((Defaults.IMAGE_HEIGHT - Defaults.CIRCLE_PADDING)/2);
        v.setBackground(gd);

    } else {
        ShapeDrawable oval = new ShapeDrawable (new OvalShape());
        oval.getPaint().setColor(color);
        v.setBackground(oval);
    }
}
 
Example 7
Source File: BaseRoundCornerProgressBar.java    From RoundCornerProgressBar with Apache License 2.0 5 votes vote down vote up
protected GradientDrawable createGradientDrawable(int[] colors) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.RECTANGLE);
    gradientDrawable.setOrientation(!isReverse() ? GradientDrawable.Orientation.LEFT_RIGHT : GradientDrawable.Orientation.RIGHT_LEFT);
    gradientDrawable.setColors(colors);
    return gradientDrawable;
}
 
Example 8
Source File: BaseRoundCornerProgressBar.java    From RoundCornerProgressBar with Apache License 2.0 5 votes vote down vote up
protected GradientDrawable createGradientDrawable(int[] colors) {
    GradientDrawable gradientDrawable = new GradientDrawable();
    gradientDrawable.setShape(GradientDrawable.RECTANGLE);
    gradientDrawable.setOrientation(!isReverse() ? GradientDrawable.Orientation.LEFT_RIGHT : GradientDrawable.Orientation.RIGHT_LEFT);
    gradientDrawable.setColors(colors);
    return gradientDrawable;
}
 
Example 9
Source File: ColorPickerAdvancedComponent.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Sets the colors for the gradient view to interpolate through.
 *
 * @param newColors The set of colors representing the interpolation points for the gradient.
 */
public void setGradientColors(int[] newColors) {
    mGradientColors = newColors.clone();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        Orientation currentOrientation = Orientation.LEFT_RIGHT;
        mGradientDrawable = new GradientDrawable(currentOrientation, mGradientColors);
    } else {
        mGradientDrawable.setColors(mGradientColors);
    }
    ApiCompatibilityUtils.setBackgroundForView(mGradientView, mGradientDrawable);
}
 
Example 10
Source File: ColorPickerAdvancedComponent.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Sets the colors for the gradient view to interpolate through.
 *
 * @param newColors The set of colors representing the interpolation points for the gradient.
 */
public void setGradientColors(int[] newColors) {
    mGradientColors = newColors.clone();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
        Orientation currentOrientation = Orientation.LEFT_RIGHT;
        mGradientDrawable = new GradientDrawable(currentOrientation, mGradientColors);
    } else {
        mGradientDrawable.setColors(mGradientColors);
    }
    ApiCompatibilityUtils.setBackgroundForView(mGradientView, mGradientDrawable);
}
 
Example 11
Source File: DrawableParser.java    From Folivora with Apache License 2.0 4 votes vote down vote up
@Override
public Drawable parse(ParseRequest request) {
  final Context ctx = request.context();
  final AttributeSet attrs = request.attrs();
  final ShapeAttrs shapeAttrs = request.shapeAttrs();

  GradientDrawable gd = new GradientDrawable();
  TypedArray a = ctx.obtainStyledAttributes(attrs, shapeAttrs.mAttrStyleable);
  gd.setShape(a.getInt(shapeAttrs.mShapeType, GradientDrawable.RECTANGLE));

  final int size = a.getDimensionPixelSize(shapeAttrs.mShapeSolidSize, -1);
  gd.setSize(a.getDimensionPixelSize(shapeAttrs.mShapeSolidWidth, size),
    a.getDimensionPixelSize(shapeAttrs.mShapeSolidHeight, size));

  gd.setGradientType(a.getInt(shapeAttrs.mShapeGradientType, GradientDrawable.LINEAR_GRADIENT));
  gd.setGradientRadius(a.getDimension(shapeAttrs.mShapeGradientRadius, 0));
  gd.setGradientCenter(a.getFloat(shapeAttrs.mShapeGradientCenterX, 0.5F),
    a.getFloat(shapeAttrs.mShapeGradientCenterY, 0.5F));
  int[] gradientColors;
  final int gradientStartColor = a.getColor(shapeAttrs.mShapeGradientStartColor, 0);
  final int gradientEndColor = a.getColor(shapeAttrs.mShapeGradientEndColor, 0);
  final int gradientCenterColor = a.getColor(shapeAttrs.mShapeGradientCenterColor, 0);
  if (a.hasValue(shapeAttrs.mShapeGradientCenterColor)) {
    gradientColors = new int[]{gradientStartColor, gradientCenterColor, gradientEndColor};
  } else {
    gradientColors = new int[]{gradientStartColor, gradientEndColor};
  }
  gd.setColors(gradientColors);
  final int orientationIndex = a.getInt(shapeAttrs.mShapeGradientAngle, 0);
  gd.setOrientation(GradientDrawable.Orientation.values()[orientationIndex]);

  if (a.hasValue(shapeAttrs.mShapeSolidColor)) {
    gd.setColor(a.getColor(shapeAttrs.mShapeSolidColor, Color.WHITE));
  }

  gd.setStroke(
    a.getDimensionPixelSize(shapeAttrs.mShapeStrokeWidth, -1),
    a.getColor(shapeAttrs.mShapeStrokeColor, Color.WHITE),
    a.getDimensionPixelSize(shapeAttrs.mShapeStrokeDashGap, 0),
    a.getDimensionPixelSize(shapeAttrs.mShapeStrokeDashWidth, 0)
  );
  final float radius = a.getDimension(shapeAttrs.mShapeCornerRadius, 0);
  gd.setCornerRadii(new float[]{
    a.getDimension(shapeAttrs.mShapeCornerRadiusTopLeft, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusTopLeft, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusTopRight, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusTopRight, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusBottomRight, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusBottomRight, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusBottomLeft, radius),
    a.getDimension(shapeAttrs.mShapeCornerRadiusBottomLeft, radius),
  });
  a.recycle();
  return gd;
}
 
Example 12
Source File: Drawables.java    From noDrawable with Apache License 2.0 4 votes vote down vote up
public static Drawable create(
        @ShapeMode int shapeMode, @ColorInt Integer solidColor,
        @ColorInt int strokeColor, @DP float strokeWidth, @DP float strokeDash, @DP float strokeDashGap,
        @DP float radius, @DP float radiusLT, @DP float radiusLB, @DP float radiusRT, @DP float radiusRB,
        @ColorInt Integer startColor, @ColorInt Integer centerColor, @ColorInt Integer endColor,
        @Orientation int orientation, @GradientType int gradientType,
        Float radialCenterX, Float radialCenterY, float radialRadius,
        @DP float width, @DP float height,
        @DP float marginLeft, @DP float marginTop, @DP float marginRight, @DP float marginBottom,
        @DP float ringThickness,
        @DP float   ringThicknessRatio,
        @DP float ringInnerRadius,
        @DP float   ringInnerRadiusRatio
) {
    if (shapeMode == INVALID && solidColor == null && strokeColor == INVALID
            && strokeWidth == INVALID && strokeDash == INVALID && strokeDashGap == INVALID
            && radius == INVALID && radiusLT == INVALID && radiusLB == INVALID
            && radiusRT == INVALID && radiusRB == INVALID && startColor == null
            && centerColor == null && endColor == null && orientation == INVALID
            && gradientType == INVALID && radialCenterX == null && radialCenterY == null
            && radialRadius == INVALID && width == INVALID && height == INVALID
            && marginLeft == INVALID && marginTop == INVALID && marginRight == INVALID && marginBottom == INVALID
            ) {
        // 这里需要判断empty,因为有可能只设置了一个state的drawable,那么其他state的就是empty了
        return null;
    }
    GradientDrawable drawable = new GradientDrawable();
    if (startColor != null && endColor != null) {
        int[] colors;
        if (centerColor != null) {
            colors = new int[3];
            colors[0] = startColor;
            colors[1] = centerColor;
            colors[2] = endColor;
        } else {
            colors = new int[2];
            colors[0] = startColor;
            colors[1] = endColor;
        }
        drawable.setColors(colors);
        drawable.setOrientation(mapOrientation(orientation));
        drawable.setGradientType(gradientType);
        if (gradientType == GradientType.RADIAL) {
            drawable.setGradientCenter(radialCenterX == null ? .5f : radialCenterX,
                    radialCenterY == null ? .5f : radialCenterY);
            drawable.setGradientRadius(dip2px(radialRadius));
        }
    } else {
        if (solidColor != null) {
            drawable.setColor(solidColor);
        }
    }
    drawable.setShape(validShapeMode(shapeMode));
    if (shapeMode == ShapeMode.RING) {
        // 由于GradientDrawable中没有ring相关的公开API,所以使用反射,若对性能有要求,请注意。
        setRingValue(drawable, ringThickness, ringThicknessRatio, ringInnerRadius, ringInnerRadiusRatio);
    }
    if (strokeWidth > 0) {
        drawable.setStroke(dip2px(strokeWidth), strokeColor, dip2px(strokeDash), dip2px(strokeDashGap));
    }
    if (radius <= 0) {
        float[] radiusEach = new float[]{dip2px(radiusLT), dip2px(radiusLT), dip2px(radiusRT), dip2px(radiusRT),
                dip2px(radiusRB), dip2px(radiusRB), dip2px(radiusLB), dip2px(radiusLB)};
        drawable.setCornerRadii(radiusEach);
    } else {
        drawable.setCornerRadius(dip2px(radius));
    }
    if (width > 0 && height > 0) {
        // https://stackoverflow.com/a/29180660/4698946
        drawable.setSize(dip2px(width), dip2px(height));
    }
    if (marginLeft != 0 || marginTop != 0 || marginRight != 0 || marginBottom != 0) {
        return new InsetDrawable(drawable,
                dip2px(marginLeft),
                dip2px(marginTop),
                dip2px(marginRight),
                dip2px(marginBottom));
    } else {
        return drawable;
    }
}