public Drawable create(Context context) { final GradientDrawable bubble = new GradientDrawable(); final int radius = context.getResources().getDimensionPixelSize(R.dimen.message_bubble_corner_radius); final float[] radii = cornerBooleansToRadii(corners, radius); bubble.setColor(color); bubble.setCornerRadii(radii); if (!hasShadow) { return bubble; } else { final GradientDrawable shadow = new GradientDrawable(); final int distance = context.getResources().getDimensionPixelSize(R.dimen.message_bubble_shadow_distance); shadow.setColor(shadowColor); shadow.setCornerRadii(radii); final LayerDrawable layers = new LayerDrawable(new Drawable[]{shadow, bubble}); layers.setLayerInset(1, 0, 0, 0, distance); return layers; } }
protected LayerDrawable generateFinalDrawables(RectF circleRect) { if (mSize == SIZE_NOSHADOW) { return new LayerDrawable( new Drawable[]{ createFillDrawable(circleRect), createStrokesDrawable(circleRect), getIconDrawable() }); } else { return new LayerDrawable( new Drawable[]{ getResources().getDrawable(getDrawableBySize(mSize)), createFillDrawable(circleRect), createStrokesDrawable(circleRect), getIconDrawable() }); } }
/** * Updates tint of a layer with the specified <var>layerId</var> of the wrapped drawable * depends on the specified <var>stateSet</var>. * * @param layerId Id of the desired layer of which tint to update. * @param stateSet State set to properly resolve tint color. * @return {@code True} if tint has ben updated, {@code false} otherwise. */ private boolean updateDrawableLayerTint(int layerId, int[] stateSet) { if ((mPrivateFlags & PFLAG_HAS_COLOR_FILTER) == 0) { final Drawable drawable = ((LayerDrawable) mDrawable).findDrawableByLayerId(layerId); if (drawable == null) { return false; } final DrawableLayerTint layerTint = mDrawableLayerTints != null ? mDrawableLayerTints.get(layerId) : null; if (layerTint != null && layerTint.tintList != null && layerTint.tintMode != null) { final int tintColor = layerTint.tintList.getColorForState(stateSet, layerTint.currentTint); if (tintColor != layerTint.currentTint || (mPrivateFlags & PFLAG_TINT_COLOR_CACHING_ENABLED) == 0) { drawable.setColorFilter(new PorterDuffColorFilter(tintColor, layerTint.tintMode)); layerTint.currentTint = tintColor; } } else { drawable.clearColorFilter(); } return true; } return false; }
@Override public void initView() { mRefreshLayout = findViewById(R.id.refreshLayout); int deta = new Random().nextInt(7 * 24 * 60 * 60 * 1000); mClassicsHeader = (ClassicsHeader)mRefreshLayout.getRefreshHeader(); mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis()-deta)); mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA)); mClassicsHeader.setTimeFormat(new DynamicTimeFormat("更新于 %s")); mClassicsHeader.setSpinnerStyle(SpinnerStyle.Translate); mDrawableProgress = mClassicsHeader.getProgressView().getDrawable(); if (mDrawableProgress instanceof LayerDrawable) { mDrawableProgress = ((LayerDrawable) mDrawableProgress).getDrawable(0); } if (isFirstEnter) { isFirstEnter = false; //触发自动刷新 mRefreshLayout.autoRefresh(); } }
private void initBackground() { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { int progressColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), R.attr.navigationViewProgress); int progressBackgroundColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), R.attr.navigationViewProgressBackground); LayerDrawable progressBarDrawable = (LayerDrawable) alertProgressBar.getProgressDrawable(); // ProgressBar progress color Drawable progressBackgroundDrawable = progressBarDrawable.getDrawable(0); progressBackgroundDrawable.setColorFilter(progressBackgroundColor, PorterDuff.Mode.SRC_IN); // ProgressBar background color Drawable progressDrawable = progressBarDrawable.getDrawable(1); progressDrawable.setColorFilter(progressColor, PorterDuff.Mode.SRC_IN); // Hide the background getBackground().setAlpha(0); } else { setBackgroundColor(ContextCompat.getColor(getContext(), android.R.color.transparent)); } }
private ReactViewBackgroundDrawable getOrCreateReactViewBackground() { if (mReactBackgroundDrawable == null) { mReactBackgroundDrawable = new ReactViewBackgroundDrawable(); Drawable backgroundDrawable = getBackground(); super.setBackground(null); // required so that drawable callback is cleared before we add the // drawable back as a part of LayerDrawable if (backgroundDrawable == null) { super.setBackground(mReactBackgroundDrawable); } else { LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{mReactBackgroundDrawable, backgroundDrawable}); super.setBackground(layerDrawable); } } return mReactBackgroundDrawable; }
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 styleSeekBar() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { final Drawable progressDrawable = mSeekBar.getProgressDrawable(); if (progressDrawable != null) { if (progressDrawable instanceof LayerDrawable) { LayerDrawable ld = (LayerDrawable) progressDrawable; int layers = ld.getNumberOfLayers(); for (int i = 0; i < layers; i++) { ld.getDrawable(i).mutate().setColorFilter(mTintColor, PorterDuff.Mode.SRC_ATOP); } } else { progressDrawable.mutate().setColorFilter(mTintColor, PorterDuff.Mode.SRC_ATOP); } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { final Drawable thumb = mSeekBar.getThumb(); if (thumb != null) { thumb.mutate().setColorFilter(mTintColor, PorterDuff.Mode.SRC_ATOP); } } } }
public void changeBackground() { if (iSchecked) { if (!isInEditMode()) { setBackgroundResource(R.drawable.background_checkbox); LayerDrawable layer = (LayerDrawable) getBackground(); GradientDrawable shape = (GradientDrawable) layer .findDrawableByLayerId(R.id.shape_bacground); shape.setColor(backgroundColor); } } else { if (!isInEditMode()) { setBackgroundResource(R.drawable.background_switch_ball_uncheck); } } }
@Override public void setBackgroundColor(int color) { backgroundColor = color; if (isEnabled()) { beforeBackground = backgroundColor; } try { LayerDrawable layer = (LayerDrawable) getBackground(); // 每个按钮的框架都是由drawable中的xml文件制定的,xml文件中都有一个item的id叫:shape_bacground GradientDrawable shape = (GradientDrawable) layer.findDrawableByLayerId(R.id.shape_bacground); /** * 给这个图片设置背景色,因为图片的主体是透明的所以可以直接显示背景色 * 效果就是一个透明但有阴影的框架下有了背景色,这样的方式可以方便的设置不同颜色的按钮,让按钮看起来还是浑然一体 */ shape.setColor(backgroundColor); /** * 当重新设定背景色后,要检查涟漪颜色。如果已经设定了涟漪颜色,那么就用之前的。如果没设定就重新生成 */ if (!settedRippleColor) { rippleColor = makePressColor(255); } } catch (Exception ex) { // Without bacground } }
/** * 动态创建带上分隔线或下分隔线的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; }
public StrengthMeter(Context context, AttributeSet attrs) { super(context, attrs, android.R.attr.progressBarStyleHorizontal); bar = new ShapeDrawable(); bar.getPaint().setColor(RED); ClipDrawable clip = new ClipDrawable(bar, LEFT, HORIZONTAL); ShapeDrawable background = new ShapeDrawable(); Paint p = background.getPaint(); p.setStyle(FILL); p.setColor(getResources().getColor(android.R.color.transparent)); p.setStyle(STROKE); p.setStrokeWidth(1); p.setColor(BLACK); Drawable[] layers = new Drawable[] { clip, background }; setProgressDrawable(new LayerDrawable(layers)); setIndeterminate(false); }
public static @Nullable BorderDrawable getBorderDrawable(@NonNull View view){ Drawable drawable=view.getBackground(); if(drawable instanceof BorderDrawable){ return (BorderDrawable) drawable; } else if(drawable instanceof LayerDrawable){ if(((LayerDrawable) drawable).getNumberOfLayers()>1) { Drawable innerDrawable=((LayerDrawable) drawable).getDrawable(0); if(innerDrawable instanceof BorderDrawable){ return (BorderDrawable) innerDrawable; } } } return null; }
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; }
public static boolean containsNinePatch(Drawable drawable) { drawable = getWrapperDrawable(drawable); if (drawable instanceof NinePatchDrawable || drawable instanceof InsetDrawable || drawable instanceof LayerDrawable) { return true; } else if (drawable instanceof StateListDrawable) { final DrawableContainer.DrawableContainerState containerState = ((DrawableContainer.DrawableContainerState) drawable.getConstantState()); //can't getBaseApplication containState from drawable which is containing DrawableWrapperDonut //https://code.google.com/p/android/issues/detail?id=169920 if (containerState == null) { return true; } for (Drawable dr : containerState.getChildren()) { dr = getWrapperDrawable(dr); if (dr instanceof NinePatchDrawable || dr instanceof InsetDrawable || dr instanceof LayerDrawable) { return true; } } } return false; }
public static Drawable fromDrawable(Drawable drawable, Resources r) { if (drawable != null) { if (drawable instanceof SelectableRoundedCornerDrawable) { return drawable; } else if (drawable instanceof LayerDrawable) { LayerDrawable ld = (LayerDrawable) drawable; final int num = ld.getNumberOfLayers(); for (int i = 0; i < num; i++) { Drawable d = ld.getDrawable(i); ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d, r)); } return ld; } Bitmap bm = drawableToBitmap(drawable); if (bm != null) { return new SelectableRoundedCornerDrawable(bm, r); } else { } } return drawable; }
private void updateAttrs(Drawable drawable) { if (drawable == null) { return; } if (drawable instanceof RoundedDrawable) { ((RoundedDrawable) drawable).setScaleType(mScaleType).setCornerRadius(cornerRadius) .setBorderWidth(borderWidth).setBorderColor(borderColor).setOval(isOval); } else if (drawable instanceof LayerDrawable) { // loop through layers to and set drawable attrs LayerDrawable ld = ((LayerDrawable) drawable); for (int i = 0, layers = ld.getNumberOfLayers(); i < layers; i++) { updateAttrs(ld.getDrawable(i)); } } }
public static Drawable fromDrawable(Drawable drawable) { if (drawable == null || (drawable instanceof RoundedPagerDrawable)) { return drawable; } if (drawable instanceof LayerDrawable) { Drawable ld = (LayerDrawable) drawable; int num = ld.getNumberOfLayers(); for (int i = 0; i < num; i++) { ld.setDrawableByLayerId(ld.getId(i), fromDrawable(ld.getDrawable(i))); } return ld; } Bitmap bm = drawableToBitmap(drawable); if (bm != null) { return new RoundedPagerDrawable(bm); } return drawable; }
private void updateAttrs(Drawable drawable) { if (drawable != null) { if (drawable instanceof RoundedPagerDrawable) { ((RoundedPagerDrawable) drawable).setScaleType(this.mScaleType).setBorderWidth(this.mBorderWidth).setBorderColor(this.mBorderColor).setOval(this.mIsOval).setTileModeX(this.mTileModeX).setTileModeY(this.mTileModeY); if (this.mCornerRadii != null) { ((RoundedPagerDrawable) drawable).setCornerRadius(this.mCornerRadii[0], this.mCornerRadii[1], this.mCornerRadii[2], this.mCornerRadii[3]); } applyColorMod(); } else if (drawable instanceof LayerDrawable) { LayerDrawable ld = (LayerDrawable) drawable; int layers = ld.getNumberOfLayers(); for (int i = 0; i < layers; i++) { updateAttrs(ld.getDrawable(i)); } } } }
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); } }
@Override protected void setUp(View view) { mRatingMessageView.setVisibility(View.GONE); mPlayStoreRatingView.setVisibility(View.GONE); LayerDrawable stars = (LayerDrawable) mRatingBar.getProgressDrawable(); stars.getDrawable(2) .setColorFilter(ContextCompat.getColor(getContext(), R.color.yellow), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(0) .setColorFilter(ContextCompat.getColor(getContext(), R.color.shadow), PorterDuff.Mode.SRC_ATOP); stars.getDrawable(1) .setColorFilter(ContextCompat.getColor(getContext(), R.color.shadow), PorterDuff.Mode.SRC_ATOP); mSubmitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mPresenter.onRatingSubmitted(mRatingBar.getRating(), mMessage.getText().toString()); } }); }
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 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 void setColor(Drawable drawable, @ColorInt int color) { if (drawable != null) { if (drawable instanceof LayerDrawable) { // LayerDrawable layerDrawable = (LayerDrawable) drawable; // int numberOfLayers = layerDrawable.getNumberOfLayers(); //// if (numberOfLayers > 0) { //// setColor((layerDrawable).getDrawable(numberOfLayers - 1), color); //// } // for (int i = 0; i < numberOfLayers; i++) { // setColor((layerDrawable).getDrawable(i), color); // } mShowMask = true; postInvalidate(); // layerDrawable.setColorFilter(color, PorterDuff.Mode.MULTIPLY); } else { drawable.mutate().setColorFilter(color, PorterDuff.Mode.MULTIPLY); } } }
public void showAtLocation(final View v, int x, int y) { final PopupMenu that = this; this.window.setOnDismissListener(new PopupWindow.OnDismissListener() { public void onDismiss() { that.main_layout.removeAllViews(); } }); this.main_layout = new LinearLayout(this.ctx); LayerDrawable layerDrawable = new LayerDrawable(new Drawable[] { (NinePatchDrawable)this.ctx.getResources().getDrawable(android.R.drawable.dialog_frame), new ColorDrawable(Color.WHITE) }); layerDrawable.setLayerInset(0, Utils.dip2px(this.ctx, 5), Utils.dip2px(this.ctx, 5) + 3, Utils.dip2px(this.ctx, 5), Utils.dip2px(this.ctx, 5)); this.main_layout.setBackgroundDrawable(layerDrawable); this.main_layout.addView(this.contentViewLayout); this.window.setContentView(this.main_layout); this.window.setWidth(this.width); this.window.setHeight(this.height); this.window.showAsDropDown(v, x, y); }
public static void drawCircle(MaterialButton view, int width, int height, float x, float y, int color, int _color, float radius, int alpha, Drawable drawable) { Bitmap bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bm); final Paint paint = new Paint(); paint.setColor(_color); paint.setAlpha(alpha); paint.setAntiAlias(true); canvas.drawCircle(x, y, radius, paint); if(drawable != null) { view.setBackgroundDrawable(new LayerDrawable(new Drawable[] {drawable, new BitmapDrawable(bm)})); } if(drawable == null) { view.setBackgroundDrawable(new LayerDrawable(new Drawable[] {new ColorDrawable(color), new BitmapDrawable(bm)})); } }
public static Drawable fromDrawable(Drawable drawable) { if (drawable != null) { if (drawable instanceof RoundedDrawable) { // just return if it's already a RoundedDrawable return drawable; } else if (drawable instanceof LayerDrawable) { LayerDrawable ld = (LayerDrawable) drawable; int num = ld.getNumberOfLayers(); // loop through layers to and change to RoundedDrawables if possible for (int i = 0; i < num; i++) { Drawable d = ld.getDrawable(i); ld.setDrawableByLayerId(ld.getId(i), fromDrawable(d)); } return ld; } // try to get a bitmap from the drawable and Bitmap bm = drawableToBitmap(drawable); if (bm != null) { return new RoundedDrawable(bm); } } return drawable; }
void setBackgroundDrawable(ColorStateList backgroundTint, Mode backgroundTintMode, int rippleColor, int borderWidth) { Drawable[] layers; this.mShapeDrawable = DrawableCompat.wrap(createShapeDrawable()); DrawableCompat.setTintList(this.mShapeDrawable, backgroundTint); if (backgroundTintMode != null) { DrawableCompat.setTintMode(this.mShapeDrawable, backgroundTintMode); } this.mRippleDrawable = DrawableCompat.wrap(createShapeDrawable()); DrawableCompat.setTintList(this.mRippleDrawable, createColorStateList(rippleColor)); if (borderWidth > 0) { this.mBorderDrawable = createBorderDrawable(borderWidth, backgroundTint); layers = new Drawable[]{this.mBorderDrawable, this.mShapeDrawable, this.mRippleDrawable}; } else { this.mBorderDrawable = null; layers = new Drawable[]{this.mShapeDrawable, this.mRippleDrawable}; } this.mContentBackground = new LayerDrawable(layers); this.mShadowDrawable = new ShadowDrawableWrapper(this.mView.getResources(), this.mContentBackground, this.mShadowViewDelegate.getRadius(), this.mElevation, this.mElevation + this.mPressedTranslationZ); this.mShadowDrawable.setAddPaddingForCorners(false); this.mShadowViewDelegate.setBackgroundDrawable(this.mShadowDrawable); }
static Drawable getRotateDrawable(final Drawable d, final int angle) { return new LayerDrawable(new Drawable[]{d}) { @Override public void draw(final Canvas canvas) { canvas.save(); canvas.rotate(angle, d.getBounds().width() / 2, d.getBounds().height() / 2); super.draw(canvas); canvas.restore(); } }; }