Java Code Examples for android.graphics.drawable.StateListDrawable#addState()

The following examples show how to use android.graphics.drawable.StateListDrawable#addState() . 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: TagView.java    From fingen with Apache License 2.0 6 votes vote down vote up
private Drawable getSelector(Tag tag) {
	if (tag.background!=null)return tag.background;
	StateListDrawable states = new StateListDrawable();
	GradientDrawable gd_normal = new GradientDrawable();
	gd_normal.setColor(tag.layoutColor);
	gd_normal.setCornerRadius(tag.radius);
	if (tag.layoutBorderSize>0){
		gd_normal.setStroke(Utils.dipToPx(getContext(),tag.layoutBorderSize), tag.layoutBorderColor);
	}
	GradientDrawable gd_press = new GradientDrawable();
	gd_press.setColor(tag.layoutColorPress);
	gd_press.setCornerRadius(tag.radius);
	states.addState(new int[] { android.R.attr.state_pressed }, gd_press);
	//must add state_pressed first,or state_pressed will not take effect
	states.addState(new int[] {}, gd_normal);
	return states;
}
 
Example 2
Source File: TagView.java    From TagView with Apache License 2.0 6 votes vote down vote up
private Drawable getSelector(Tag tag) {
    if (tag.getBackground() != null)
        return tag.getBackground();

    StateListDrawable states = new StateListDrawable();
    GradientDrawable gdNormal = new GradientDrawable();
    gdNormal.setColor(tag.getLayoutColor());
    gdNormal.setCornerRadius(tag.getRadius());
    if (tag.getLayoutBorderSize() > 0) {
        gdNormal.setStroke(Utils.dipToPx(getContext(), tag.getLayoutBorderSize()), tag.getLayoutBorderColor());
    }
    GradientDrawable gdPress = new GradientDrawable();
    gdPress.setColor(tag.getLayoutColorPress());
    gdPress.setCornerRadius(tag.getRadius());
    states.addState(new int[]{android.R.attr.state_pressed}, gdPress);
    //must add state_pressed first,or state_pressed will not take effect
    states.addState(new int[]{}, gdNormal);
    return states;
}
 
Example 3
Source File: QButton.java    From QButton with MIT License 6 votes vote down vote up
private void notifyChanges() {

        Float factor = 0.8f, factorStorke = 0.9f;

        if (!isEnable) {
            //handling for button disable state
            setAlpha(0.6f);
        }

        if (mStrokeColor == 0) {
            mStrokeColor = manipulateColor(mBackgroundColor, factorStorke);
        }

        Drawable pressed = getDrawable1(manipulateColor(mBackgroundColor, factor), mRadius);
        Drawable normal = getDrawable1(mBackgroundColor, mRadius);

        StateListDrawable states = new StateListDrawable();
        states.addState(new int[]{android.R.attr.state_pressed}, pressed);
        states.addState(new int[]{}, normal);
        setBackground(states);

    }
 
Example 4
Source File: RelativeLayoutFeedback.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private void setSelector(TypedArray a) {
    touchFeedbackDrawable = new StateListDrawable();
    touchFeedbackDrawable.addState(
            new int[]{android.R.attr.state_pressed},
            getColor(a)
    );
}
 
Example 5
Source File: EditText.java    From Genius-Android with Apache License 2.0 5 votes vote down vote up
private static StateListDrawable createStateListDrawable(Drawable drawable[]) {
    if (drawable == null || drawable.length < 4)
        return null;
    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[1]);
    states.addState(new int[]{android.R.attr.state_enabled}, drawable[2]);
    states.addState(new int[]{-android.R.attr.state_enabled}, drawable[3]);
    return states;
}
 
Example 6
Source File: ViewUtil.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static Drawable createSelectorDrawable(Context context, @ColorInt int color) {
    final StateListDrawable baseSelector = new StateListDrawable();
    baseSelector.addState(new int[]{android.R.attr.state_activated}, new ColorDrawable(color));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return new RippleDrawable(ColorStateList.valueOf(color), baseSelector, new ColorDrawable(Color.WHITE));
    }

    baseSelector.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
    baseSelector.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(color));
    return baseSelector;
}
 
