Java Code Examples for android.graphics.drawable.StateListDrawable

The following are top voted examples for showing how to use android.graphics.drawable.StateListDrawable. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: RoundButton   File: RoundButton.java   View source code 6 votes vote down vote up
private void update() {
    StateListDrawable background = new StateListDrawable();
    background.addState(new int[]{android.R.attr.state_pressed}, createDrawable(backgroundColorPressed, cornerColorPressed, cornerWidth, cornerRadius));
    background.addState(new int[]{-android.R.attr.state_enabled}, createDrawable(backgroundColorDisabled, cornerColorDisabled, cornerWidth, cornerRadius));
    background.addState(StateSet.WILD_CARD, createDrawable(backgroundColor, cornerColor, cornerWidth, cornerRadius));
    setBackground(background);

    setTextColor(new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_pressed},
                    new int[]{-android.R.attr.state_enabled},
                    new int[]{}
            },
            new int[]{
                    textColorPressed,
                    textColorDisabled,
                    textColor
            }
    ));
}
 
Example 2
Project: Toodoo   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
    drawable.addState(new int[]{}, createRectDrawable(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;
}
 
Example 3
Project: SunmiUI   File: ButtonRoundCorner.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void setBackgroundColor(int colorPressed, int colorNormal){
    StateListDrawable sd = new StateListDrawable();
    int[] state_pressed = new int[]{android.R.attr.state_pressed};
    int[] state_normal = new int[]{android.R.attr.state_enabled};

    GradientDrawable pressed = new GradientDrawable();
    pressed.setColor(colorPressed);
    pressed.setCornerRadius(10);

    GradientDrawable enable = new GradientDrawable();
    enable.setColor(colorNormal);
    enable.setCornerRadius(10);

    sd.addState(state_pressed, pressed);
    sd.addState(state_normal, enable);
    bg.setBackground(sd);
}
 
Example 4
Project: Toodoo   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void onActionDown() {
        if (mUsingStyle) {
            mBackgroundDrawable = getBackground();
        }

        if (mBackgroundDrawable instanceof StateListDrawable) {
            StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
            drawable.setState(new int[]{android.R.attr.state_pressed});
        } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
            RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
            ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
            ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
            ripple.setVisible(true, true);
        }
//        setPressed(true);
    }
 
Example 5
Project: LuaViewPlayground   File: DrawableUtil.java   View source code 6 votes vote down vote up
/**
 * create state list drawable
 *
 * @param context
 * @param idNormal
 * @param idPressed
 * @param idFocused
 * @param idUnable
 * @return
 */
public static StateListDrawable createStateListDrawable(Context context, int idNormal, int idPressed, int idFocused, int idUnable) {
    StateListDrawable bg = new StateListDrawable();
    Drawable normal = idNormal == -1 ? null : context.getResources().getDrawable(idNormal);
    Drawable pressed = idPressed == -1 ? null : context.getResources().getDrawable(idPressed);
    Drawable focused = idFocused == -1 ? null : context.getResources().getDrawable(idFocused);
    Drawable unable = idUnable == -1 ? null : context.getResources().getDrawable(idUnable);
    // View.PRESSED_ENABLED_STATE_SET
    bg.addState(new int[]{android.R.attr.state_pressed, android.R.attr.state_enabled}, pressed);
    // View.ENABLED_FOCUSED_STATE_SET
    bg.addState(new int[]{android.R.attr.state_enabled, android.R.attr.state_focused}, focused);
    // View.ENABLED_STATE_SET
    bg.addState(new int[]{android.R.attr.state_enabled}, normal);
    // View.FOCUSED_STATE_SET
    bg.addState(new int[]{android.R.attr.state_focused}, focused);
    // View.WINDOW_FOCUSED_STATE_SET
    bg.addState(new int[]{android.R.attr.state_window_focused}, unable);
    // View.EMPTY_STATE_SET
    bg.addState(new int[]{}, normal);
    return bg;
}
 
