下面列出了android.view.View#setStateListAnimator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
/**
* 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);
}
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();
}
}
/** 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);
}
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);
}
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();
}
}
@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);
}
@Override
public void setStateListAnimator(View view, StateListAnimator stateListAnimator) {
view.setStateListAnimator(stateListAnimator);
}
@Override
public void setStateListAnimator(View view, int id) {
view.setStateListAnimator(AnimatorInflater.loadStateListAnimator(view.getContext(), id));
}