@TargetApi(Build.VERSION_CODES.LOLLIPOP) private Drawable createFillDrawable() { StateListDrawable drawable = new StateListDrawable(); drawable.addState( new int[] {-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled)); drawable.addState( new int[] {android.R.attr.state_pressed}, createCircleDrawable(mColorPressed)); drawable.addState(new int[] {}, createCircleDrawable(mColorNormal)); if (Util.hasLollipop()) { RippleDrawable ripple = new RippleDrawable( new ColorStateList(new int[][] {{}}, new int[] {mColorRipple}), drawable, null); setOutlineProvider( new ViewOutlineProvider() { @Override public void getOutline(View view, Outline outline) { outline.setOval(0, 0, view.getWidth(), view.getHeight()); } }); setClipToOutline(true); mBackgroundDrawable = ripple; return ripple; } mBackgroundDrawable = drawable; return drawable; }
@Override public void draw(@NonNull final Canvas canvas) { super.draw(canvas); // Only recalculate the close bounds if they are dirty if (mCloseBoundChanged) { mCloseBoundChanged = false; mClosableLayoutRect.set(0, 0, getWidth(), getHeight()); // Create the bounds for our close regions. applyCloseRegionBounds(mClosePosition, mClosableLayoutRect, mCloseRegionBounds); // The inset rect applies padding around the visible closeButton. mInsetCloseRegionBounds.set(mCloseRegionBounds); mInsetCloseRegionBounds.inset(mCloseButtonPadding, mCloseButtonPadding); // The close button sits inside the close region with padding and gravity // in the same way the close region sits inside the whole ClosableLayout applyCloseButtonBounds(mClosePosition, mInsetCloseRegionBounds, mCloseButtonBounds); mCloseDrawable.setBounds(mCloseButtonBounds); } // Draw last so that this gets drawn as the top layer. This is also why we override // draw instead of onDraw. if (mCloseDrawable.isVisible()) { mCloseDrawable.draw(canvas); } }
@SuppressWarnings("deprecation") public void setCircleButtonStateListDrawable( View circleButton, int radius, int pressedColor, int normalColor) { WeakReference<Bitmap> imagePressed = new WeakReference<>(Bitmap.createBitmap(2 * radius, 2 * radius, Bitmap.Config.ARGB_8888)); Canvas canvasPressed = new Canvas(imagePressed.get()); Paint paintPressed = new Paint(); paintPressed.setAntiAlias(true); paintPressed.setColor(pressedColor); canvasPressed.drawCircle(radius, radius, radius, paintPressed); WeakReference<Bitmap> imageNormal = new WeakReference<>(Bitmap.createBitmap(2 * radius, 2 * radius, Bitmap.Config.ARGB_8888)); Canvas canvasNormal = new Canvas(imageNormal.get()); Paint paintNormal = new Paint(); paintNormal.setAntiAlias(true); paintNormal.setColor(normalColor); canvasNormal.drawCircle(radius, radius, radius, paintNormal); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState( new int[] {android.R.attr.state_pressed}, new BitmapDrawable(circleButton.getContext().getResources(), imagePressed.get())); stateListDrawable.addState( StateSet.WILD_CARD, new BitmapDrawable(circleButton.getContext().getResources(), imageNormal.get())); if (android.os.Build.VERSION.SDK_INT >= 16) { circleButton.setBackground(stateListDrawable); } else { circleButton.setBackgroundDrawable(stateListDrawable); } }
/** * add new tab with title text * * @param title title text */ public void addTab(CharSequence title) { int layoutId = getLayoutId(type); TextView tv = (TextView) inflater.inflate(layoutId, tabWidget, false); tv.setText(title); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // tv.setBackgroundResource(R.drawable.mth_tab_widget_background_ripple); } else { // create background using colorControlActivated StateListDrawable d = new StateListDrawable(); d.addState( new int[] {android.R.attr.state_pressed}, new ColorDrawable(colorControlActivated)); d.setAlpha(180); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { tv.setBackgroundDrawable(d); } else { tv.setBackgroundDrawable(d); } } int tabId = tabWidget.getTabCount(); addTab(newTabSpec(String.valueOf(tabId)).setIndicator(tv).setContent(android.R.id.tabcontent)); }
private void updateBackground() { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[] {android.R.attr.state_pressed}, createDrawable(mColorPressed)); drawable.addState(new int[] {-android.R.attr.state_enabled}, createDrawable(mColorDisabled)); drawable.addState(new int[] {}, createDrawable(mColorNormal)); setBackgroundCompat(drawable); }
public static int applyLabelColor(Label label, TextView view) { int bgColorRgb = U.decodeLabelBackgroundColor(label.backgroundColor); if (bgColorRgb == -1) { bgColorRgb = 0x212121; // default color Grey 900 } GradientDrawable grad = null; Drawable bg = view.getBackground(); if (bg instanceof GradientDrawable) { grad = (GradientDrawable) bg; } else if (bg instanceof StateListDrawable) { StateListDrawable states = (StateListDrawable) bg; Drawable current = states.getCurrent(); if (current instanceof GradientDrawable) { grad = (GradientDrawable) current; } } if (grad != null) { grad.setColor(0xff000000 | bgColorRgb); final int labelColor = 0xff000000 | U.getLabelForegroundColorBasedOnBackgroundColor(bgColorRgb); view.setTextColor(labelColor); return labelColor; } return 0; }
public DrawableLayeredButton(Context context, int topLayer, boolean hasBorder) { Drawable topLayerDrawable = context.getResources().getDrawable(topLayer); Drawable greenButtonDrawable = context.getResources().getDrawable(mGreenButtonResource); InsetDrawable topLayerInset = new InsetDrawable(topLayerDrawable, Utilities.convertToPx(8)); InsetDrawable greenButtonInset = new InsetDrawable(greenButtonDrawable, Utilities.convertToPx(5)); int width = Utilities.convertToPx(40); int height = Utilities.convertToPx(40); ShapeDrawable border = new ShapeDrawable(new OvalShape()); border.getPaint().setColor(0xffabab25); border.setBounds(0, 0, width, height); Drawable[] nonActivatedlayers = new Drawable[2]; nonActivatedlayers[0] = greenButtonInset; nonActivatedlayers[1] = topLayerInset; LayerDrawable nonActivatedLayer = new LayerDrawable(nonActivatedlayers); Drawable[] activatedlayers = new Drawable[3]; activatedlayers[0] = border; activatedlayers[1] = greenButtonInset; activatedlayers[2] = topLayerInset; LayerDrawable activatedLayer = new LayerDrawable(activatedlayers); mDrawable = new StateListDrawable(); if (hasBorder) { mDrawable.addState(new int[] {android.R.attr.state_checked}, activatedLayer); } mDrawable.addState(new int[] {}, nonActivatedLayer); }
private void updateBackground(View view, int checked, int unchecked) { // Set text color ColorStateList colorStateList = new ColorStateList( new int[][] { {android.R.attr.state_pressed}, {-android.R.attr.state_pressed, -android.R.attr.state_checked}, {-android.R.attr.state_pressed, android.R.attr.state_checked} }, new int[] {Color.GRAY, mTintColor, mCheckedTextColor}); ((Button) view).setTextColor(colorStateList); // Redraw with tint color Drawable checkedDrawable = resources.getDrawable(checked).mutate(); Drawable uncheckedDrawable = resources.getDrawable(unchecked).mutate(); ((GradientDrawable) checkedDrawable).setColor(mTintColor); ((GradientDrawable) uncheckedDrawable).setStroke(oneDP, mTintColor); // Create drawable StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[] {-android.R.attr.state_checked}, uncheckedDrawable); stateListDrawable.addState(new int[] {android.R.attr.state_checked}, checkedDrawable); // Set button background if (Build.VERSION.SDK_INT >= 16) { view.setBackground(stateListDrawable); } else { view.setBackgroundDrawable(stateListDrawable); } }
private Drawable createSelector(int color) { ShapeDrawable darkerCircle = new ShapeDrawable(new OvalShape()); darkerCircle.getPaint().setColor(translucentColor(shiftColorUp(color))); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[] {android.R.attr.state_pressed}, darkerCircle); return stateListDrawable; }
/** * Create a layered drawable * * @param back - Background image to use when target is active * @param front - Front image to use for target * @param inset - Target inset padding * @param frontBlank - Whether the front image for active target should be blank * @return StateListDrawable */ private StateListDrawable getLayeredDrawable( Drawable back, Drawable front, int inset, boolean frontBlank) { front.mutate(); back.mutate(); InsetDrawable[] inactivelayer = new InsetDrawable[2]; InsetDrawable[] activelayer = new InsetDrawable[2]; Drawable activeFront = frontBlank ? mResources.getDrawable(android.R.color.transparent) : front; Drawable inactiveBack = mResources.getDrawable(com.android.internal.R.drawable.ic_lockscreen_lock_pressed); inactivelayer[0] = new InsetDrawable(inactiveBack, 0, 0, 0, 0); inactivelayer[1] = new InsetDrawable(front, inset, inset, inset, inset); activelayer[0] = new InsetDrawable(back, 0, 0, 0, 0); activelayer[1] = new InsetDrawable(activeFront, inset, inset, inset, inset); StateListDrawable states = new StateListDrawable(); LayerDrawable inactiveLayerDrawable = new LayerDrawable(inactivelayer); inactiveLayerDrawable.setId(0, 0); inactiveLayerDrawable.setId(1, 1); LayerDrawable activeLayerDrawable = new LayerDrawable(activelayer); activeLayerDrawable.setId(0, 0); activeLayerDrawable.setId(1, 1); states.addState(TargetDrawable.STATE_INACTIVE, inactiveLayerDrawable); states.addState(TargetDrawable.STATE_ACTIVE, activeLayerDrawable); states.addState(TargetDrawable.STATE_FOCUSED, activeLayerDrawable); return states; }
public static StateListDrawable createBlueButtonDrawable(Context context) { float corner = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 2, context.getResources().getDisplayMetrics()); float[] corners = new float[] {corner, corner, corner, corner, corner, corner, corner, corner}; ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.getPaint().setColor(TgR.color.blue_color); Shape rrs = new RoundRectShape(corners, null, null); shapeDrawable.setShape(rrs); ShapeDrawable shapeDrawablePressed = new ShapeDrawable(); // shapeDrawablePressed.getPaint().setColor((TgR.color.blue_color & 0x00FFFFFF) | (0xCC // << 24)); shapeDrawablePressed.getPaint().setColor(TgR.color.main_theme_color); Shape rrsPressed = new RoundRectShape(corners, null, null); shapeDrawablePressed.setShape(rrsPressed); ShapeDrawable shapeDrawableDisable = new ShapeDrawable(); shapeDrawableDisable.getPaint().setColor(0xFFD1D1D1); Shape rrsDisable = new RoundRectShape(corners, null, null); shapeDrawableDisable.setShape(rrsDisable); StateListDrawable sld = new StateListDrawable(); sld.addState(new int[] {-android.R.attr.state_enabled}, shapeDrawableDisable); sld.addState(new int[] {android.R.attr.state_pressed}, shapeDrawablePressed); sld.addState(new int[] {android.R.attr.state_enabled}, shapeDrawable); return sld; }
private Drawable createRadioBackground(int position) { StateListDrawable listDrawable = new StateListDrawable(); listDrawable.addState( new int[] {android.R.attr.state_checked}, new CircleAnimDrawable(this).setPosition(position)); return listDrawable; }
public static StateListDrawable createItemsStateListDrawable() { StateListDrawable sld = new StateListDrawable(); sld.addState(new int[] {android.R.attr.state_pressed}, new ColorDrawable(0x33D1D1D1)); sld.addState(new int[] {android.R.attr.state_enabled}, new ColorDrawable(0x00FFFFFF)); return sld; }
/** * 创建一个图片选择器 * * @param normalState 普通状态的图片 * @param pressedState 按压状态的图片 */ public static StateListDrawable createSelector(Drawable normalState, Drawable pressedState) { StateListDrawable bg = new StateListDrawable(); bg.addState( new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled}, pressedState); bg.addState(new int[] {android.R.attr.state_enabled}, normalState); bg.addState(new int[] {}, normalState); return bg; }
/** * ============================================================================================= * Init State List Drawable and Color * ============================================================================================= */ private StateListDrawable createStateListDrawable(Drawable drawable[]) { StateListDrawable states = new StateListDrawable(); states.addState( new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled}, drawable[0]); states.addState( new int[] {android.R.attr.state_focused, android.R.attr.state_enabled}, drawable[0]); states.addState(new int[] {android.R.attr.state_enabled}, drawable[1]); states.addState(new int[] {-android.R.attr.state_enabled}, drawable[2]); return states; }
private StateListDrawable createFillDrawable(float strokeWidth) { StateListDrawable drawable = new StateListDrawable(); drawable.addState( new int[] {-android.R.attr.state_enabled}, createCircleDrawable(mColorDisabled, strokeWidth)); drawable.addState( new int[] {android.R.attr.state_pressed}, createCircleDrawable(mColorPressed, strokeWidth)); drawable.addState(new int[] {}, createCircleDrawable(mColorNormal, strokeWidth)); return drawable; }
public static StateListDrawable getStates(BitmapDrawable normal, BitmapDrawable selected) { StateListDrawable states = new StateListDrawable(); states.addState(new int[] {android.R.attr.state_pressed}, selected); states.addState(new int[] {android.R.attr.state_focused}, selected); states.addState(new int[] {android.R.attr.state_selected}, selected); states.addState(new int[] {android.R.attr.state_activated}, selected); states.addState(new int[] {}, normal); return states; }
public void addTabIcon(int idNormal, int idSelected) { StateListDrawable sld = new StateListDrawable(); Drawable normal = idNormal == -1 ? null : mContext.getResources().getDrawable(idNormal); Drawable select = idSelected == -1 ? null : mContext.getResources().getDrawable(idSelected); sld.addState(new int[] {android.R.attr.state_selected}, select); sld.addState(new int[] {}, normal); if (mTabIcons == null) { mTabIcons = new ArrayList<>(); } mTabIcons.add(sld); }
@TargetApi(Build.VERSION_CODES.LOLLIPOP) void onActionUp() { if (mBackgroundDrawable instanceof StateListDrawable) { StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable; drawable.setState(new int[] {android.R.attr.state_enabled}); } else if (Util.hasLollipop()) { RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable; ripple.setState(new int[] {android.R.attr.state_enabled}); ripple.setHotspot(calculateCenterX(), calculateCenterY()); ripple.setVisible(true, true); } }
public static Drawable getColorStateDrawable(int color) { StateListDrawable retVal = new StateListDrawable(); Drawable normal = new ColorDrawable(color); Drawable superS = new ColorDrawable(0xFF99CC00); Drawable select = new ColorDrawable(0x8899CC00); retVal.addState(new int[] {android.R.attr.state_pressed}, select); retVal.addState(new int[] {android.R.attr.state_focused}, select); retVal.addState(new int[] {android.R.attr.state_checked}, superS); retVal.addState(new int[] {android.R.attr.state_selected}, select); retVal.addState(new int[0], normal); return retVal; }
private StateListDrawable getButtonBackground() { int[] pressedState = {android.R.attr.state_pressed}; int[] focusedState = {android.R.attr.state_focused}; int[] defaultState = {android.R.attr.state_enabled}; ColorDrawable colorDefault = new ColorDrawable(mButtonBackgroundColorNormal); ColorDrawable colorPressed = new ColorDrawable(mButtonBackgroundColorPressed); ColorDrawable colorFocused = new ColorDrawable(mButtonBackgroundColorFocused); StateListDrawable background = new StateListDrawable(); background.addState(pressedState, colorPressed); background.addState(focusedState, colorFocused); background.addState(defaultState, colorDefault); return background; }
private static Drawable getAccentStateDrawable(Context context) { TypedValue typedValue = new TypedValue(); Resources.Theme theme = context.getTheme(); theme.resolveAttribute(R.attr.colorControlHighlight, typedValue, true); Drawable colorDrawable = new ColorDrawable(typedValue.data); StateListDrawable stateListDrawable = new StateListDrawable(); stateListDrawable.addState(new int[] {android.R.attr.state_activated}, colorDrawable); stateListDrawable.addState(StateSet.WILD_CARD, null); return stateListDrawable; }
@Override protected void drawableStateChanged() { super.drawableStateChanged(); if (mImageView != null) { mHolographicHelper.generatePressedFocusedStates(mImageView); Drawable d = mImageView.getDrawable(); if (d instanceof StateListDrawable) { StateListDrawable sld = (StateListDrawable) d; sld.setState(getDrawableState()); sld.invalidateSelf(); } } }
private void initBackground(TypedArray typedArray) { int radius = typedArray.getDimensionPixelOffset( R.styleable.ImageTextButton_itb_radius, getResources().getDimensionPixelOffset(R.dimen.default_radius)); int btnNormalBgColor = typedArray.getColor( R.styleable.ImageTextButton_itb_bg, getResources().getColor(R.color.default_bg)); int btnPressedBgColor = typedArray.getColor(R.styleable.ImageTextButton_itb_bg_pressed, -1); if (btnPressedBgColor == -1) { int alpha = Color.alpha(btnNormalBgColor); int red = Color.red(btnNormalBgColor); int green = Color.green(btnNormalBgColor); int blue = Color.blue(btnNormalBgColor); if (alpha < 0xFF) { btnPressedBgColor = Color.argb(0xFF, red, green, blue); } else { btnPressedBgColor = Color.argb(0x90, red, green, blue); } } int btnDisabledBgColor = typedArray.getColor( R.styleable.ImageTextButton_itb_bg_disabled, getResources().getColor(R.color.default_bg_disabled)); GradientDrawable normalShape = new GradientDrawable(); normalShape.setColor(btnNormalBgColor); if (radius > 0) { normalShape.setCornerRadius(radius); } GradientDrawable pressedShape = new GradientDrawable(); pressedShape.setColor(btnPressedBgColor); if (radius > 0) { pressedShape.setCornerRadius(radius); } GradientDrawable disabledShape = new GradientDrawable(); disabledShape.setColor(btnDisabledBgColor); if (radius > 0) { disabledShape.setCornerRadius(radius); } StateListDrawable bg = new StateListDrawable(); bg.addState(new int[] {android.R.attr.state_pressed}, pressedShape); bg.addState(new int[] {android.R.attr.state_enabled}, normalShape); setBackground(bg); setClickable(true); }
/** Generate the pressed/focused states if necessary. */ public void generatePressedFocusedStates(ImageView v) { if (!mStatesUpdated && v != null) { mStatesUpdated = true; Bitmap original = createOriginalImage(v, mTempCanvas); Bitmap outline = createPressImage(v, mTempCanvas); FastBitmapDrawable originalD = new FastBitmapDrawable(original); FastBitmapDrawable outlineD = new FastBitmapDrawable(outline); StateListDrawable states = new StateListDrawable(); states.addState(new int[] {android.R.attr.state_pressed}, outlineD); states.addState(new int[] {android.R.attr.state_focused}, outlineD); states.addState(new int[] {}, originalD); v.setImageDrawable(states); } }
@Override protected boolean onStateChange(int[] states) { boolean isStatePressedInArray = false; for (int state : states) { if (state == android.R.attr.state_pressed) { isStatePressedInArray = true; } } if (isStatePressedInArray) { super.setColorFilter(mColorPressed, PorterDuff.Mode.SRC_IN); } else { super.setColorFilter(mColorNormal, PorterDuff.Mode.SRC_IN); } return super.onStateChange(states); }
@Override public void onLightweightThemeChanged() { Drawable drawable = mActivity.getLightweightTheme().getDrawable(this); if (drawable == null) return; StateListDrawable stateList = new StateListDrawable(); stateList.addState( new int[] {R.attr.state_private}, new ColorDrawable(mActivity.getResources().getColor(R.color.background_private))); stateList.addState(new int[] {}, drawable); int[] padding = new int[] {getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom()}; setBackgroundDrawable(stateList); setPadding(padding[0], padding[1], padding[2], padding[3]); }
@SuppressLint("NewApi") void init(Context context) { mIsLollipop = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); if (mIsLollipop) { int elevation = context.getResources().getDimensionPixelSize(R.dimen.fab_elevation); setElevation(elevation); } int fabColor = context.getResources().getColor(R.color.fab_color); int fabColorPressed = darken(fabColor); StateListDrawable background = new StateListDrawable(); background.addState(new int[] {android.R.attr.state_pressed}, createOval(fabColorPressed)); background.addState(new int[] {}, createOval(fabColor)); setBackgroundCompat(background); }
public Drawable createIconDrawable(Drawable src) { Bitmap scaled = createIconBitmap(src); StateListDrawable result = new StateListDrawable(); result.addState( new int[] {android.R.attr.state_focused}, new BitmapDrawable(createSelectedBitmap(scaled, false))); result.addState( new int[] {android.R.attr.state_pressed}, new BitmapDrawable(createSelectedBitmap(scaled, true))); result.addState(new int[0], new BitmapDrawable(scaled)); result.setBounds(0, 0, mIconTextureWidth, mIconTextureHeight); return result; }
// The drawable is constructed as per @drawable/shaped_button. @Override public void onLightweightThemeChanged() { final int background = ContextCompat.getColor(getContext(), R.color.text_and_tabs_tray_grey); final LightweightThemeDrawable lightWeight = getTheme().getColorDrawable(this, background); if (lightWeight == null) return; lightWeight.setAlpha(34, 34); final StateListDrawable stateList = new StateListDrawable(); stateList.addState(PRESSED_ENABLED_STATE_SET, getColorDrawable(R.color.highlight_shaped)); stateList.addState(FOCUSED_STATE_SET, getColorDrawable(R.color.highlight_shaped_focused)); stateList.addState(PRIVATE_STATE_SET, getColorDrawable(R.color.text_and_tabs_tray_grey)); stateList.addState(EMPTY_STATE_SET, lightWeight); setBackgroundDrawable(stateList); }