Example 6
Project: OpenEyesReading-android   File: Util.java   View source code 6 votes vote down vote up
public void setCircleButtonStateListDrawable(View circleButton, int radius,int color) {
    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(color);
    canvasNormal.drawCircle(radius, radius, radius, paintNormal);
    StateListDrawable stateListDrawable = new StateListDrawable();
    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);
    }

}
 
Example 7
Project: AssistantBySDK   File: SwitchButton.java   View source code 6 votes vote down vote up
@Override
protected void drawableStateChanged() {
	super.drawableStateChanged();

	if (!mIsThumbUseDrawable && mThumbColor != null) {
		mCurrThumbColor = mThumbColor.getColorForState(getDrawableState(), mCurrThumbColor);
	} else {
		setDrawableState(mThumbDrawable);
	}

	int[] nextState = isChecked() ? UNCHECKED_PRESSED_STATE : CHECKED_PRESSED_STATE;
	ColorStateList textColors = getTextColors();
	if (textColors != null) {
		int defaultTextColor = textColors.getDefaultColor();
		mOnTextColor = textColors.getColorForState(CHECKED_PRESSED_STATE, defaultTextColor);
		mOffTextColor = textColors.getColorForState(UNCHECKED_PRESSED_STATE, defaultTextColor);
	}
	if (!mIsBackUseDrawable && mBackColor != null) {
		mCurrBackColor = mBackColor.getColorForState(getDrawableState(), mCurrBackColor);
		mNextBackColor = mBackColor.getColorForState(nextState, mCurrBackColor);
	} else {
		if (mBackDrawable instanceof StateListDrawable && mFadeBack) {
			mBackDrawable.setState(nextState);
			mNextBackDrawable = mBackDrawable.getCurrent().mutate();
		} else {
			mNextBackDrawable = null;
		}
		setDrawableState(mBackDrawable);
		if (mBackDrawable != null) {
			mCurrentBackDrawable = mBackDrawable.getCurrent().mutate();
		}
	}
}
 
Example 8
Project: RLibrary   File: ResUtil.java   View source code 6 votes vote down vote up
/**
 * Generate bg drawable drawable.
 *
 * @param radii        the radii
 * @param pressColor   the press color
 * @param defaultColor the default color
 * @return the drawable
 */
public static Drawable generateRoundDrawable(Resources res, float radii, int pressColor, int defaultColor) {

    radii = dpToPx(res, radii);

    //外环的圆角矩形
    float[] outRadii = new float[]{radii, radii, radii, radii, radii, radii, radii, radii};//四个角的 圆角幅度,8个可以设置的值,每个角都有2个边 2*4=8个

    //按下状态
    Shape roundRectShape = new RoundRectShape(outRadii, null, null);//圆角背景
    ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
    shopDrawablePress.getPaint().setColor(pressColor);//设置颜色

    //正常状态
    Shape roundRectShapeNormal = new RoundRectShape(outRadii, null, null);
    ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal);
    shopDrawableNormal.getPaint().setColor(defaultColor);

    StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
    bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
    bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态

    return bgStateDrawable;
}
 
Example 9
Project: RLibrary   File: ResUtil.java   View source code 6 votes vote down vote up
/**
 * 正常 圆角边框;
 * 按下 圆角色块
 */
public static Drawable generateBorderDrawable(float radii, float borderWidth, int pressColor, int defaultColor) {

    //外环的圆角矩形
    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, null, null);//圆角背景
    ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
    shopDrawablePress.getPaint().setColor(pressColor);//设置颜色

    //正常状态
    Shape roundRectShapeNormal = new RoundRectShape(outRadii, inset, outRadii);
    ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShapeNormal);
    shopDrawableNormal.getPaint().setColor(defaultColor);

    StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
    bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
    bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态

    return bgStateDrawable;
}
 
