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

The following examples show how to use android.graphics.drawable.StateListDrawable#setEnterFadeDuration() . 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: DrawableUtils.java    From FlexibleAdapter with Apache License 2.0 6 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));
    if (!FlexibleUtils.hasLollipop()) {
        states.addState(new int[]{android.R.attr.state_pressed}, getColorDrawable(pressedColor));
    }
    states.addState(new int[]{}, getColorDrawable(normalColor));
    // Animating across states.
    // It seems item background is lost on scrolling out of the screen on 21 <= API <= 23
    if (!FlexibleUtils.hasLollipop() || FlexibleUtils.hasNougat()) {
        int duration = 200; //android.R.integer.config_shortAnimTime
        states.setEnterFadeDuration(duration);
        states.setExitFadeDuration(duration);
    }
    return states;
}
 
Example 2
Source File: RadiusViewDelegate.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
/**
 * 设置shape属性
 * 设置完所有属性后调用设置背景
 */
public void init() {
    if (mView instanceof EditText) {
        Log.i("v", "click:" + mView.isClickable() + ";enable:" + mView.isEnabled());
    }
    //获取view当前drawable--用于判断是否通过默认属性设置背景
    Drawable mDrawable = mView.getBackground();
    //判断是否使用自定义颜色值
    boolean isSetBg = mBackgroundColor != Integer.MAX_VALUE
            || mBackgroundPressedColor != Integer.MAX_VALUE
            || mBackgroundDisabledColor != Integer.MAX_VALUE
            || mBackgroundSelectedColor != Integer.MAX_VALUE
            || mStrokeWidth > 0 || mRadius > 0
            || mTopLeftRadius > 0 || mTopLeftRadius > 0 || mBottomLeftRadius > 0 || mBottomRightRadius > 0;

    setDrawable(mBackgroundChecked, mBackgroundCheckedColor, mStrokeCheckedColor);
    setDrawable(mBackgroundSelected, mBackgroundSelectedColor, mStrokeSelectedColor);
    setDrawable(mBackground, mBackgroundColor, mStrokeColor);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
            && mRippleEnable
            && mView.isEnabled()
            && mView.isClickable()) {//5.0以上且设置水波属性并且可操作
        RippleDrawable rippleDrawable = new RippleDrawable(
                new ColorStateList(
                        new int[][]{
                                new int[]{mStatePressed},
                                new int[]{}
                        },
                        new int[]{
                                mRippleColor != Integer.MAX_VALUE ? mRippleColor : getBackColor(mBackgroundPressedColor),
                                mRippleColor
                        }
                )
                , getContentDrawable(mDrawable, isSetBg)
                , null);
        mView.setBackground(rippleDrawable);
    } else {
        if (!isSetBg) {
            return;
        }
        setDrawable(mBackgroundPressed, mBackgroundPressedColor, mStrokePressedColor);
        setDrawable(mBackgroundDisabled, mBackgroundDisabledColor, mStrokeDisabledColor);
        StateListDrawable mStateDrawable = new StateListDrawable();
        mStateDrawable.setEnterFadeDuration(mEnterFadeDuration);
        mStateDrawable.setExitFadeDuration(mExitFadeDuration);

        mStateDrawable.addState(new int[]{mStatePressed}, mBackgroundPressed);
        mStateDrawable.addState(new int[]{mStateSelected}, mBackgroundSelected);
        mStateDrawable.addState(new int[]{mStateChecked}, mBackgroundChecked);
        mStateDrawable.addState(new int[]{mStateDisabled}, mBackgroundDisabled);
        //默认状态--放置在最后否则其它状态不生效
        mStateDrawable.addState(new int[]{}, mBackground);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            mView.setBackground(mStateDrawable);
        } else {
            mView.setBackgroundDrawable(mStateDrawable);
        }
    }
    return;
}
 
