Java Code Examples for android.graphics.drawable.ShapeDrawable#setShape()

The following examples show how to use android.graphics.drawable.ShapeDrawable#setShape() . 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 android-round-textview with Apache License 2.0 6 votes vote down vote up
public static Drawable getCornerDrawable(float topLeft,
                                         float topRight,
                                         float bottomLeft,
                                         float bottomRight,
                                          @ColorInt int color) {

    float[] outerR = new float[8];
    outerR[0] = topLeft;
    outerR[1] = topLeft;
    outerR[2] = topRight;
    outerR[3] = topRight;
    outerR[4] = bottomRight;
    outerR[5] = bottomRight;
    outerR[6] = bottomLeft;
    outerR[7] = bottomLeft;

    ShapeDrawable drawable = new ShapeDrawable();
    drawable.setShape(new RoundRectShape(outerR, null, null));
    drawable.getPaint().setColor(color);

    return drawable;
}
 
Example 2
Source File: DefaultHeaderTransformer.java    From Klyph with MIT License 6 votes vote down vote up
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);
    }
}
 
Example 3
Source File: DefaultHeaderTransformer.java    From KlyphMessenger with MIT License 6 votes vote down vote up
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);
    }
}
 
Example 4
Source File: OrderDialogFragment.java    From From-design-to-Android-part1 with Apache License 2.0 6 votes vote down vote up
private Drawable createProductImageDrawable(Product product) {
    final ShapeDrawable background = new ShapeDrawable();
    background.setShape(new OvalShape());
    background.getPaint().setColor(ContextCompat.getColor(getContext(), product.color));

    final BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(),
        BitmapFactory.decodeResource(getResources(), product.image));

    final LayerDrawable layerDrawable = new LayerDrawable
        (new Drawable[]{background, bitmapDrawable});

    final int padding = (int) getResources().getDimension(R.dimen.spacing_huge);
    layerDrawable.setLayerInset(1, padding, padding, padding, padding);

    return layerDrawable;
}
 
Example 5
Source File: BaseMessageDialog.java    From Leanplum-Android-SDK with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private ImageView createBackgroundImageView(Context context, boolean fullscreen) {
  BackgroundImageView view = new BackgroundImageView(context, fullscreen);
  view.setScaleType(ImageView.ScaleType.CENTER_CROP);
  int cornerRadius;
  if (!fullscreen) {
    cornerRadius = SizeUtil.dp20;
  } else {
    cornerRadius = 0;
  }
  view.setImageBitmap(options.getBackgroundImage());
  ShapeDrawable footerBackground = new ShapeDrawable();
  footerBackground.setShape(createRoundRect(cornerRadius));
  footerBackground.getPaint().setColor(options.getBackgroundColor());
  if (Build.VERSION.SDK_INT >= 16) {
    view.setBackground(footerBackground);
  } else {
    view.setBackgroundDrawable(footerBackground);
  }
  RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
      LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  view.setLayoutParams(layoutParams);
  return view;
}
 
Example 6
Source File: BitmapUtil.java    From Leanplum-Android-SDK with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 7
Source File: ZProgressBar.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 根据 radius 和 color 来创建 ShapeDrawable
 * @param radius 弧度
 * @param color  颜色
 * @return drawable
 */
private Drawable createShape(float radius, int color){
    ShapeDrawable shape = new ShapeDrawable();
    // 设置弧度
    radius = dp2px(radius);
    float[] outerRadii = new float[]{radius, radius, radius, radius, radius, radius, radius, radius};
    RoundRectShape roundShape = new RoundRectShape(outerRadii, null, null);
    shape.setShape(roundShape);
    // 设置颜色
    shape.getPaint().setColor(color);
    return shape;
}
 
Example 8
Source File: SweetToast.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
 * @param backgroundColor
 * @return
 */
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
    try {
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        //获取当前设备的屏幕尺寸
        //实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
        int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
        int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
        float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
        if(widthPixels >= 1070){
            //例如小米5S:1920 x 1080
            shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
        }else {
            //例如红米2:1280x720
            shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
        }
        float radius = density*8;
        float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        int width = sweetToast.getContentView().getWidth();
        int height = sweetToast.getContentView().getHeight();
        RectF rectF = new RectF(1,1,width-1,height-1);
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
        shapeDrawable.setShape(roundRectShape);
        //在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);

        //解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
        //经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
        shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
        return shapeDrawable;
    }catch (Exception e){
        Log.e("幻海流心","e:"+e.getLocalizedMessage());
    }
    return null;
}
 
Example 9
Source File: ProgressBarWrapper.java    From Man-Man with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: SweetToast.java    From SweetTips with Apache License 2.0 5 votes vote down vote up
/**
 * 根据指定的背景色,获得当前SweetToast实例中mContentView的背景drawable实例
 * @param backgroundColor
 * @return
 */