Example 10
Project: boohee_v5.6   File: YearPickerView.java   View source code 6 votes vote down vote up
public YearPickerView(Context context, DatePickerController controller) {
    super(context);
    this.mController = controller;
    this.mController.registerOnDateChangedListener(this);
    setLayoutParams(new LayoutParams(-1, -2));
    Resources res = context.getResources();
    this.mViewSize = res.getDimensionPixelOffset(R.dimen.mdtp_date_picker_view_animator_height);
    this.mChildSize = res.getDimensionPixelOffset(R.dimen.mdtp_year_label_height);
    setVerticalFadingEdgeEnabled(true);
    setFadingEdgeLength(this.mChildSize / 3);
    init(context);
    setOnItemClickListener(this);
    setSelector(new StateListDrawable());
    setDividerHeight(0);
    onDateChanged();
}
 
Example 11
Project: AppCommonFrame   File: BitmapUtil.java   View source code 6 votes vote down vote up
/**
 * 通过代码配置一个selector XML对象 . <br>
 * @author liulongzhenhai 2012-7-4 上午10:45:03 <br>
 * @param normal 没有状态
 * @param pressed 按下状态
 * @param focused 获取焦点状态
 * @param unable 无状态
 * @return 返回selector 的对象布局
 */
public static StateListDrawable getNewSelector(final Drawable normal, final Drawable pressed,
                                                  final Drawable focused, final Drawable unable) {
	final StateListDrawable bg = new StateListDrawable();
	// View.PRESSED_ENABLED_STATE_SET
	bg.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed);
	// View.ENABLED_FOCUSED_STATE_SET
	bg.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused);
	// View.ENABLED_STATE_SET
	bg.addState(new int[] { android.R.attr.state_enabled }, normal);
	// View.FOCUSED_STATE_SET
	bg.addState(new int[] { android.R.attr.state_focused }, focused);
	// View.WINDOW_FOCUSED_STATE_SET
	bg.addState(new int[] { android.R.attr.state_window_focused }, unable);
	// View.EMPTY_STATE_SET
	bg.addState(new int[] {}, normal);
	return bg;
}
 
Example 12
Project: editor-sql   File: FloatingActionButton.java   View source code 6 votes vote down vote up
@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;
}
 
Example 13
Project: editor-sql   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
    drawable.addState(new int[]{}, createRectDrawable(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;
}
 
Example 14
Project: SmartOrnament   File: CircularProgressButton.java   View source code 6 votes vote down vote up
private void initIdleStateDrawable() {
    int colorNormal = getNormalColor(mIdleColorState);
    int colorPressed = getPressedColor(mIdleColorState);
    int colorFocused = getFocusedColor(mIdleColorState);
    int colorDisabled = getDisabledColor(mIdleColorState);
    if (background == null) {
        background = createDrawable(colorNormal);
    }

    StrokeGradientDrawable drawableDisabled = createDrawable(colorDisabled);
    StrokeGradientDrawable drawableFocused = createDrawable(colorFocused);
    StrokeGradientDrawable drawablePressed = createDrawable(colorPressed);
    mIdleStateDrawable = new StateListDrawable();

    mIdleStateDrawable.addState(new int[]{android.R.attr.state_pressed}, drawablePressed.getGradientDrawable());
    mIdleStateDrawable.addState(new int[]{android.R.attr.state_focused}, drawableFocused.getGradientDrawable());
    mIdleStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, drawableDisabled.getGradientDrawable());
    mIdleStateDrawable.addState(StateSet.WILD_CARD, background.getGradientDrawable());
}
 
Example 15
Project: MDWechat   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
    drawable.addState(new int[]{}, createRectDrawable(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;
}
 
Example 16
Project: MDWechat   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void onActionDown() {
        if (mUsingStyle) {
            mBackgroundDrawable = getBackground();
        }

        if (mBackgroundDrawable instanceof StateListDrawable) {
            StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
            drawable.setState(new int[]{android.R.attr.state_pressed});
        } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
            RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
            ripple.setState(new int[]{android.R.attr.state_enabled, android.R.attr.state_pressed});
            ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
            ripple.setVisible(true, true);
        }
//        setPressed(true);
    }
 