Example 3
Source File: Coloring.java    From actual-number-picker with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new {@code StateListDrawable} drawable. States that should be provided are "normal",<br>
 * "clicked" (pressed) and "checked" (selected). All states are actually integer colors.<br>
 * Optionally, {@code shouldFade} can be set to false to avoid the fading effect.<br>
 * <br>
 * Note: <i>{@link Color#TRANSPARENT} can be used to supply a transparent state.</i>
 *
 * @param normal Color for the idle state
 * @param clicked Color for the clicked/pressed state
 * @param checked Color for the checked/selected state
 * @param shouldFade Set to true to enable the fading effect, false otherwise
 * @return A {@link StateListDrawable} drawable object ready for use
 */
@SuppressLint({
        "InlinedApi", "NewApi"
})
public Drawable createStateDrawable(int normal, int clicked, int checked, boolean shouldFade) {
    // init state arrays
    int[] selectedState = new int[] {
            android.R.attr.state_selected
    };
    int[] pressedState = new int[] {
            android.R.attr.state_pressed
    };
    int[] checkedState = new int[] {
            android.R.attr.state_checked
    };
    int[] focusedState = new int[] {
            android.R.attr.state_focused
    };
    int[] activatedState = new int[] {};
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        activatedState = new int[] {
                android.R.attr.state_activated
        };
    }

    // init normal state drawable
    Drawable normalDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
            normal, normal
    }).mutate();
    if (normal == Color.TRANSPARENT)
        normalDrawable.setAlpha(0);
    else
        normalDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);

    // init clicked state drawable
    Drawable clickedDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
            clicked, clicked
    }).mutate();
    if (clicked == Color.TRANSPARENT)
        clickedDrawable.setAlpha(0);
    else
        clickedDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);

    // init checked state drawable
    Drawable checkedDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
            checked, checked
    }).mutate();
    if (checked == Color.TRANSPARENT)
        checkedDrawable.setAlpha(0);
    else
        checkedDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);

    // init focused state drawable (use normal color)
    Drawable focusedDrawable = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] {
            normal, normal
    }).mutate();
    if (normal == Color.TRANSPARENT)
        focusedDrawable.setAlpha(0);
    else
        focusedDrawable.setBounds(BOUNDS, BOUNDS, BOUNDS, BOUNDS);

    // prepare state list (order of adding states is important!)
    StateListDrawable states = new StateListDrawable();
    states.addState(pressedState, clickedDrawable);
    if (!shouldFade) {
        states.addState(selectedState, clickedDrawable);
        states.addState(focusedState, focusedDrawable);
        states.addState(checkedState, checkedDrawable);
    }

    // add fade effect if applicable
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        if (shouldFade) {
            states.addState(new int[] {}, normalDrawable);
            states.setEnterFadeDuration(0);
            states.setExitFadeDuration(FADE_DURATION);
        } else {
            states.addState(activatedState, clickedDrawable);
            states.addState(new int[] {}, normalDrawable);
        }
    } else {
        states.addState(new int[] {}, normalDrawable);
    }

    return states;
}
 
Example 4
Source File: Coloring.java    From actual-number-picker with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Similar to {@link #createBackgroundDrawable(int, int, int, boolean)} but with additional {@code original} drawable parameter.
 *
 * @param context Which context to use
 * @param normal Color normal state of the drawable to this color
 * @param clickedBackground Background color of the View that will show when view is clicked
 * @param shouldFade Set to true if the state list should have a fading effect
 * @param original This drawable will be contrasted to the {@code clickedBackground} color on press
 * @return The state list drawable that is in contrast with the on-click background color
 */
@SuppressLint({
        "InlinedApi", "NewApi"
})
public Drawable createContrastStateDrawable(Context context, int normal, int clickedBackground, boolean shouldFade, Drawable original) {
    if (original == null || original instanceof StateListDrawable) {
        if (original != null) {
            Log.i(LOG_TAG, "Original drawable is already a StateListDrawable");
            original = original.getCurrent();
        }

        // overridden in previous if clause, so check again
        if (original == null) {
            return null;
        }
    }

    // init state arrays
    int[] selectedState = new int[] {
            android.R.attr.state_selected
    };
    int[] pressedState = new int[] {
            android.R.attr.state_pressed
    };
    int[] checkedState = new int[] {
            android.R.attr.state_checked
    };
    int[] activatedState = new int[] {};
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        activatedState = new int[] {
                android.R.attr.state_activated
        };
    }

    Drawable normalStateDrawable = colorDrawable(context, original, normal);
    Drawable clickedStateDrawable = colorDrawable(context, original, getContrastColor(clickedBackground));
    Drawable checkedStateDrawable = colorDrawable(context, original, getContrastColor(clickedBackground));

    // prepare state list (order of adding states is important!)
    StateListDrawable states = new StateListDrawable();
    states.addState(pressedState, clickedStateDrawable);
    if (!shouldFade) {
        states.addState(selectedState, clickedStateDrawable);
        states.addState(checkedState, checkedStateDrawable);
    }

    // add fade effect if applicable
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
        if (shouldFade) {
            states.addState(new int[] {}, normalStateDrawable);
            states.setEnterFadeDuration(0);
            states.setExitFadeDuration(FADE_DURATION);
        } else {
            states.addState(activatedState, clickedStateDrawable);
            states.addState(new int[] {}, normalStateDrawable);
        }
    } else {
        states.addState(new int[] {}, normalStateDrawable);
    }

    return states;
}