private static Drawable getBackgroundDrawable(SweetToast sweetToast, @ColorInt int backgroundColor){
    try {
        ShapeDrawable shapeDrawable = new ShapeDrawable();
        //获取当前设备的屏幕尺寸
        //实验发现不同的设备上面,Toast内容区域的padding值并不相同,根据屏幕的宽度分别进行处理,尽量接近设备原生Toast的体验
        int widthPixels = sweetToast.getContentView().getResources().getDisplayMetrics().widthPixels;
        int heightPixels = sweetToast.getContentView().getResources().getDisplayMetrics().heightPixels;
        float density = sweetToast.getContentView().getResources().getDisplayMetrics().density;
        if(widthPixels >= 1070){
            //例如小米5S:1920 x 1080
            shapeDrawable.setPadding((int)(density*13),(int)(density*12),(int)(density*13),(int)(density*12));
        }else {
            //例如红米2:1280x720
            shapeDrawable.setPadding((int)(density*14),(int)(density*13),(int)(density*14),(int)(density*13));
        }
        float radius = density*8;
        float[] outerRadii = new float[]{radius,radius,radius,radius,radius,radius,radius,radius};
        int width = sweetToast.getContentView().getWidth();
        int height = sweetToast.getContentView().getHeight();
        RectF rectF = new RectF(1,1,width-1,height-1);
        RoundRectShape roundRectShape = new RoundRectShape(outerRadii,rectF,null);
        shapeDrawable.setShape(roundRectShape);
        //在Android 5.0以下,直接设置 DrawableCompat.setTint 未变色:DrawableCompat.setTint(shapeDrawable,backgroundColor);

        //解决:不使用DrawableCompat,直接使用 Drawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP),
        //经测试,在4.22(山寨机)和5.1(中兴)和6.0.1(小米5s)上颜色正常显示
        shapeDrawable.setColorFilter(backgroundColor, PorterDuff.Mode.SRC_ATOP);
        return shapeDrawable;
    }catch (Exception e){
        Log.e("幻海流心","e:"+e.getLocalizedMessage());
    }
    return null;
}
 
Example 11
Source File: DefaultHeaderTransformer.java    From AndroidPullMenu with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 12
Source File: ThreadViewFragment.java    From something.apk with MIT License 5 votes vote down vote up
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);
}
 
Example 13
Source File: LiveButton.java    From LiveButton with Apache License 2.0 5 votes vote down vote up
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;
	}
 
Example 14
Source File: DefaultHeaderTransformer.java    From Bitocle with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 15
Source File: DefaultHeaderTransformer.java    From Bitocle with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 16
Source File: LineChartView.java    From line-chart-view with MIT License 5 votes vote down vote up
protected void drawYLabels(ShapeDrawable labelDrawable) {
    Shape labelsShape = new Shape() {
        @Override
        public void draw(Canvas canvas, Paint paint) {
            labelPaint.setTextAlign(Paint.Align.RIGHT);
            labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
            labelPaint.setColor(lineChartStyle.getLabelTextColor());

            long minY = getMinY();
            long maxY = getMaxY();
            long yrange = maxY - minY;

            float height = getHeight();

            float left = getYLabelWidth();
            List<Long> yLabels = getYLabels();
            for (long y : yLabels) {
                String label = formatYLabel(y);
                float yCoordinate = getYCoordinate(height, y, minY, yrange);
                canvas.drawText(label, left, yCoordinate, labelPaint);
            }
        }
    };
    measureYLabel();
    labelDrawable.setBounds(0, 0, getWidth(), getHeight());
    labelDrawable.setShape(labelsShape);
}
 
Example 17
Source File: LineChartView.java    From line-chart-view with MIT License 5 votes vote down vote up
protected void drawXLabels(ShapeDrawable labelDrawable) {
    Shape labelsShape = new Shape() {
        @Override
        public void draw(Canvas canvas, Paint paint) {
            labelPaint.setTextAlign(Paint.Align.CENTER);
            labelPaint.setTextSize(lineChartStyle.getLabelTextSize());
            labelPaint.setColor(lineChartStyle.getLabelTextColor());

            long minX = getMinX();
            long maxX = getMaxX();
            long xrange = maxX - minX;

            float width = getWidth();
            float height = getHeight();

            float labelHeight = height - lineChartStyle.getXLabelMargin();
            Rect textBounds = new Rect();
            List<Long> xLabels = getXLabels();
            for (long x : xLabels) {
                String label = formatXLabel(x);
                labelPaint.getTextBounds(label, 0, label.length(), textBounds);
                float xCoordinate = getXCoordinate(width, x, minX, xrange);
                canvas.drawText(label, xCoordinate, labelHeight, labelPaint);
            }
        }
    };
    measureXLabel();
    labelDrawable.setBounds(0, 0, getWidth(), getHeight());
    labelDrawable.setShape(labelsShape);
}
 