Example 17
Project: MDWechat   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void onActionUp() {
        if (mUsingStyle) {
            mBackgroundDrawable = getBackground();
        }

        if (mBackgroundDrawable instanceof StateListDrawable) {
            StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
            drawable.setState(new int[]{});
        } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
            RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
            ripple.setState(new int[]{});
            ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
            ripple.setVisible(true, true);
        }
//        setPressed(false);
    }
 
Example 18
Project: silly-android   File: ColoringTest.java   View source code 6 votes vote down vote up
/**
 * Tests the {@link Coloring#createMultiStateDrawable(Drawable, Drawable, Drawable, boolean)} method.
 * <p>
 * Unfortunately some Drawable properties are not shadowed by Robolectric yet, so we can test only the basic stuff here.
 */
@Test
public final void testCreateMultiStateDrawable() {
    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable normal = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_on);
    assertNotNull("Normal drawable is null", normal);

    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable clicked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.btn_star_big_off);
    assertNotNull("Clicked drawable is null", clicked);

    // noinspection deprecation - can't enforce Lollipop here
    final BitmapDrawable checked = (BitmapDrawable) mActivityContext.getResources().getDrawable(android.R.drawable.star_off);
    assertNotNull("Checked drawable is null", checked);

    final StateListDrawable stateList = Coloring.createMultiStateDrawable(normal, clicked, checked, true);
    assertNotNull("Contrast state drawable is null", stateList);
    assertTrue("Contrast state drawable is not stateful", stateList.isStateful());
    final Drawable.ConstantState constantState = stateList.getConstantState();
    assertNotNull("Constant state is null", constantState);
}
 
Example 19
Project: android-paypal-example   File: BadgeDrawableBuilder.java   View source code 6 votes vote down vote up
public StateListDrawable build(Context ctx) {
    StateListDrawable stateListDrawable = new StateListDrawable();

    GradientDrawable normal = (GradientDrawable) ContextCompat.getDrawable(ctx, R.drawable.action_item_badge);
    GradientDrawable selected = (GradientDrawable) normal.getConstantState().newDrawable().mutate();

    normal.setColor(mColor);
    selected.setColor(mColorPressed);

    if (mStroke > -1) {
        normal.setStroke(mStroke, mStrokeColor);
        selected.setStroke(mStroke, mStrokeColor);
    }

    if (mCorners > -1) {
        normal.setCornerRadius(mCorners);
        selected.setCornerRadius(mCorners);
    }

    stateListDrawable.addState(new int[]{android.R.attr.state_pressed}, selected);
    stateListDrawable.addState(StateSet.WILD_CARD, normal);

    return stateListDrawable;
}
 
Example 20
Project: Mix   File: ThemeUtils.java   View source code 6 votes vote down vote up
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;
}
 
Example 21
Project: letv   File: CustomResourceMgmt.java   View source code 6 votes vote down vote up
public Drawable getCheckStatusDrawable(String origin, String checked, String pressed, String checkpressed, boolean is9Png) {
    int originId = this.mResources.getIdentifier(origin, "drawable", this.packageName);
    int selectId = this.mResources.getIdentifier(checked, "drawable", this.packageName);
    int pressedId = this.mResources.getIdentifier(pressed, "drawable", this.packageName);
    int selectpressId = this.mResources.getIdentifier(checkpressed, "drawable", this.packageName);
    if (originId == 0 && selectId == 0 && pressedId == 0 && selectpressId == 0) {
        String suffix = (is9Png ? ".9" : "") + ".png";
        return new StateListDrawableBuilder(this.mContext, "drawable/" + origin + suffix).setPressDrawable("drawable/" + pressed + suffix).setCheckedDrawable("drawable/" + checked + suffix).setPressCheckedDrawable("drawable/" + checkpressed + suffix).create();
    }
    Drawable mStateListDrawable = new StateListDrawable();
    mStateListDrawable.addState(new int[]{16842912}, this.mResources.getDrawable(selectId));
    mStateListDrawable.addState(new int[]{16842919}, this.mResources.getDrawable(pressedId));
    mStateListDrawable.addState(new int[]{16842919, 16842912}, this.mResources.getDrawable(selectpressId));
    mStateListDrawable.addState(new int[0], this.mResources.getDrawable(originId));
    return mStateListDrawable;
}
 
