Java源码示例:androidx.core.widget.CompoundButtonCompat

示例1
@SuppressLint({"RestrictedApi"})
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void setColor() {
    if (mColor != WidgetDefaults.ADS_COLOR_UNKNOWN) {
        @ColorInt int tintColor = DynamicTheme.getInstance().get().getTintBackgroundColor();

        if (isBackgroundAware()) {
            tintColor = DynamicColorUtils.getContrastColor(
                    tintColor, DynamicTheme.getInstance().get().getBackgroundColor());

            if (mContrastWithColor != WidgetDefaults.ADS_COLOR_UNKNOWN) {
                mColor = DynamicColorUtils.getContrastColor(mColor, mContrastWithColor);
            }
        }

        DynamicTintUtils.setViewBackgroundTint(this,
                mContrastWithColor, mColor, true, true);
        CompoundButtonCompat.setButtonTintList(this,
                DynamicResourceUtils.getColorStateList(tintColor, mColor, true));
    }
}
 
示例2
@SuppressLint({"RestrictedApi"})
@TargetApi(Build.VERSION_CODES.M)
@Override
public void setColor() {
    if (mColor != WidgetDefaults.ADS_COLOR_UNKNOWN) {
        @ColorInt int tintColor = DynamicTheme.getInstance().get().getTintBackgroundColor();

        if (isBackgroundAware()) {
            tintColor = DynamicColorUtils.getContrastColor(
                    tintColor, DynamicTheme.getInstance().get().getBackgroundColor());

            if (mContrastWithColor != WidgetDefaults.ADS_COLOR_UNKNOWN) {
                mColor = DynamicColorUtils.getContrastColor(mColor, mContrastWithColor);
            }
        }

        DynamicTintUtils.setViewBackgroundTint(this,
                mContrastWithColor, mColor, true, true);
        CompoundButtonCompat.setButtonTintList(this,
                DynamicResourceUtils.getColorStateList(tintColor, mColor, true));
    }
}
 
示例3
@Override
public void bind(CheckBoxSetting entry) {
    super.bind(entry);
    int[] overrideColors = ((ThemeOptionSetting) entry).overrideColors;
    int bgColor = StyledAttributesHelper.getColor(mCheckBox.getContext(),
            android.R.attr.colorBackground, 0);
    boolean darkBg = ColorUtils.calculateLuminance(bgColor) < 0.4;
    int overrideColor = overrideColors[0];
    for (int c : overrideColors) {
        if ((!darkBg && ColorUtils.calculateLuminance(c) < 0.75)
                || (darkBg && ColorUtils.calculateLuminance(c) > 0.25)) {
            overrideColor = c;
            break;
        }
    }
    if (overrideColor != 0)
        CompoundButtonCompat.setButtonTintList(mCheckBox,
                ColorStateList.valueOf(overrideColor));
    else
        CompoundButtonCompat.setButtonTintList(mCheckBox, mDefaultButtonTintList);
}
 
示例4
public MaterialRadioButton(
    @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
  // Ensure we are using the correctly themed context rather than the context that was passed in.
  context = getContext();

  TypedArray attributes =
      ThemeEnforcement.obtainStyledAttributes(
          context, attrs, R.styleable.MaterialRadioButton, defStyleAttr, DEF_STYLE_RES);

  // If buttonTint is specified, read it using MaterialResources to allow themeable attributes in
  // all API levels.
  if (attributes.hasValue(R.styleable.MaterialRadioButton_buttonTint)) {
    CompoundButtonCompat.setButtonTintList(
        this,
        MaterialResources.getColorStateList(
            context, attributes, R.styleable.MaterialRadioButton_buttonTint));
  }

  useMaterialThemeColors =
      attributes.getBoolean(R.styleable.MaterialRadioButton_useMaterialThemeColors, false);

  attributes.recycle();
}
 
示例5
public MaterialCheckBox(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
  // Ensure we are using the correctly themed context rather than the context that was passed in.
  context = getContext();

  TypedArray attributes =
      ThemeEnforcement.obtainStyledAttributes(
          context, attrs, R.styleable.MaterialCheckBox, defStyleAttr, DEF_STYLE_RES);

  // If buttonTint is specified, read it using MaterialResources to allow themeable attributes in
  // all API levels.
  if (attributes.hasValue(R.styleable.MaterialCheckBox_buttonTint)) {
    CompoundButtonCompat.setButtonTintList(
        this,
        MaterialResources.getColorStateList(
            context, attributes, R.styleable.MaterialCheckBox_buttonTint));
  }

  useMaterialThemeColors =
      attributes.getBoolean(R.styleable.MaterialCheckBox_useMaterialThemeColors, false);

  attributes.recycle();
}
 
