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

The following examples show how to use android.graphics.drawable.GradientDrawable#setGradientRadius() . 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: 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 3
Source File: GradientDrawableInflateImpl.java    From timecat with Apache License 2.0 5 votes vote down vote up
void setGradientRadius(Context context, AttributeSet attrs, GradientDrawable drawable, int gradientType) throws XmlPullParserException {
    TypedArray a = DrawableUtils.obtainAttributes(context.getResources(), context.getTheme(), attrs, new int[]{android.R.attr.gradientRadius});
    TypedValue value = a.peekValue(0);
    if (value != null) {
        boolean radiusRel = value.type == TypedValue.TYPE_FRACTION;
        drawable.setGradientRadius(radiusRel ? value.getFraction(1.0f, 1.0f) : value.getFloat());
    } else if (gradientType == GradientDrawable.RADIAL_GRADIENT) {
        throw new XmlPullParserException("<gradient> tag requires 'gradientRadius' " + "attribute with radial type");
    }
    a.recycle();
}
 
Example 4
Source File: NoInternetDialog.java    From NoInternetDialog with Apache License 2.0 5 votes vote down vote up
private void initBackground() {
    GradientDrawable.Orientation orientation = getOrientation();

    GradientDrawable drawable = new GradientDrawable(orientation, new int[]{bgGradientStart, bgGradientCenter, bgGradientEnd});
    drawable.setShape(GradientDrawable.RECTANGLE);
    drawable.setCornerRadius(dialogRadius);

    switch (bgGradientType) {
        case GRADIENT_RADIAL:
            drawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
            break;
        case GRADIENT_SWEEP:
            drawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);
            break;
        default:
            drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
            break;
    }

    if (isHalloween) {
        drawable.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        drawable.setGradientRadius(getContext().getResources().getDimensionPixelSize(R.dimen.dialog_height) / 2);
    } else {
        drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        root.setBackground(drawable);
    } else {
        root.setBackgroundDrawable(drawable);
    }
}
 
Example 5
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 6
Source File: GradientDrawableInflateImpl.java    From MagicaSakura with Apache License 2.0 5 votes vote down vote up
void setGradientRadius(Context context, AttributeSet attrs, GradientDrawable drawable, int gradientType) throws XmlPullParserException {
    TypedArray a = DrawableUtils.obtainAttributes(context.getResources(), context.getTheme(), attrs, new int[]{android.R.attr.gradientRadius});
    TypedValue value = a.peekValue(0);
    if (value != null) {
        boolean radiusRel = value.type == TypedValue.TYPE_FRACTION;
        drawable.setGradientRadius(radiusRel ? value.getFraction(1.0f, 1.0f) : value.getFloat());
    } else if (gradientType == GradientDrawable.RADIAL_GRADIENT) {
        throw new XmlPullParserException(
                "<gradient> tag requires 'gradientRadius' "
                        + "attribute with radial type");
    }
    a.recycle();
}
 
Example 7
Source File: DrawableValue.java    From proteus with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(ProteusView view, final GradientDrawable drawable) {
  if (null != centerX && null != centerY) {
    drawable.setGradientCenter(centerX, centerY);
  }

  if (null != gradientRadius) {
    drawable.setGradientRadius(DimensionAttributeProcessor.evaluate(gradientRadius, view));
  }

  if (GRADIENT_TYPE_NONE != gradientType) {
    drawable.setGradientType(gradientType);
  }
}
 
Example 8
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 9
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;
    }
}
 
Example 10
Source File: MeasurementView.java    From openScale with GNU General Public License v3.0 4 votes vote down vote up
private void initView(Context context) {
    measurementRow = new TableRow(context);

    iconView = new ImageView(context);
    iconViewBackground = new GradientDrawable();
    nameView = new TextView(context);
    valueView = new TextView(context);
    editModeView = new ImageView(context);
    indicatorView = new ImageView(context);

    evaluatorRow = new TableRow(context);
    evaluatorView = new LinearGaugeView(context);

    incDecLayout = new LinearLayout(context);

    measurementRow.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 1.0f));
    measurementRow.setGravity(Gravity.CENTER);
    measurementRow.addView(iconView);
    measurementRow.addView(nameView);
    measurementRow.addView(valueView);
    measurementRow.addView(incDecLayout);
    measurementRow.addView(editModeView);
    measurementRow.addView(indicatorView);

    addView(measurementRow);
    addView(evaluatorRow);

    iconViewBackground.setColor(ColorUtil.COLOR_GRAY);
    iconViewBackground.setShape(GradientDrawable.OVAL);
    iconViewBackground.setGradientRadius(iconView.getWidth());

    iconView.setImageResource(iconId);
    iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    iconView.setPadding(25,25,25,25);

    iconView.setColorFilter(ColorUtil.COLOR_BLACK);
    iconView.setBackground(iconViewBackground);

    TableRow.LayoutParams iconLayout = new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    iconLayout.setMargins(10, 5, 10, 5);
    iconView.setLayoutParams(iconLayout);

    nameView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
    nameView.setLines(2);
    nameView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.55f));

    valueView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
    valueView.setGravity(Gravity.RIGHT | Gravity.CENTER);
    valueView.setPadding(0,0,20,0);
    valueView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.29f));

    incDecLayout.setOrientation(VERTICAL);
    incDecLayout.setVisibility(View.GONE);
    incDecLayout.setPadding(0,0,0,0);
    incDecLayout.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.05f));

    editModeView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_editable));
    editModeView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    editModeView.setVisibility(View.GONE);
    editModeView.setColorFilter(getForegroundColor());

    indicatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 0.01f));
    indicatorView.setBackgroundColor(Color.GRAY);

    evaluatorRow.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, 1.0f));
    evaluatorRow.addView(new Space(context));
    evaluatorRow.addView(evaluatorView);
    Space spaceAfterEvaluatorView = new Space(context);
    evaluatorRow.addView(spaceAfterEvaluatorView);
    evaluatorRow.setVisibility(View.GONE);

    evaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.99f));
    spaceAfterEvaluatorView.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.01f));

    setOnClickListener(new onClickListenerEvaluation());
}