下面列出了怎么用androidx.annotation.AnimatorRes的API类实例代码及写法,或者点击链接到github查看源代码。
/** Inflates an instance of MotionSpec from the given animator resource. */
@Nullable
public static MotionSpec createFromResource(@NonNull Context context, @AnimatorRes int id) {
try {
Animator animator = AnimatorInflater.loadAnimator(context, id);
if (animator instanceof AnimatorSet) {
AnimatorSet set = (AnimatorSet) animator;
return createSpecFromAnimators(set.getChildAnimations());
} else if (animator != null) {
List<Animator> animators = new ArrayList<>();
animators.add(animator);
return createSpecFromAnimators(animators);
} else {
return null;
}
} catch (Exception e) {
Log.w(TAG, "Can't load animation resource ID #0x" + Integer.toHexString(id), e);
return null;
}
}
/**
* 获取 Animation
* @param id resource identifier
* @return XmlResourceParser
*/
public static XmlResourceParser getAnimation(@AnimatorRes @AnimRes final int id) {
try {
return DevUtils.getContext().getResources().getAnimation(id);
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getAnimation");
}
return null;
}
/**
* Play an animator animation on a view.
*
* @param view The view to play the animation.
* @param animator The animator to be applied on the view.
*/
public static void playAnimation(@NonNull View view, @AnimatorRes int animator) {
AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater
.loadAnimator(view.getContext(), animator);
animatorSet.setTarget(view);
animatorSet.start();
}
@NonNull
@Override
protected FabTransformationSpec onCreateMotionSpec(Context context, boolean expanded) {
@AnimatorRes int specRes;
if (expanded) {
specRes = R.animator.mtrl_fab_transformation_sheet_expand_spec;
} else {
specRes = R.animator.mtrl_fab_transformation_sheet_collapse_spec;
}
FabTransformationSpec spec = new FabTransformationSpec();
spec.timings = MotionSpec.createFromResource(context, specRes);
spec.positioning = new Positioning(Gravity.CENTER, 0f, 0f);
return spec;
}
@Nullable
private static Animator loadAnimator(@NonNull Context context,
@NonNull TypedArray typedArray,
int index,
int fallbackIndex,
@AnimatorRes int defaultValue) {
return AnimatorInflater.loadAnimator(context,
typedArray.getResourceId(index, typedArray.getResourceId(fallbackIndex, defaultValue)));
}
public Builder animator(@AnimatorRes int animatorResId) {
mConfig.animatorResId = animatorResId;
return this;
}
public Builder animatorReverse(@AnimatorRes int animatorReverseResId) {
mConfig.animatorReverseResId = animatorReverseResId;
return this;
}
/**
* Updates the motion spec for the show animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_showMotionSpec
*/
public void setShowMotionSpecResource(@AnimatorRes int id) {
setShowMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Updates the motion spec for the hide animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_hideMotionSpec
*/
public void setHideMotionSpecResource(@AnimatorRes int id) {
setHideMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Updates the motion spec for the extend animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_extendMotionSpec
*/
public void setExtendMotionSpecResource(@AnimatorRes int id) {
setExtendMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Updates the motion spec for the shrink animation.
*
* @attr ref com.google.android.material.R.styleable#ExtendedFloatingActionButton_shrinkMotionSpec
*/
public void setShrinkMotionSpecResource(@AnimatorRes int id) {
setShrinkMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Updates the motion spec for the show animation.
*
* @attr ref com.google.android.material.R.styleable#FloatingActionButton_showMotionSpec
*/
public void setShowMotionSpecResource(@AnimatorRes int id) {
setShowMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Updates the motion spec for the hide animation.
*
* @attr ref com.google.android.material.R.styleable#FloatingActionButton_hideMotionSpec
*/
public void setHideMotionSpecResource(@AnimatorRes int id) {
setHideMotionSpec(MotionSpec.createFromResource(getContext(), id));
}
/**
* Sets this chip's show motion spec using a resource id.
*
* @param id The resource id of this chip's show motion spec.
* @attr ref com.google.android.material.R.styleable#Chip_showMotionSpec
*/
public void setShowMotionSpecResource(@AnimatorRes int id) {
setShowMotionSpec(MotionSpec.createFromResource(context, id));
}
/**
* Sets this chip's hide motion spec using a resource id.
*
* @param id The resource id of this chip's hide motion spec.
* @attr ref com.google.android.material.R.styleable#Chip_hideMotionSpec
*/
public void setHideMotionSpecResource(@AnimatorRes int id) {
setHideMotionSpec(MotionSpec.createFromResource(context, id));
}
/**
* Sets this chip's show motion spec using a resource id.
*
* @param id The resource id of this chip's show motion spec.
* @attr ref com.google.android.material.R.styleable#Chip_showMotionSpec
*/
public void setShowMotionSpecResource(@AnimatorRes int id) {
if (chipDrawable != null) {
chipDrawable.setShowMotionSpecResource(id);
}
}
/**
* Sets this chip's hide motion spec using a resource id.
*
* @param id The resource id of this chip's hide motion spec.
* @attr ref com.google.android.material.R.styleable#Chip_hideMotionSpec
*/
public void setHideMotionSpecResource(@AnimatorRes int id) {
if (chipDrawable != null) {
chipDrawable.setHideMotionSpecResource(id);
}
}
@AnimatorRes int getDefaultMotionSpecResource();