示例6
static void updateCheckBoxColor(CompoundButton checkBox, LiveThemeComponent component) {
    CompoundButtonCompat.setButtonTintList(checkBox,
            createCheckBoxTintStateList(checkBox.getContext(), component));
    if (Build.VERSION.SDK_INT >= 21 && checkBox.getBackground() instanceof RippleDrawable) {
        ((RippleDrawable) checkBox.getBackground()).setColor(
                createCheckBoxRippleTintStateList(checkBox.getContext(), component));
    }
}
 
示例7
@Override
public void applySkin() {
    mButtonResourceId = SkinCompatHelper.checkResourceId(mButtonResourceId);
    if (mButtonResourceId != INVALID_ID) {
        mView.setButtonDrawable(SkinCompatVectorResources.getDrawableCompat(mView.getContext(), mButtonResourceId));
    }
    mButtonTintResId = SkinCompatHelper.checkResourceId(mButtonTintResId);
    if (mButtonTintResId != INVALID_ID) {
        CompoundButtonCompat.setButtonTintList(mView, SkinCompatResources.getColorStateList(mView.getContext(), mButtonTintResId));
    }
}
 
示例8
@Override
protected void onAttachedToWindow() {
  super.onAttachedToWindow();

  if (useMaterialThemeColors && CompoundButtonCompat.getButtonTintList(this) == null) {
    setUseMaterialThemeColors(true);
  }
}
 
示例9
/**
 * Forces the {@link MaterialRadioButton} to use colors from a Material Theme. Overrides any
 * specified ButtonTintList. If set to false, sets the tints to null. Use {@link
 * MaterialRadioButton#setSupportButtonTintList} to change button tints.
 */
public void setUseMaterialThemeColors(boolean useMaterialThemeColors) {
  this.useMaterialThemeColors = useMaterialThemeColors;
  if (useMaterialThemeColors) {
    CompoundButtonCompat.setButtonTintList(this, getMaterialThemeColorsTintList());
  } else {
    CompoundButtonCompat.setButtonTintList(this, null);
  }
}
 
示例10
@Override
protected void onAttachedToWindow() {
  super.onAttachedToWindow();

  if (useMaterialThemeColors && CompoundButtonCompat.getButtonTintList(this) == null) {
    setUseMaterialThemeColors(true);
  }
}
 
示例11
/**
 * Forces the {@link MaterialCheckBox} to use colors from a Material Theme. Overrides any
 * specified ButtonTintList. If set to false, sets the tints to null. Use {@link
 * MaterialCheckBox#setSupportButtonTintList} to change button tints.
 */
public void setUseMaterialThemeColors(boolean useMaterialThemeColors) {
  this.useMaterialThemeColors = useMaterialThemeColors;
  if (useMaterialThemeColors) {
    CompoundButtonCompat.setButtonTintList(this, getMaterialThemeColorsTintList());
  } else {
    CompoundButtonCompat.setButtonTintList(this, null);
  }
}
 
示例12
/**
 * Checks that the {@link MaterialRadioButton} buttonTint matches {@link
 * R.color#radiobutton_themeable_attribute_color}.
 */
static void testThemeableButtonTint(RadioButton radioButton) {
  ColorStateList buttonTintList = CompoundButtonCompat.getButtonTintList(radioButton);
  assertThat(buttonTintList.getColorForState(STATE_CHECKED, Color.BLACK))
      .isEqualTo(MaterialColors.getColor(radioButton, R.attr.colorControlActivated));
  assertThat(buttonTintList.getColorForState(STATE_UNCHECKED, Color.BLACK))
      .isEqualTo(MaterialColors.getColor(radioButton, R.attr.colorOnSurface));
}
 
示例13
/**
 * Checks that the {@link MaterialCheckBox} buttonTint matches {@link
 * R.color#checkbox_themeable_attribute_color}.
 */
