Java Code Examples for android.animation.StateListAnimator#addState()

The following examples show how to use android.animation.StateListAnimator#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: ViewUtilsLollipop.java    From GpCollapsingToolbar with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and sets a {@link StateListAnimator} with a custom elevation value
 */
@SuppressLint("PrivateResource")
static void setDefaultAppBarLayoutStateListAnimator(final View view, final float targetElevation) {
    final StateListAnimator sla = new StateListAnimator();

    // Enabled, collapsible and collapsed == elevated
    sla.addState(new int[]{android.R.attr.enabled, R.attr.state_collapsible, R.attr.state_collapsed},
            ObjectAnimator.ofFloat(view, "elevation", targetElevation));

    // Enabled and collapsible, but not collapsed != elevated
    sla.addState(new int[]{android.R.attr.enabled, R.attr.state_collapsible, -R.attr.state_collapsed},
            ObjectAnimator.ofFloat(view, "elevation", 0f));

    // Enabled but not collapsible == elevated
    sla.addState(new int[]{android.R.attr.enabled, -R.attr.state_collapsible},
            ObjectAnimator.ofFloat(view, "elevation", targetElevation));

    // Default, none elevated state
    sla.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));

    view.setStateListAnimator(sla);
}
 
Example 2
Source File: ViewUtilsLollipop.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/** Creates and sets a {@link StateListAnimator} with a custom elevation value */
static void setDefaultAppBarLayoutStateListAnimator(
    @NonNull final View view, final float elevation) {
  final int dur = view.getResources().getInteger(R.integer.app_bar_elevation_anim_duration);

  final StateListAnimator sla = new StateListAnimator();

  // Enabled and liftable, but not lifted means not elevated
  sla.addState(
      new int[] {android.R.attr.enabled, R.attr.state_liftable, -R.attr.state_lifted},
      ObjectAnimator.ofFloat(view, "elevation", 0f).setDuration(dur));

  // Default enabled state
  sla.addState(
      new int[] {android.R.attr.enabled},
      ObjectAnimator.ofFloat(view, "elevation", elevation).setDuration(dur));

  // Disabled state
  sla.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0).setDuration(0));

  view.setStateListAnimator(sla);
}
 
Example 3
Source File: StateListBuilder.java    From relight with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = VERSION_CODES.LOLLIPOP)
public static StateListBuilder<Animator, StateListAnimator> animatorBuilder() {
    return new StateListBuilder<Animator, StateListAnimator>() {
        @Override
        public StateListAnimator build() {
            StateListAnimator drawable = new StateListAnimator();
            int[][] states = getStates();
            Animator[] values = getValues(new Animator[this.values.size()]);
            for (int i = 0; i < states.length; i++) {
                drawable.addState(states[i], values[i]);
            }
            return drawable;
        }
    };
}
 
Example 4
Source File: RaiflatDelegate.java    From RaiflatButton with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void setupStateListAnimator() {
    mFlatStateListAnimator = new StateListAnimator();

    ObjectAnimator pressed = ObjectAnimator.ofFloat(this, "elevation", 0)
            .setDuration(mView.getContext().getResources()
                    .getInteger(android.R.integer.config_shortAnimTime));

    ObjectAnimator notPressed = ObjectAnimator.ofFloat(this, "elevation", mView.getElevation()
            + mView.getTranslationZ()).setDuration(mView.getContext().getResources()
            .getInteger(android.R.integer.config_shortAnimTime));


    notPressed.setStartDelay(100);

    mFlatStateListAnimator.addState(new int[]{android.R.attr.state_pressed,
            android.R.attr.state_enabled}, pressed);

    mFlatStateListAnimator.addState(new int[]{android.R.attr.state_enabled}, notPressed);

    mFlatStateListAnimator.addState(new int[]{},
            ObjectAnimator.ofFloat(this, "elevation", 0).setDuration(0));

    mView.setStateListAnimator(mFlatStateListAnimator);
}
 
Example 5
Source File: FloatingActionButtonImplLollipop.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
void onElevationsChanged(
    final float elevation,
    final float hoveredFocusedTranslationZ,
    final float pressedTranslationZ) {

  if (Build.VERSION.SDK_INT == VERSION_CODES.LOLLIPOP) {
    // Animations produce NPE in version 21. Bluntly set the values instead in
    // #onDrawableStateChanged (matching the logic in the animations below).
    view.refreshDrawableState();
  } else {
    final StateListAnimator stateListAnimator = new StateListAnimator();

    // Animate elevation and translationZ to our values when pressed, focused, and hovered
    stateListAnimator.addState(
        PRESSED_ENABLED_STATE_SET, createElevationAnimator(elevation, pressedTranslationZ));
    stateListAnimator.addState(
        HOVERED_FOCUSED_ENABLED_STATE_SET,
        createElevationAnimator(elevation, hoveredFocusedTranslationZ));
    stateListAnimator.addState(
        FOCUSED_ENABLED_STATE_SET,
        createElevationAnimator(elevation, hoveredFocusedTranslationZ));
    stateListAnimator.addState(
        HOVERED_ENABLED_STATE_SET,
        createElevationAnimator(elevation, hoveredFocusedTranslationZ));

    // Animate translationZ to 0 if not pressed, focused, or hovered
    AnimatorSet set = new AnimatorSet();
    List<Animator> animators = new ArrayList<>();
    animators.add(ObjectAnimator.ofFloat(view, "elevation", elevation).setDuration(0));
    if (Build.VERSION.SDK_INT >= 22 && Build.VERSION.SDK_INT <= 24) {
      // This is a no-op animation which exists here only for introducing the duration
      // because setting the delay (on the next animation) via "setDelay" or "after"
      // can trigger a NPE between android versions 22 and 24 (due to a framework
      // bug). The issue has been fixed in version 25.
      animators.add(
          ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, view.getTranslationZ())
              .setDuration(ELEVATION_ANIM_DELAY));
    }
    animators.add(
        ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, 0f)
            .setDuration(ELEVATION_ANIM_DURATION));
    set.playSequentially(animators.toArray(new Animator[0]));
    set.setInterpolator(ELEVATION_ANIM_INTERPOLATOR);
    stateListAnimator.addState(ENABLED_STATE_SET, set);

    // Animate everything to 0 when disabled
    stateListAnimator.addState(EMPTY_STATE_SET, createElevationAnimator(0f, 0f));

    view.setStateListAnimator(stateListAnimator);
  }

  if (shouldAddPadding()) {
    updatePadding();
  }
}