Example 22
Project: RLibrary   File: EmojiTabLayout.java   View source code 6 votes vote down vote up
public void addItem(@DrawableRes int ico) {
    EmojiTabItemView itemView = new EmojiTabItemView(getContext());
    itemView.setImageResource(ico);
    if (mItemBackgroundRes == 0) {
        StateListDrawable stateListDrawable = new StateListDrawable();
        stateListDrawable.addState(new int[]{android.R.attr.state_checked},
                new ColorDrawable(getResources().getColor(R.color.default_base_bg_press)));
        stateListDrawable.addState(new int[]{android.R.attr.state_pressed},
                new ColorDrawable(getResources().getColor(R.color.default_base_bg_press)));
        stateListDrawable.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
        itemView.setBackground(stateListDrawable);
    } else {
        itemView.setBackgroundResource(mItemBackgroundRes);
    }
    addView(itemView, -1, -1);
    itemView.setOnClickListener(this);
}
 
Example 23
Project: ChatExchange-old   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private Drawable createFillDrawable() {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createRectDrawable(mColorPressed));
    drawable.addState(new int[]{}, createRectDrawable(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;
}
 
Example 24
Project: RLibrary   File: ResUtil.java   View source code 6 votes vote down vote up
/**
 * Generate bg drawable drawable.
 *
 * @param pressColor   the press color
 * @param defaultColor the default color
 * @return the drawable
 */
public static Drawable generateRoundDrawable(float radii, int pressColor, int defaultColor) {
    //圆角
    Shape roundRectShape = new RoundRectShape(new float[]{radii, radii, radii, radii, radii, radii, radii, radii}, null, null);//圆角背景

    //按下状态
    ShapeDrawable shopDrawablePress = new ShapeDrawable(roundRectShape);//圆角shape
    shopDrawablePress.getPaint().setColor(pressColor);//设置颜色

    //正常状态
    ShapeDrawable shopDrawableNormal = new ShapeDrawable(roundRectShape);
    shopDrawableNormal.getPaint().setColor(defaultColor);

    StateListDrawable bgStateDrawable = new StateListDrawable();//状态shape
    bgStateDrawable.addState(new int[]{android.R.attr.state_pressed}, shopDrawablePress);//按下状态
    bgStateDrawable.addState(new int[]{-android.R.attr.state_enabled}, shopDrawablePress);
    bgStateDrawable.addState(new int[]{}, shopDrawableNormal);//其他状态

    return bgStateDrawable;
}
 
Example 25
Project: ChatExchange-old   File: Label.java   View source code 6 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
    void onActionUp() {
        if (mUsingStyle) {
            mBackgroundDrawable = getBackground();
        }

        if (mBackgroundDrawable instanceof StateListDrawable) {
            StateListDrawable drawable = (StateListDrawable) mBackgroundDrawable;
            drawable.setState(new int[]{});
        } else if (Util.hasLollipop() && mBackgroundDrawable instanceof RippleDrawable) {
            RippleDrawable ripple = (RippleDrawable) mBackgroundDrawable;
            ripple.setState(new int[]{});
            ripple.setHotspot(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
            ripple.setVisible(true, true);
        }
//        setPressed(false);
    }
 
Example 26
Project: Watermark   File: MediaActivity.java   View source code 6 votes vote down vote up
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;
}
 
Example 27
Project: PWEditText-SafeKeyboard   File: SafetyKeyboard.java   View source code 5 votes vote down vote up
private StateListDrawable getDrawableSeletor(Drawable normal,
        Drawable press) {
    StateListDrawable bg = new StateListDrawable();
    if (null != normal) {
        bg.addState(new int[] { android.R.attr.state_pressed }, press);
    }
    if (null != press) {
        bg.addState(new int[] { android.R.attr.state_enabled }, normal);
    }
    return bg;
}
 
Example 28
Project: MeetMusic   File: ScanActivity.java   View source code 5 votes vote down vote up
private void setScanBtnBg(){
    int defColor = CustomAttrValueUtil.getAttrColorValue(R.attr.colorAccent,R.color.colorAccent,this);
    int pressColor = CustomAttrValueUtil.getAttrColorValue(R.attr.press_color,R.color.colorAccent,this);
    Drawable backgroundDrawable =  scanBtn.getBackground();
    StateListDrawable sld = (StateListDrawable) backgroundDrawable;// 通过向下转型,转回原型,selector对应的Java类为:StateListDrawable
    SelectorUtil.changeViewColor(sld,new int[]{pressColor,defColor});
}
 
Example 29
Project: GitHub   File: DrawableUtils.java   View source code 5 votes vote down vote up
private static StateListDrawable getStateListDrawable(@ColorInt int normalColor,
													  @ColorInt int pressedColor) {
	StateListDrawable states = new StateListDrawable();
	states.addState(new int[]{android.R.attr.state_activated}, getColorDrawable(pressedColor));
	states.addState(new int[]{}, getColorDrawable(normalColor));
	// Animating across states.
	// It seems item background is lost on scrolling out of the screen, 21 <= API <= 23
	if (!Utils.hasLollipop() || Utils.hasNougat()) {
		int duration = 200; //android.R.integer.config_shortAnimTime
		states.setEnterFadeDuration(duration);
		states.setExitFadeDuration(duration);
	}
	return states;
}
 
Example 30
Project: SingleSelectBar   File: ResHelper.java   View source code 5 votes vote down vote up
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;
}
 