Example 7
Source File: FloatingActionButton.java    From FloatingActionButton with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: RippleHelper.java    From SegmentedButton with Apache License 2.0 5 votes vote down vote up
private static StateListDrawable getStateListDrawable(int pressedColor, Integer normalColor, int radius) {
    StateListDrawable states = new StateListDrawable();
    states.addState(
            new int[]{android.R.attr.state_pressed}
            , getDrawable(pressedColor, radius)
    );
    if (null != normalColor)
        states.addState(
                new int[]{}
                , getDrawable(normalColor, radius)
        );
    return states;
}
 
Example 9
Source File: DrawableValue.java    From proteus with Apache License 2.0 5 votes vote down vote up
@Override
public void apply(ProteusView view, Context context, ProteusLayoutInflater.ImageLoader loader, Callback callback) {
  final StateListDrawable stateListDrawable = new StateListDrawable();
  int size = states.length;
  for (int i = 0; i < size; i++) {
    stateListDrawable.addState(states[i], DrawableResourceProcessor.evaluate(values[i], view));
  }
  callback.apply(stateListDrawable);
}
 
Example 10
Source File: RelativeLayoutFeedback.java    From ListBuddies with Apache License 2.0 5 votes vote down vote up
private void setSelector(TypedArray a) {
    touchFeedbackDrawable = new StateListDrawable();
    touchFeedbackDrawable.addState(
            new int[]{android.R.attr.state_pressed},
            getColor(a)
    );
}
 
Example 11
Source File: ViewBgUtil.java    From SimpleProject with MIT License 5 votes vote down vote up
public static Drawable getDrawable(int state, int shape, GradientDrawable.Orientation[] orientation,
                                   int[][] bgColor, int[] borderColor, int borderWidth, float[] radius) {
	if (bgColor == null || bgColor.length < MIN_RESOURCE_COUNT) {
		throw new IllegalArgumentException();
	}
	StateListDrawable drawable = new StateListDrawable();
	drawable.addState(new int[] {state}, getDrawable(shape, orientation[1], bgColor[1], borderColor[1], borderWidth, radius));
	drawable.addState(new int[] {}, getDrawable(shape, orientation[0], bgColor[0], borderColor[0], borderWidth, radius));

	return drawable;
}
 
Example 12
Source File: FloatingActionButton.java    From android-floating-action-button with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: UIHelper.java    From uPods-android with Apache License 2.0 5 votes vote down vote up
private static StateListDrawable getStateListDrawable(
        int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[]{android.R.attr.state_pressed},
            new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_focused},
            new ColorDrawable(pressedColor));
    states.addState(new int[]{android.R.attr.state_activated},
            new ColorDrawable(pressedColor));
    states.addState(new int[]{},
            new ColorDrawable(normalColor));
    return states;
}
 
Example 14
Source File: DefaultTagView.java    From TagFlowLayout with Apache License 2.0 5 votes vote down vote up
protected Drawable getBackgroundDrawable() {
    //设置字体颜色的选择器
    ColorStateList colorSateList = mContext.getResources().getColorStateList(R.color.secondary_text);
    setTextColor(colorSateList);

    GradientDrawable normal = new GradientDrawable();
    normal.setShape(GradientDrawable.RECTANGLE);
    normal.setCornerRadius(DensityUtils.dp2px(mContext, getTagRadius()));
    if (isSolid()) {
        normal.setColor(getNormalBackgroundColor());
    } else {
        normal.setStroke(DensityUtils.dp2px(mContext, getStrokeWidth()), getNormalBackgroundColor());
        normal.setColor(getBackgroundColor());
    }

    GradientDrawable pressed = new GradientDrawable();
    pressed.setShape(GradientDrawable.RECTANGLE);
    pressed.setCornerRadius(DensityUtils.dp2px(mContext, getTagRadius()));
    if (isSolid()) {
        pressed.setColor(getPressedBackgroundColor());
    } else {
        pressed.setStroke(DensityUtils.dp2px(mContext, getStrokeWidth()), getPressedBackgroundColor());
        pressed.setColor(getPressedBackgroundColor());
    }

    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[]{android.R.attr.state_pressed}, pressed);
    selector.addState(new int[]{}, normal);
    return selector;
}
 