Example 18
Source File: LineChartView.java    From line-chart-view with MIT License 5 votes vote down vote up
protected void drawLineChart(ShapeDrawable chartDrawable) {
    Shape chartShape = new Shape() {
        @Override
        public void draw(Canvas canvas, Paint paint) {
            long minX = getMinX();
            long maxX = getMaxX();
            long xrange = maxX - minX;

            long minY = getMinY();
            long maxY = getMaxY();
            long yrange = maxY - minY;

            float width = getWidth();
            float height = getHeight();
            float left = getChartLeftMargin();
            float top = getChartTopMargin();
            float right = width - getChartRightMargin();
            float bottom = height - getChartBottomMargin();

            drawChartFrame(canvas, left, top, right, bottom);

            drawXGrid(canvas, minX, xrange);
            drawYGrid(canvas, minY, yrange);

            List<LineChartStyle.Border> borders = lineChartStyle.getBorders();
            for (LineChartStyle.Border border : borders) {
                drawChartBorder(canvas, border, left, top, right, bottom);
            }

            drawLines(canvas, minX, xrange, minY, yrange);

            if (lineChartStyle.isDrawPoint()) {
                drawPoints(canvas, minX, xrange, minY, yrange);
            }
        }
    };
    chartDrawable.setBounds(0, 0, getWidth(), getHeight());
    chartDrawable.setShape(chartShape);
}
 
Example 19
Source File: TextPointView.java    From jianshi with Apache License 2.0 4 votes vote down vote up
public void setCircleBackgroundColor(@ColorRes int circleColorRes) {
  ShapeDrawable circleShapeDrawable = new ShapeDrawable();
  circleShapeDrawable.setShape(new OvalShape());
  circleShapeDrawable.getPaint().setColor(ContextCompat.getColor(context, circleColorRes));
  circleView.setBackgroundDrawable(circleShapeDrawable);
}
 
Example 20
Source File: ShadowUtils.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
public static Drawable generateBackgroundWithShadow(View view, @ColorRes int backgroundColor,
                                                    @DimenRes int cornerRadius,
                                                    @ColorRes int shadowColor,
                                                    @DimenRes int elevation,
                                                    int shadowGravity) {

  float cornerRadiusValue = view.getContext().getResources().getDimension(cornerRadius);
  int elevationValue = (int) view.getContext().getResources().getDimension(elevation);
  int shadowColorValue = ContextCompat.getColor(view.getContext(), shadowColor);
  int backgroundColorValue = ContextCompat.getColor(view.getContext(), backgroundColor);

  float[] outerRadius = {cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
    cornerRadiusValue, cornerRadiusValue, cornerRadiusValue, cornerRadiusValue,
    cornerRadiusValue};

  Rect shapeDrawablePadding = new Rect();
  shapeDrawablePadding.left = 0;
  shapeDrawablePadding.right = 0;

  int DY = 0;
  int DX = 0;

  switch (shadowGravity) {
    case Gravity.CENTER:
      shapeDrawablePadding.top = elevationValue;
      shapeDrawablePadding.bottom = elevationValue;
      DY = 0;
      break;

    case Gravity.RIGHT:
      shapeDrawablePadding.right = elevationValue * 2;
      shapeDrawablePadding.bottom = elevationValue * 2;
      DY = elevationValue / 3;
      DX = elevationValue / 3;
      break;

    case Gravity.TOP:
      shapeDrawablePadding.top = elevationValue * 2;
      shapeDrawablePadding.bottom = elevationValue;
      DY = -1 * elevationValue / 3;
      break;
    case Gravity.BOTTOM:
      shapeDrawablePadding.top = elevationValue;
      shapeDrawablePadding.bottom = elevationValue * 2;
      DY = elevationValue / 3;
      break;
  }

  ShapeDrawable shapeDrawable = new ShapeDrawable();
  shapeDrawable.setPadding(shapeDrawablePadding);

  shapeDrawable.getPaint().setColor(backgroundColorValue);
  shapeDrawable.getPaint().setShadowLayer(cornerRadiusValue / 3, DX, DY, shadowColorValue);

  view.setLayerType(LAYER_TYPE_SOFTWARE, shapeDrawable.getPaint());

  shapeDrawable.setShape(new RoundRectShape(outerRadius, null, null));

  LayerDrawable drawable = new LayerDrawable(new Drawable[]{shapeDrawable});
  drawable.setLayerInset(0, 0, 0, 0, elevationValue * 2);

  return drawable;

}