Example 31
Project: GitHub   File: FastAdapterUIUtils.java   View source code 5 votes vote down vote up
/**
 * helper to create an StateListDrawable for the given normal and pressed color
 *
 * @param normalColor  the normal color
 * @param pressedColor the pressed color
 * @return the StateListDrawable
 */
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 32
Project: GitHub   File: JellyBeanFloatingActionButton.java   View source code 5 votes vote down vote up
/**
 * more advanced usage for fillable in alpha
 *
 * @param circleRect the defined rectangle
 * @return StateListDrawable item
 */
protected StateListDrawable createFillDrawable(RectF circleRect) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createAlphaDrawble(circleRect, mColorPressed, mAlpha_press));
    drawable.addState(new int[]{}, createAlphaDrawble(circleRect, mColorNormal, mAlpha_normal));
    return drawable;
}
 
Example 33
Project: GitHub   File: FloatingActionButton.java   View source code 5 votes vote down vote up
/**
 * @param circleRect the defined rectangle
 * @param alpha      between  0 - 1
 * @return StateListDrawable
 */
protected StateListDrawable createFillDrawable(RectF circleRect, float alpha) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createAlphaDrawble(circleRect, mColorPressed, alpha));
    drawable.addState(new int[]{}, createAlphaDrawble(circleRect, mColorNormal, alpha));
    return drawable;
}
 
Example 34
Project: GitHub   File: FloatingActionButton.java   View source code 5 votes vote down vote up
/**
 * @param circleRect the defined rectangle
 * @return StateListDrawable
 */
