Java Code Examples for android.view.View#setStateListAnimator()

The following examples show how to use android.view.View#setStateListAnimator() . 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: MountState.java    From litho with Apache License 2.0 6 votes vote down vote up
private static void setViewStateListAnimator(View view, ViewNodeInfo viewNodeInfo) {
  StateListAnimator stateListAnimator = viewNodeInfo.getStateListAnimator();
  final int stateListAnimatorRes = viewNodeInfo.getStateListAnimatorRes();
  if (stateListAnimator == null && stateListAnimatorRes == 0) {
    return;
  }
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    throw new IllegalStateException(
        "MountState has a ViewNodeInfo with stateListAnimator, "
            + "however the current Android version doesn't support stateListAnimator on Views");
  }
  if (stateListAnimator == null) {
    stateListAnimator =
        AnimatorInflater.loadStateListAnimator(view.getContext(), stateListAnimatorRes);
  }
  view.setStateListAnimator(stateListAnimator);
}
 
Example 2
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 3
Source File: ViewUtilsLollipop.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
static void setStateListAnimatorFromAttrs(
    @NonNull View view, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
  final Context context = view.getContext();
  final TypedArray a =
      ThemeEnforcement.obtainStyledAttributes(
          context, attrs, STATE_LIST_ANIM_ATTRS, defStyleAttr, defStyleRes);
  try {
    if (a.hasValue(0)) {
      StateListAnimator sla =
          AnimatorInflater.loadStateListAnimator(context, a.getResourceId(0, 0));
      view.setStateListAnimator(sla);
    }
  } finally {
    a.recycle();
  }
}
 
Example 4
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 5
Source File: MountState.java    From litho with Apache License 2.0 5 votes vote down vote up
private static void unsetViewStateListAnimator(View view, ViewNodeInfo viewNodeInfo) {
  if (viewNodeInfo.getStateListAnimator() == null
      && viewNodeInfo.getStateListAnimatorRes() == 0) {
    return;
  }
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    throw new IllegalStateException(
        "MountState has a ViewNodeInfo with stateListAnimator, "
            + "however the current Android version doesn't support stateListAnimator on Views");
  }
  view.setStateListAnimator(null);
}
 
Example 6
Source File: ViewUtilsLollipop.java    From GpCollapsingToolbar with Apache License 2.0 5 votes vote down vote up
static void setStateListAnimatorFromAttrs(View view, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    final Context context = view.getContext();
    final TypedArray a = context.obtainStyledAttributes(attrs, STATE_LIST_ANIM_ATTRS,
            defStyleAttr, defStyleRes);
    try {
        if (a.hasValue(0)) {
            StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context, a.getResourceId(0, 0));
            view.setStateListAnimator(sla);
        }
    } finally {
        a.recycle();
    }
}
 
Example 7
Source File: RaiflatUtils.java    From RaiflatButton with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void setupRaiflat(View view) {
    StateListAnimator stateListAnimator
            = AnimatorInflater.loadStateListAnimator(view.getContext(),
            R.drawable.raiflatbutton_statelistanimator);
    view.setStateListAnimator(stateListAnimator);
}
 
Example 8
Source File: AMViewCompat.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
@Override
public void setStateListAnimator(View view, StateListAnimator stateListAnimator) {
    view.setStateListAnimator(stateListAnimator);
}
 
Example 9
Source File: AMViewCompat.java    From ProjectX with Apache License 2.0 4 votes vote down vote up
@Override
public void setStateListAnimator(View view, int id) {
    view.setStateListAnimator(AnimatorInflater.loadStateListAnimator(view.getContext(), id));
}