Example 15
Source File: CircularProgressButton.java    From timecat with Apache License 2.0 5 votes vote down vote up
private void initErrorStateDrawable() {
    int colorPressed = getPressedColor(mErrorColorState);

    StrokeGradientDrawable drawablePressed = createDrawable(colorPressed);
    mErrorStateDrawable = new StateListDrawable();

    mErrorStateDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed.getGradientDrawable());
    mErrorStateDrawable.addState(StateSet.WILD_CARD, background.getGradientDrawable());
}
 
Example 16
Source File: UpdateDialog.java    From AppUpdate with Apache License 2.0 4 votes vote down vote up
private void initView(View view) {
    View ibClose = view.findViewById(R.id.ib_close);
    ImageView ivBg = view.findViewById(R.id.iv_bg);
    TextView title = view.findViewById(R.id.tv_title);
    TextView size = view.findViewById(R.id.tv_size);
    TextView description = view.findViewById(R.id.tv_description);
    progressBar = view.findViewById(R.id.np_bar);
    progressBar.setVisibility(forcedUpgrade ? View.VISIBLE : View.GONE);
    update = view.findViewById(R.id.btn_update);
    update.setTag(0);
    View line = view.findViewById(R.id.line);
    update.setOnClickListener(this);
    ibClose.setOnClickListener(this);
    //自定义
    if (dialogImage != -1) {
        ivBg.setBackgroundResource(dialogImage);
    }
    if (dialogButtonTextColor != -1) {
        update.setTextColor(dialogButtonTextColor);
    }
    if (dialogButtonColor != -1) {
        StateListDrawable drawable = new StateListDrawable();
        GradientDrawable colorDrawable = new GradientDrawable();
        colorDrawable.setColor(dialogButtonColor);
        colorDrawable.setCornerRadius(DensityUtil.dip2px(context, 3));
        drawable.addState(new int[]{android.R.attr.state_pressed}, colorDrawable);
        drawable.addState(new int[]{}, colorDrawable);
        update.setBackgroundDrawable(drawable);
    }
    if (dialogProgressBarColor != -1) {
        progressBar.setReachedBarColor(dialogProgressBarColor);
        progressBar.setProgressTextColor(dialogProgressBarColor);
    }
    //强制升级
    if (forcedUpgrade) {
        line.setVisibility(View.GONE);
        ibClose.setVisibility(View.GONE);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                //屏蔽返回键
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });
    }
    //设置界面数据
    if (!TextUtils.isEmpty(manager.getApkVersionName())) {
        String newVersion = context.getResources().getString(R.string.dialog_new);
        title.setText(String.format(newVersion, manager.getApkVersionName()));
    }
    if (!TextUtils.isEmpty(manager.getApkSize())) {
        String newVersionSize = context.getResources().getString(R.string.dialog_new_size);
        size.setText(String.format(newVersionSize, manager.getApkSize()));
        size.setVisibility(View.VISIBLE);
    }
    description.setText(manager.getApkDescription());
}
 
Example 17
Source File: PXVirtualToggleAdapter.java    From pixate-freestyle-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean updateStyle(List<PXRuleSet> ruleSets, List<PXStylerContext> contexts) {
    if (!super.updateStyle(ruleSets, contexts)) {
        return false;
    }
    // Apply the toggle Drawables wrapped in a LayerDrawable. The drawable
    // that will go into this LayerDrawable has to be a StateListDrawable,
    // so we construct it using the 'on' and 'off' states to provide the
    // toggle images in its different states.
    if (!contexts.isEmpty()) {
        // same styleable for all contexts
        Object styleable = contexts.get(0).getStyleable();
        if (styleable instanceof ToggleButton) {
            // prepare the StateListDrawable
            StateListDrawable toggleDrawable = new StateListDrawable();
            for (PXStylerContext context : contexts) {
                Drawable drawable = context.getBackgroundImage();
                if (drawable != null) {
                    if (ON.equals(context.getActiveStateName())) {
                        toggleDrawable.addState(new int[] { android.R.attr.state_checked },
                                drawable);
                    } else {
                        toggleDrawable.addState(new int[] { -android.R.attr.state_checked },
                                drawable);
                    }
                }
            }
            ToggleButton toggleButton = (ToggleButton) styleable;
            Drawable background = toggleButton.getBackground();
            if (toggleDrawable != null) {
                if (background instanceof LayerDrawable) {
                    LayerDrawable layerDrawable = (LayerDrawable) background;
                    layerDrawable.setDrawableByLayerId(android.R.id.toggle, toggleDrawable);
                } else if (background instanceof StateListDrawable) {
                    // just replace it
                    toggleButton.setBackgroundDrawable(toggleDrawable);
                }
            }
        }
    }
    return true;
}
 