protected StateListDrawable createFillDrawable(RectF circleRect) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(new int[]{android.R.attr.state_pressed}, createCircleDrawable(circleRect, mColorPressed));
    drawable.addState(new int[]{}, createCircleDrawable(circleRect, mColorNormal));
    return drawable;
}
 
Example 35
Project: boohee_v5.6   File: DayView.java   View source code 5 votes vote down vote up
private static Drawable generateBackground(int color, int fadeTime) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.setExitFadeDuration(fadeTime);
    drawable.addState(new int[]{16842912}, generateCircleDrawable(color));
    if (VERSION.SDK_INT >= 21) {
        drawable.addState(new int[]{16842919}, generateRippleDrawable(color));
    } else {
        drawable.addState(new int[]{16842919}, generateCircleDrawable(color));
    }
    drawable.addState(new int[0], generateCircleDrawable(0));
    return drawable;
}
 
Example 36
Project: GitHub   File: MsgView.java   View source code 5 votes vote down vote up
public void setBgSelector() {
    StateListDrawable bg = new StateListDrawable();

    setDrawable(gd_background, backgroundColor, strokeColor);
    bg.addState(new int[]{-android.R.attr.state_pressed}, gd_background);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {//16
        setBackground(bg);
    } else {
        //noinspection deprecation
        setBackgroundDrawable(bg);
    }
}
 
Example 37
Project: GitHub   File: MsgView.java   View source code 5 votes vote down vote up
public void setBgSelector() {
    StateListDrawable bg = new StateListDrawable();

    setDrawable(gd_background, backgroundColor, strokeColor);
    bg.addState(new int[]{-android.R.attr.state_pressed}, gd_background);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {//16
        setBackground(bg);
    } else {
        //noinspection deprecation
        setBackgroundDrawable(bg);
    }
}
 
Example 38
Project: GitHub   File: WoWoStateListColorAnimation.java   View source code 5 votes vote down vote up
private void setColors(View view, int[] colors) {
    Drawable drawable = view.getBackground();
    if (drawable instanceof StateListDrawable) {
        StateListDrawable stateListDrawable = (StateListDrawable) drawable;
        DrawableContainerState drawableContainerState = (DrawableContainerState) stateListDrawable.getConstantState();
        if (drawableContainerState != null) {
            Drawable[] drawables = drawableContainerState.getChildren();
            for (int i = 0; i < colors.length; i++) if (drawables[i] instanceof GradientDrawable) ((GradientDrawable) drawables[i]).setColor(colors[i]);
        }
    } else Log.w(TAG, "Drawable of view must be StateListDrawable in WoWoStateListColorAnimation");
}
 
Example 39
Project: boohee_v5.6   File: NavigationMenuItemView.java   View source code 5 votes vote down vote up
private StateListDrawable createDefaultBackground() {
    TypedValue value = new TypedValue();
    if (!getContext().getTheme().resolveAttribute(R.attr.colorControlHighlight, value, true)) {
        return null;
    }
    StateListDrawable drawable = new StateListDrawable();
    drawable.addState(CHECKED_STATE_SET, new ColorDrawable(value.data));
    drawable.addState(EMPTY_STATE_SET, new ColorDrawable(0));
    return drawable;
}
 
Example 40
Project: GitHub   File: DayView.java   View source code 5 votes vote down vote up
private static Drawable generateBackground(int color, int fadeTime, Rect bounds) {
    StateListDrawable drawable = new StateListDrawable();
    drawable.setExitFadeDuration(fadeTime);
    drawable.addState(new int[]{android.R.attr.state_checked}, generateCircleDrawable(color));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        drawable.addState(new int[]{android.R.attr.state_pressed}, generateRippleDrawable(color, bounds));
    } else {
        drawable.addState(new int[]{android.R.attr.state_pressed}, generateCircleDrawable(color));
    }

    drawable.addState(new int[]{}, generateCircleDrawable(Color.TRANSPARENT));

    return drawable;
}