public static Bitmap roundBitmap(Bitmap bitmap, int i, int i2, float f, float f2, float f3, float f4) throws Throwable { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Rect rect = new Rect(0, 0, width, height); Bitmap createBitmap = (width == i && height == i2) ? Bitmap.createBitmap(bitmap.getWidth (), bitmap.getHeight(), Config.ARGB_8888) : Bitmap.createBitmap(i, i2, Config .ARGB_8888); Canvas canvas = new Canvas(createBitmap); Paint paint = new Paint(); Rect rect2 = new Rect(0, 0, i, i2); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(-12434878); float[] fArr = new float[]{f, f, f2, f2, f3, f3, f4, f4}; ShapeDrawable shapeDrawable = new ShapeDrawable(new RoundRectShape(fArr, new RectF(0.0f, 0.0f, 0.0f, 0.0f), fArr)); shapeDrawable.setBounds(rect2); shapeDrawable.draw(canvas); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect2, paint); return createBitmap; }
public void select(int dayOfOrder) { this.selected = dayOfOrder; resetSelect(); if (this.mSelectListener != null) { this.mSelectListener.onSelect(dayOfOrder); } TextView tv = (TextView) this.tvList.get(dayOfOrder); tv.setTextColor(getResources().getColor(R.color.ju)); ShapeDrawable oval = new ShapeDrawable(new OvalShape()); if (dayOfOrder == this.orderOfToday) { oval.getPaint().setColor(getResources().getColor(R.color.he)); } else { oval.getPaint().setColor(getResources().getColor(R.color.hb)); } tv.setBackgroundDrawable(oval); }
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; }
/** * Adds an arrow view pointing at the original icon. * @param horizontalOffset the horizontal offset of the arrow, so that it * points at the center of the original icon */ private View addArrowView(int horizontalOffset, int verticalOffset, int width, int height) { LinearLayout.LayoutParams layoutParams = new LayoutParams(width, height); if (mIsLeftAligned) { layoutParams.gravity = Gravity.LEFT; layoutParams.leftMargin = horizontalOffset; } else { layoutParams.gravity = Gravity.RIGHT; layoutParams.rightMargin = horizontalOffset; } if (mIsAboveIcon) { layoutParams.topMargin = verticalOffset; } else { layoutParams.bottomMargin = verticalOffset; } View arrowView = new View(getContext()); ShapeDrawable arrowDrawable = new ShapeDrawable(TriangleShape.create( width, height, !mIsAboveIcon)); arrowDrawable.getPaint().setColor(Color.WHITE); arrowView.setBackground(arrowDrawable); arrowView.setElevation(getElevation()); addView(arrowView, mIsAboveIcon ? getChildCount() : 0, layoutParams); return arrowView; }
/** * Adds an arrow view pointing at the original icon. * @param horizontalOffset the horizontal offset of the arrow, so that it * points at the center of the original icon */ private View addArrowView(int horizontalOffset, int verticalOffset, int width, int height) { LayoutParams layoutParams = new LayoutParams(width, height); if (mIsLeftAligned) { layoutParams.gravity = Gravity.START; layoutParams.leftMargin = horizontalOffset; } else { layoutParams.gravity = Gravity.END; layoutParams.rightMargin = horizontalOffset; } if (mIsAboveIcon) { layoutParams.topMargin = verticalOffset; } else { layoutParams.bottomMargin = verticalOffset; } View arrowView = new View(getContext()); if (Gravity.isVertical(((FrameLayout.LayoutParams) getLayoutParams()).gravity)) { // This is only true if there wasn't room for the container next to the icon, // so we centered it instead. In that case we don't want to show the arrow. arrowView.setVisibility(INVISIBLE); } else { ShapeDrawable arrowDrawable = new ShapeDrawable(TriangleShape.create( width, height, !mIsAboveIcon)); Paint arrowPaint = arrowDrawable.getPaint(); // Note that we have to use getChildAt() instead of getItemViewAt(), // since the latter expects the arrow which hasn't been added yet. PopupItemView itemAttachedToArrow = (PopupItemView) (getChildAt(mIsAboveIcon ? getChildCount() - 1 : 0)); arrowPaint.setColor(itemAttachedToArrow.getArrowColor(mIsAboveIcon)); // The corner path effect won't be reflected in the shadow, but shouldn't be noticeable. int radius = getResources().getDimensionPixelSize(R.dimen.popup_arrow_corner_radius); arrowPaint.setPathEffect(new CornerPathEffect(radius)); arrowView.setBackground(arrowDrawable); arrowView.setElevation(getElevation()); } addView(arrowView, mIsAboveIcon ? getChildCount() : 0, layoutParams); return arrowView; }
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; }
/** * 动态创建带上分隔线或下分隔线的Drawable * * @param separatorColor * @param bgColor * @param top * @return */ public static LayerDrawable createItemSeparatorBg(@ColorInt int separatorColor, @ColorInt int bgColor, int separatorHeight, boolean top) { ShapeDrawable separator = new ShapeDrawable(); separator.getPaint().setStyle(Paint.Style.FILL); separator.getPaint().setColor(separatorColor); ShapeDrawable bg = new ShapeDrawable(); bg.getPaint().setStyle(Paint.Style.FILL); bg.getPaint().setColor(bgColor); Drawable[] layers = {separator, bg}; LayerDrawable layerDrawable = new LayerDrawable(layers); layerDrawable.setLayerInset(1, 0, top ? separatorHeight : 0, 0, top ? 0 : separatorHeight); return layerDrawable; }
private Drawable createRectDrawable(int color) { RoundRectShape shape = new RoundRectShape( new float[]{ mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius }, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(shape); shapeDrawable.getPaint().setColor(color); return shapeDrawable; }
/** * 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 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; }
@Override public Drawable createShadowShapeDrawable(Context context, final CircleLoadingView circleLoadingView, int shadowColor) { final float density = context.getResources().getDisplayMetrics().density; ShapeDrawable circle = new ShapeDrawable(new OvalShape()); circle.getPaint().setColor(shadowColor); final float elevation = SHADOW_ELEVATION * density; circleLoadingView.setElevation(elevation); circleLoadingView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { ViewParent p = circleLoadingView.getParent(); if(p instanceof ViewGroup) { final int margin = (int) elevation; ViewGroup.LayoutParams params = circleLoadingView.getLayoutParams(); if(params instanceof ViewGroup.MarginLayoutParams){ ((ViewGroup.MarginLayoutParams) params).setMargins(margin, margin, margin, margin); } } circleLoadingView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); return circle; }
@Override public Drawable createShadowShapeDrawable(Context context, CircleLoadingView circleLoadingView, int shadowColor) { final float density = context.getResources().getDisplayMetrics().density; mShadowRadius = (int) (density * SHADOW_RADIUS); final int diameter = (int) (RADIUS * density * 2); final int shadowYOffset = (int) (density * Y_OFFSET); final int shadowXOffset = (int) (density * X_OFFSET); OvalShape oval = new OvalShadow(mShadowRadius, diameter); ShapeDrawable circle = new ShapeDrawable(oval); ViewCompat.setLayerType(circleLoadingView, ViewCompat.LAYER_TYPE_SOFTWARE, circle.getPaint()); circle.getPaint().setShadowLayer(mShadowRadius, shadowXOffset, shadowYOffset, KEY_SHADOW_COLOR); final int padding = mShadowRadius; // set padding so the inner image sits correctly within the shadow. circleLoadingView.setPadding(padding, padding, padding, padding); return circle; }
/** * 动态创建带上分隔线或下分隔线的Drawable。 * * @param separatorColor 分割线颜色。 * @param bgColor Drawable 的背景色。 * @param top true 则分割线为上分割线,false 则为下分割线。 * @return 返回所创建的 Drawable。 */ public static LayerDrawable createItemSeparatorBg(@ColorInt int separatorColor, @ColorInt int bgColor, int separatorHeight, boolean top) { ShapeDrawable separator = new ShapeDrawable(); separator.getPaint().setStyle(Paint.Style.FILL); separator.getPaint().setColor(separatorColor); ShapeDrawable bg = new ShapeDrawable(); bg.getPaint().setStyle(Paint.Style.FILL); bg.getPaint().setColor(bgColor); Drawable[] layers = {separator, bg}; LayerDrawable layerDrawable = new LayerDrawable(layers); layerDrawable.setLayerInset(1, 0, top ? separatorHeight : 0, 0, top ? 0 : separatorHeight); return layerDrawable; }
public MagnifierView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.pic)).getBitmap(); scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth() * FACTOR, bitmap.getHeight() * FACTOR, true); // bitmap.recycle(); mBitmapShader = new BitmapShader(scaledBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mShapeDrawable = new ShapeDrawable(new OvalShape()); mShapeDrawable.setBounds(0, 0, WIDTH, WIDTH); mShapeDrawable.getPaint().setShader(mBitmapShader); mMatrix = new Matrix(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(Color.RED); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(3); }
void setCornerRadius(View view, int radius, int color) { radius = _DP(radius); int borderWidth = 0;// 加边框后会出现空心圆角矩形的效果,所以设置为0 float[] outerRadius = new float[8]; float[] innerRadius = new float[8]; for (int i = 0; i < 8; i++) { outerRadius[i] = radius + borderWidth; innerRadius[i] = radius; } ShapeDrawable shapeDrawable = // 创建图形drawable new ShapeDrawable( // 创建圆角矩形 new RoundRectShape(outerRadius, new RectF(borderWidth, borderWidth, borderWidth, borderWidth), innerRadius)); shapeDrawable.getPaint().setColor(color);// 使用指定的颜色绘制,即背景颜色 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { // 高版本SDK使用新的API view.setBackground(shapeDrawable); } else { view.setBackgroundDrawable(shapeDrawable); } }
@Override public void onBindViewHolder(MyViewHolder holder, int position) { RestTest restTest = datas.get(position); RoundRectShape shape = new RoundRectShape(new float[]{8, 8, 8, 8, 8, 8, 8, 8}, null, null); ShapeDrawable drawable = new ShapeDrawable(shape); String color = "6bbd5b"; switch (restTest.getMethod().toUpperCase()) { case "GET": break; case "POST": color = "248fb2"; break; case "DELETE": color = "e27a7a"; break; case "PUT": color = "9b708b"; break; } drawable.getPaint().setColor(Color.parseColor("#" + color)); holder.tv.setBackground(drawable); holder.tv.setText(restTest.getMethod().toUpperCase()); holder.spec.setText(restTest.getUrl()); }
private void setColor(Drawable drawable, int color) { if (drawable instanceof ShapeDrawable) { ((ShapeDrawable) drawable).getPaint().setColor(color); } else if (drawable instanceof GradientDrawable) { ((GradientDrawable) drawable).setColor(color); } else if (drawable instanceof ColorDrawable) { ((ColorDrawable) drawable).setColor(color); } else if (drawable instanceof LayerDrawable) { LayerDrawable layerDrawable = (LayerDrawable) drawable; RotateDrawable rotateDrawable = (RotateDrawable) layerDrawable.findDrawableByLayerId(R.id.carrot_shape_top); setColor(rotateDrawable.getDrawable(), color); } else if (drawable instanceof RotateDrawable) { setColor(((RotateDrawable) drawable).getDrawable(), color); } }
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; }
@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; }
private static Bitmap getCircleIcon( Context context, @ColorInt int backgroundColor, int backgroundInset, @DrawableRes int iconResId, @ColorInt int iconColor, int iconInset) { Drawable[] layers = new Drawable[2]; ShapeDrawable background = new ShapeDrawable(new OvalShape()); background.getPaint().setColor(backgroundColor); Drawable icon = ContextCompat.getDrawable(context, iconResId); Drawable tintedIcon = DrawableCompat.wrap(icon.mutate()); DrawableCompat.setTint(tintedIcon, iconColor); layers[0] = background; layers[1] = tintedIcon; LayerDrawable layerDrawable = new LayerDrawable(layers); layerDrawable.setLayerInset(1, iconInset, iconInset, iconInset, iconInset); layerDrawable.setLayerInset(0, backgroundInset, backgroundInset, backgroundInset, backgroundInset); return drawableToBitmap(layerDrawable); }
private StateListDrawable createDefaultOverButtonBgDrawable() { int dp12 = (int) ThemeUtils.applyDimensionDp(this, 12.f); int dp8 = (int) ThemeUtils.applyDimensionDp(this, 8.f); float dp4 = ThemeUtils.applyDimensionDp(this, 4.f); float[] round = new float[]{dp4, dp4, dp4, dp4, dp4, dp4, dp4, dp4}; ShapeDrawable pressedDrawable = new ShapeDrawable(new RoundRectShape(round, null, null)); pressedDrawable.setPadding(dp12, dp8, dp12, dp8); int pressedColor = ThemeUtils.resolveColor(this, R.attr.gallery_toolbar_over_button_pressed_color, R.color.gallery_default_toolbar_over_button_pressed_color); pressedDrawable.getPaint().setColor(pressedColor); int normalColor = ThemeUtils.resolveColor(this, R.attr.gallery_toolbar_over_button_normal_color, R.color.gallery_default_toolbar_over_button_normal_color); ShapeDrawable normalDrawable = new ShapeDrawable(new RoundRectShape(round, null, null)); normalDrawable.setPadding(dp12, dp8, dp12, dp8); normalDrawable.getPaint().setColor(normalColor); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable); stateListDrawable.addState(new int[]{}, normalDrawable); return stateListDrawable; }
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); }
/** * Update the background color of the mBgCircle image view. */ public void setBackgroundColor(int colorRes) { if (getBackground() instanceof ShapeDrawable) { final Resources res = getResources(); ((ShapeDrawable) getBackground()).getPaint().setColor(res.getColor(colorRes)); } }
private static Drawable getRippleMask(@ColorInt int color) { float[] outerRadii = new float[8]; // 3 is the radius of final ripple, instead of 3 we can give required final radius Arrays.fill(outerRadii, 3); RoundRectShape r = new RoundRectShape(outerRadii, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(r); shapeDrawable.getPaint().setColor(color); return shapeDrawable; }
private void change() { float[] outerR = new float[]{mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2, mSize / 2}; Shape shape = new RoundRectShape(outerR, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(shape); shapeDrawable.setIntrinsicHeight((int) mSize); shapeDrawable.setIntrinsicWidth((int) mSize); shapeDrawable.setPadding(0, 0, 0, 0); shapeDrawable.getPaint().setColor(mColor); shapeDrawable.getPaint().setStyle(Paint.Style.FILL); setBackgroundDrawable(shapeDrawable); setHeight((int) mSize); setMinWidth((int) mSize); }
/** * 位图图像渲染 * @param canvas */ private void studyBitmapShader(Canvas canvas) { //创建BitmapShader //其中TileMode.MIRROR x方向镜像 TileMode.REPEAT y方向重复 mBitmapShader = new BitmapShader(mBitMap, Shader.TileMode.MIRROR, Shader.TileMode.REPEAT); //创建矩阵 来缩放图片 Matrix matrix = new Matrix(); matrix.setScale(0.2f,0.2f); mBitmapShader.setLocalMatrix(matrix); mPaint.setAntiAlias(true); mPaint.setShader(mBitmapShader); //画圆形的图片 // canvas.drawCircle(mBitMap.getWidth()/2,mBitMap.getHeight()/2,300,mPaint); //画矩形的图片 // Rect rect = new Rect(0,0,600,600); // canvas.drawRect(rect,mPaint); // //画椭圆的图片 // RectF rectf = new RectF(0,0,400,300); // // canvas.drawOval(rectf,mPaint); //用shapeDrawable 实现圆形图片 ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape()); shapeDrawable.getPaint().setShader(mBitmapShader); shapeDrawable.setBounds(0,0,400,400); shapeDrawable.draw(canvas); }
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); }
private static Drawable getRippleMask(int color) { float[] outerRadii = new float[8]; // 3 is radius of final ripple, // instead of 3 you can give required final radius Arrays.fill(outerRadii, 3); RoundRectShape r = new RoundRectShape(outerRadii, null, null); ShapeDrawable shapeDrawable = new ShapeDrawable(r); shapeDrawable.getPaint().setColor(color); return shapeDrawable; }
private Drawable getPressedEffectBeforeLollipop(CORNER_POSITION cornerPosition, int color) { ShapeDrawable colorDrawablePressed = getCornerStateDrawable(cornerPosition); colorDrawablePressed.getPaint().setColor(color); colorDrawablePressed.getPaint().setStyle(Paint.Style.FILL); colorDrawablePressed.getPaint().setAlpha(PRESSED_ALPHA_VALUE); colorDrawablePressed.mutate(); StateListDrawable textBgDrawable = new StateListDrawable(); textBgDrawable.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed}, colorDrawablePressed); return textBgDrawable; }
/** * Update the background color of the mBgCircle image view. */ public void setBackgroundColor(@ColorRes int colorRes) { if (getBackground() instanceof ShapeDrawable) { ((ShapeDrawable) getBackground()) .getPaint() .setColor(getResources().getColor(colorRes)); } }
private void setUp(final double diameter) { final int shadowYOffset = PixelUtl.dp2px(mParent.getContext(), Y_OFFSET); final int shadowXOffset = PixelUtl.dp2px(mParent.getContext(), X_OFFSET); int mShadowRadius = PixelUtl.dp2px(mParent.getContext(), 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 Drawable generateCircleBgDrawable(float width, int color) { Shape arcShape = new ArcShape(0, 360); ShapeDrawable shopDrawablePress = new ShapeDrawable(arcShape);//圆形shape shopDrawablePress.getPaint().setColor(color);//设置颜色 shopDrawablePress.getPaint().setStyle(Paint.Style.STROKE);//设置颜色 shopDrawablePress.getPaint().setStrokeWidth(width);//设置颜色 return shopDrawablePress; }
/** * 创建一个框框的Drawable */ public static Drawable createStrokeDrawable(int color, float radii, float borderWidth) { float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个 RectF inset = new RectF(borderWidth, borderWidth, borderWidth, borderWidth); Shape roundRectShape = new RoundRectShape(outRadii, inset, outRadii);//圆角背景 ShapeDrawable shapeDrawable = new ShapeDrawable(roundRectShape);//圆角shape shapeDrawable.getPaint().setColor(color);//设置颜色 return shapeDrawable; }
@NonNull public static Drawable getSelectableDrawableFor(int color) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState( new int[]{android.R.attr.state_pressed}, new ColorDrawable(lightenOrDarken(color, 0.40D)) ); stateListDrawable.addState( new int[]{android.R.attr.state_focused}, new ColorDrawable(lightenOrDarken(color, 0.80D)) ); stateListDrawable.addState( new int[]{}, new ColorDrawable(color) ); return stateListDrawable; } else { ColorStateList pressedColor = ColorStateList.valueOf(lightenOrDarken(color, 0.4D)); ColorDrawable defaultColor = new ColorDrawable(color); ShapeDrawable shapeDrawable = (ShapeDrawable) getShapeWithColor(color); Drawable rippleColor = getRippleColor(color); return new RippleDrawable( pressedColor, shapeDrawable, rippleColor ); } }
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 void setCommonTabDivider(CommonTabLayout tabLayout, @ColorInt int color, int showDividers, int padding) { LinearLayout linearLayout = (LinearLayout) tabLayout.getChildAt(0); linearLayout.setDividerPadding(padding); RectShape rectShape = new RectShape(); float density = tabLayout.getResources().getDisplayMetrics().density; ShapeDrawable shapeDrawable = new ShapeDrawable(rectShape); shapeDrawable.setIntrinsicWidth((int) density); shapeDrawable.setIntrinsicHeight((int) density); shapeDrawable.getPaint().setColor(color); shapeDrawable.getPaint().setStyle(Paint.Style.STROKE); linearLayout.setDividerDrawable(shapeDrawable); linearLayout.setShowDividers(showDividers); }
public static void setDrawableColor(Drawable v, Integer customColor){ if(customColor != null) { if (v instanceof ShapeDrawable) { ((ShapeDrawable)v).getPaint().setColor(customColor); } else if (v instanceof GradientDrawable) { ((GradientDrawable)v).setColor(customColor); } else if (v instanceof ColorDrawable) { ((ColorDrawable)v).setColor(customColor); } } }