Example 18
Source File: RadiusTextDelegate.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
/**
 * 设置TextView的Left、Top、Right、Bottom Drawable属性
 *
 * @param normal
 * @param checked
 * @param selected
 * @param pressed
 * @param disabled
 * @param gravity
 */
private void setTextDrawable(Drawable normal, Drawable checked, Drawable selected, Drawable pressed,
                             Drawable disabled, int gravity) {
    int index = 0;
    int width = mLeftDrawableWidth;
    int height = mLeftDrawableHeight;
    float radius = mLeftDrawableColorCircleEnable ? width + height / 2 : mLeftDrawableColorRadius;
    switch (gravity) {
        case Gravity.TOP:
            index = 1;
            width = mTopDrawableWidth;
            height = mTopDrawableHeight;
            radius = mTopDrawableColorCircleEnable ? width + height / 2 : mTopDrawableColorRadius;
            break;
        case Gravity.RIGHT:
            index = 2;
            width = mRightDrawableWidth;
            height = mRightDrawableHeight;
            radius = mRightDrawableColorCircleEnable ? width + height / 2 : mRightDrawableColorRadius;
            break;
        case Gravity.BOTTOM:
            index = 3;
            width = mBottomDrawableWidth;
            height = mBottomDrawableHeight;
            radius = mBottomDrawableColorCircleEnable ? width + height / 2 : mBottomDrawableColorRadius;
            break;
    }
    Drawable[] drawables = mTextView.getCompoundDrawables();
    if (normal == null && pressed == null && disabled == null
            && selected == null && checked == null) {
        drawables[index] = null;
    } else {
        StateListDrawable stateDrawable = new StateListDrawable();
        if (checked != null) {
            stateDrawable.addState(new int[]{mStateChecked}, getStateDrawable(checked, radius, width, height));
        }
        if (selected != null) {
            stateDrawable.addState(new int[]{mStateSelected}, getStateDrawable(selected, radius, width, height));
        }
        if (pressed != null) {
            stateDrawable.addState(new int[]{mStatePressed}, getStateDrawable(pressed, radius, width, height));
        }
        if (disabled != null) {
            stateDrawable.addState(new int[]{mStateDisabled}, getStateDrawable(disabled, radius, width, height));
        }
        if (normal != null) {
            stateDrawable.addState(new int[]{}, getStateDrawable(normal, radius, width, height));
        }
        DrawableUtil.setDrawableWidthHeight(stateDrawable, width, height);
        drawables[index] = stateDrawable;
    }
    mTextView.setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}
 
Example 19
Source File: MainActivity.java    From iTab with Apache License 2.0 4 votes vote down vote up
private Drawable createTabDrawable(int resId) {
	Resources res = getResources();
	StateListDrawable states = new StateListDrawable();

	final Options options = new Options();
	options.inPreferredConfig = Config.ARGB_8888;
	
	Bitmap icon = BitmapFactory.decodeResource(res, resId, options);
	
	Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
	Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);
	
	icon.recycle();
	
	states.addState(new int[] { android.R.attr.state_selected }, new BitmapDrawable(res, selected));
	states.addState(new int[] { android.R.attr.state_enabled }, new BitmapDrawable(res, unselected));
	
	return states;
}
 
Example 20
Source File: DrawableProvider.java    From Aurora with Apache License 2.0 3 votes vote down vote up
/**
 * 获得选择器
 *
 * @param normalDrawable
 * @param pressDrawable
 * @return
 */
public static Drawable getStateListDrawable(Drawable normalDrawable, Drawable pressDrawable) {
    StateListDrawable stateListDrawable = new StateListDrawable();
    stateListDrawable.addState(new int[]{android.R.attr.state_checked}, pressDrawable);
    stateListDrawable.addState(new int[]{}, normalDrawable);
    return stateListDrawable;
}