static void testThemeableButtonTint(CheckBox checkBox) {
  ColorStateList buttonTintList = CompoundButtonCompat.getButtonTintList(checkBox);
  assertThat(buttonTintList.getColorForState(STATE_CHECKED, Color.BLACK))
      .isEqualTo(MaterialColors.getColor(checkBox, R.attr.colorControlActivated));
  assertThat(buttonTintList.getColorForState(STATE_UNCHECKED, Color.BLACK))
      .isEqualTo(MaterialColors.getColor(checkBox, R.attr.colorOnSurface));
}
 
示例14
public Holder(View itemView, SettingsListAdapter adapter) {
    super(itemView, adapter);
    mDefaultButtonTintList = CompoundButtonCompat.getButtonTintList(mCheckBox);
    itemView.setOnLongClickListener(this);
}
 
示例15
@Override
protected void onDraw(Canvas canvas) {
	// If there's text of any sort resort to CompoundButton#onDraw
	if (getText() != null && getText().length() > 0 ||
		getTextOff() != null && getTextOff().length() > 0 ||
		getTextOff() != null && getTextOn().length() > 0) {
		super.onDraw(canvas);
	}
	// Otherwise override CompoundButton#onDraw entirely to allow properly aligned image toggles
	else {
		final Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(this);
		if (buttonDrawable != null) {
			final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
			final int horizontalGravity = getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
			final int drawableHeight = buttonDrawable.getIntrinsicHeight();
			final int drawableWidth = buttonDrawable.getIntrinsicWidth();

			final int top;
			switch (verticalGravity) {
				case Gravity.BOTTOM:
					top = getHeight() - drawableHeight;
					break;
				case Gravity.CENTER_VERTICAL:
					top = (getHeight() - drawableHeight) / 2;
					break;
				default:
					top = 0;
			}

			final int left;
			switch (horizontalGravity) {
				case Gravity.RIGHT:
				case Gravity.END:
					left = getWidth() - drawableWidth;
					break;
				case Gravity.CENTER_HORIZONTAL:
					left = (getWidth() - drawableWidth) / 2;
					break;
				default:
					left = 0;
			}

			final int bottom = top + drawableHeight;
			final int right = left + drawableWidth;

			buttonDrawable.setBounds(left, top, right, bottom);

			final Drawable background = getBackground();
			if (Build.VERSION.SDK_INT > 21 && background != null) {
				background.setHotspotBounds(left, top, right, bottom);
			}

			buttonDrawable.draw(canvas);
		}
	}
}
 
示例16
@Override
public void customizeRadioButton(AppCompatRadioButton button) {
  CompoundButtonCompat.setButtonTintList(
      button, ColorStateList.valueOf(convertToDisplay(main)));
}
 
示例17
@Override
protected void onDraw(Canvas canvas) {
	// If there's text of any sort resort to CompoundButton#onDraw
	if (getText() != null && getText().length() > 0 ||
		getTextOff() != null && getTextOff().length() > 0 ||
		getTextOff() != null && getTextOn().length() > 0) {
		super.onDraw(canvas);
	}
	// Otherwise override CompoundButton#onDraw entirely to allow properly aligned image toggles
	else {
		final Drawable buttonDrawable = CompoundButtonCompat.getButtonDrawable(this);
		if (buttonDrawable != null) {
			final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK;
			final int horizontalGravity = getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK;
			final int drawableHeight = buttonDrawable.getIntrinsicHeight();
			final int drawableWidth = buttonDrawable.getIntrinsicWidth();

			final int top;
			switch (verticalGravity) {
				case Gravity.BOTTOM:
					top = getHeight() - drawableHeight;
					break;
				case Gravity.CENTER_VERTICAL:
					top = (getHeight() - drawableHeight) / 2;
					break;
				default:
					top = 0;
			}

			final int left;
			switch (horizontalGravity) {
				case Gravity.RIGHT:
				case Gravity.END:
					left = getWidth() - drawableWidth;
					break;
				case Gravity.CENTER_HORIZONTAL:
					left = (getWidth() - drawableWidth) / 2;
					break;
				default:
					left = 0;
			}

			final int bottom = top + drawableHeight;
			final int right = left + drawableWidth;

			buttonDrawable.setBounds(left, top, right, bottom);

			final Drawable background = getBackground();
			if (Build.VERSION.SDK_INT > 21 && background != null) {
				background.setHotspotBounds(left, top, right, bottom);
			}

			buttonDrawable.draw(canvas);
		}
	}
}