下面列出了怎么用android.view.animation.AnimationUtils的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 单击时,隐藏头和尾
*/
@Override
public void onImageSingleTap() {
if (topBar.getVisibility() == View.VISIBLE) {
topBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.top_out));
bottomBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out));
topBar.setVisibility(View.GONE);
bottomBar.setVisibility(View.GONE);
tintManager.setStatusBarTintResource(Color.TRANSPARENT);//通知栏所需颜色
//给最外层布局加上这个属性表示,Activity全屏显示,且状态栏被隐藏覆盖掉。
// if (Build.VERSION.SDK_INT >= 16) content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
} else {
topBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.top_in));
bottomBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in));
topBar.setVisibility(View.VISIBLE);
bottomBar.setVisibility(View.VISIBLE);
tintManager.setStatusBarTintResource(R.color.ip_color_primary_dark);//通知栏所需颜色
//Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住
// if (Build.VERSION.SDK_INT >= 16) content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
void stopLoadingAnimation() {
final View preferenceView = getView();
final Activity activity = getActivity();
if (null == activity) return;
final View loadingView = mLoadingView;
final MenuItem updateNowMenu = mUpdateNowMenu;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
loadingView.setVisibility(View.GONE);
preferenceView.setVisibility(View.VISIBLE);
loadingView.startAnimation(AnimationUtils.loadAnimation(
activity, android.R.anim.fade_out));
preferenceView.startAnimation(AnimationUtils.loadAnimation(
activity, android.R.anim.fade_in));
// The menu is created by the framework asynchronously after the activity,
// which means it's possible to have the activity running but the menu not
// created yet - hence the necessity for a null check here.
if (null != updateNowMenu) {
updateNowMenu.setTitle(R.string.check_for_updates_now);
}
}
});
}
private void init(AttributeSet attrs) {
if (isInEditMode()) {
return;
}
LayoutInflater.from(getContext()).inflate(R.layout.bt_animated_button_view, this);
mViewAnimator = findViewById(R.id.bt_view_animator);
mButton = findViewById(R.id.bt_button);
mButton.setOnClickListener(this);
mViewAnimator.setInAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_in));
mViewAnimator.setOutAnimation(AnimationUtils.loadAnimation(getContext(), android.R.anim.fade_out));
TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.bt_AnimatedButtonAttributes);
mButton.setText(attributes.getString(R.styleable.bt_AnimatedButtonAttributes_bt_buttonText));
attributes.recycle();
setFocusable(true);
setFocusableInTouchMode(true);
}
public void incorrectAnimation() {
incorrect.setVisibility(View.VISIBLE);
//Animation
final Animation myAnim = AnimationUtils.loadAnimation(this, R.anim.bounce);
//Use bounce interpolator with amplitude 0.2 and frequency 20
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.1, 60);
myAnim.setInterpolator(interpolator);
incorrect.startAnimation(myAnim);
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
incorrect.setVisibility(View.INVISIBLE); //for interval...
}
};
handler.postDelayed(runnable, 100); //for initial delay..*/
}
public void showConnectingAnimation() {
runOnUiThread(new Runnable() {
@Override
public void run() {
scanningGradientContainer.setVisibility(View.GONE);
Animation connectingGradientAnimation = AnimationUtils.loadAnimation(BrowserActivity.this,
R.anim.connection_translate_right);
connectingContainer.setVisibility(View.VISIBLE);
connectingGradientContainer.startAnimation(connectingGradientAnimation);
Animation connectingBarFlyIn = AnimationUtils.loadAnimation(BrowserActivity.this,
R.anim.scanning_bar_fly_in);
connectingBarContainer.startAnimation(connectingBarFlyIn);
}
});
}
private void updateBlockNumberHead() {
final Handler handler = new Handler();
final Activity myActivity = this;
final Runnable updateTask = new Runnable() {
@Override
public void run() {
if (Application.isConnected()) {
ivSocketConnected.setImageResource(R.drawable.icon_connecting);
tvBlockNumberHead.setText(Application.blockHead);
ivSocketConnected.clearAnimation();
} else {
ivSocketConnected.setImageResource(R.drawable.icon_disconnecting);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(myActivity.getApplicationContext(), R.anim.flash);
ivSocketConnected.startAnimation(myFadeInAnimation);
}
handler.postDelayed(this, 1000);
}
};
handler.postDelayed(updateTask, 1000);
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
Animation animation = super.onCreateAnimation(transit, enter, nextAnim);
if (animation == null && nextAnim != 0) {
try {
animation = AnimationUtils.loadAnimation(getActivity(), nextAnim);
} catch (Resources.NotFoundException e) {
return null;
}
}
if (animation != null) {
final AnimationSet animSet = new AnimationSet(true);
animSet.addAnimation(animation);
this.animationSet = animSet;
return animSet;
} else {
return null;
}
}
public void hideFab() {
fab.clearAnimation();
Animation animation = AnimationUtils.loadAnimation(this, R.anim.hide_fab);
fab.startAnimation(animation);
CoordinatorLayout.LayoutParams params =
(CoordinatorLayout.LayoutParams) fab.getLayoutParams();
FloatingActionButton.Behavior behavior =
(FloatingActionButton.Behavior) params.getBehavior();
if (behavior != null) {
behavior.setAutoHideEnabled(false);
}
fab.hide();
}
public void animateExit(final int duration) {
final Animation fadeOut = AnimationUtils.loadAnimation(getContext(), R.anim.px_fade_out);
fadeOut.setDuration(duration);
if (showingBigLogo) {
bigHeaderDescriptor.startAnimation(fadeOut);
} else {
toolbarElementDescriptor.startAnimation(
AnimationUtils.loadAnimation(getContext(), R.anim.px_summary_slide_up_out));
}
detailRecyclerView.startAnimation(fadeOut);
findViewById(R.id.separator).startAnimation(fadeOut);
startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.px_summary_translate_out));
totalAmountDescriptor.startAnimation(fadeOut);
}
public void notifyFinalPositionExtended(int position) {
mOver = 0;
mFinished = false;
mDuration = mDuration - (int) (mStartTime - AnimationUtils.currentAnimationTimeMillis());
if (mDuration < 50) {
mDuration = 50;
}
mSplineDuration = mDuration;
mStartTime = AnimationUtils.currentAnimationTimeMillis();
mStart = mCurrentPosition;
mFinal = position;
mState = SPLINE;
mSplineDistance = mFinal - mStart;
}
/** 单击时,隐藏头和尾 */
@Override
public void onImageSingleTap() {
if (topBar.getVisibility() == View.VISIBLE) {
topBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.atom_ui_top_out));
topBar.setVisibility(View.GONE);
tintManager.setStatusBarTintResource(Color.TRANSPARENT);//通知栏所需颜色
//给最外层布局加上这个属性表示,Activity全屏显示,且状态栏被隐藏覆盖掉。
// if (Build.VERSION.SDK_INT >= 16) content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
} else {
topBar.setAnimation(AnimationUtils.loadAnimation(this, R.anim.atom_ui_top_in));
topBar.setVisibility(View.VISIBLE);
tintManager.setStatusBarTintResource(R.color.ip_color_primary_dark);//通知栏所需颜色
//Activity全屏显示,但状态栏不会被隐藏覆盖,状态栏依然可见,Activity顶端布局部分会被状态遮住
// if (Build.VERSION.SDK_INT >= 16) content.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}
@SuppressLint("RestrictedApi")
private void animateIn(FloatingActionButton button) {
button.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(button).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
.setInterpolator(INTERPOLATOR).withLayer().setListener(null)
.start();
}
else {
Animation anim = AnimationUtils.loadAnimation(button.getContext(), R.anim.fab_in);
anim.setDuration(200L);
anim.setInterpolator(INTERPOLATOR);
button.startAnimation(anim);
}
}
private void init(Context context, AttributeSet attrs) {
hadAttrs = (attrs != null);
if (attrs != null) {
TypedArray styledAttributes = getContext().getApplicationContext().obtainStyledAttributes(attrs, R.styleable.LoadingBananaPeelView);
// Load the resource IDs for each property from the styled attributes set
bananaPeelContentViewLayoutResourceId = styledAttributes.getResourceId(R.styleable.LoadingBananaPeelView_bananaPeelContentViewLayoutResource, 0);
bananaPeelViewResourceId = styledAttributes.getResourceId(R.styleable.LoadingBananaPeelView_bananaPeelViewResource, 0);
bananaPeelDefaultMessageResourceId = styledAttributes.getResourceId(R.styleable.LoadingBananaPeelView_bananaPeelDefaultMessageStringResource, 0);
bananaPeelDefaultImageResourceId = styledAttributes.getResourceId(R.styleable.LoadingBananaPeelView_bananaPeelDefaultImageResource, 0);
// Set the default animations for changing between View Flipper children
this.setAnimateFirstView(false);
this.setInAnimation(AnimationUtils.loadAnimation(context, R.anim.abc_fade_in));
this.setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.abc_fade_out));
styledAttributes.recycle();
}
}
private void init(Context context) {
setOverScrollMode(View.OVER_SCROLL_NEVER);
setOnScrollListener(this);
this.mHeaderView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.imitateautohome_header, null, false);
this.mAutoHome = ViewFinder.findViewById(mHeaderView, R.id.sdi_autohome);
this.mPullToRefreshTV = ViewFinder.findViewById(mHeaderView, R.id.sdi_pulltorefresh_tv);
mAnimContainer = ViewFinder.findViewById(mHeaderView, R.id.sdi_animcontainer_fl);
mAutoHomeAnim = ViewFinder.findViewById(mHeaderView, R.id.sdi_animpointer_iv);
mAnimation = AnimationUtils.loadAnimation(context, R.anim.sda_autohome_pointer_rotate);
measureView(mHeaderView);
addHeaderView(mHeaderView);
mHeaderViewHeight = mHeaderView.getMeasuredHeight();
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);
mState = DONE;
mIsEnd = true;
mIsRefreable = false;
}
public void testReplaceView() throws Exception {
EditText mockEditText = mock(EditText.class);
ViewGroup mockViewGroup = mock(ViewGroup.class);
LinearLayout mockNewContainer = mock(LinearLayout.class);
int mockInt = PowerMockito.mock(Integer.class);
TextView mockTextView = mock(TextView.class);
PowerMockito.mockStatic(AnimationUtils.class);
when(mMockValidationHolder.getEditText()).thenReturn(mockEditText);
when(mMockValidationHolder.getErrMsg()).thenReturn(PowerMockito.mock(String.class));
when(mockEditText.getParent()).thenReturn(mockViewGroup);
when(mockViewGroup.indexOfChild(mockEditText)).thenReturn(mockInt);
when(mockEditText.getPaddingLeft()).thenReturn(PowerMockito.mock(Integer.class));
when(mockEditText.getPaddingRight()).thenReturn(PowerMockito.mock(Integer.class));
PowerMockito.whenNew(LinearLayout.class).withArguments(mMockContext).thenReturn(mockNewContainer);
PowerMockito.whenNew(TextView.class).withArguments(mMockContext).thenReturn(mockTextView);
PowerMockito.when(AnimationUtils.loadAnimation(any(Context.class), anyInt())).thenReturn(mock(Animation.class));
assertEquals(mockTextView, Whitebox.invokeMethod(mSpiedUnderlabelValidator, "replaceView", mMockValidationHolder));
verify(mockViewGroup).addView(mockNewContainer, mockInt);
}
private void invalidateField(final EditText editText){
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), getResources().getColor(R.color.colorHeader), getResources().getColor(R.color.brightRed));
colorAnimation.setDuration(200);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
editText.setTextColor((int) animation.getAnimatedValue());
}
});
Animation invalidShake = AnimationUtils.loadAnimation(this, R.anim.invalid_shake);
invalidShake.setInterpolator(new CycleInterpolator(7F));
colorAnimation.start();
editText.startAnimation(invalidShake);
}
@Override public void showError() {
viewState.setShowError();
setFormEnabled(true);
loginButton.setProgress(0);
if (!isRestoringViewState()) {
// Enable animations only if not restoring view state
loginForm.clearAnimation();
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
loginForm.startAnimation(shake);
}
errorView.setVisibility(View.VISIBLE);
}
public static Interpolator getFastOutLinearInInterpolator(Context context) {
if (fastOutLinearIn == null) {
fastOutLinearIn = AnimationUtils.loadInterpolator(context,
android.R.interpolator.fast_out_linear_in);
}
return fastOutLinearIn;
}
/**
* Load anim and set duration
*
* @param anim
* @param duration
* @return
*/
public static Animation loadAnimation(Context context, int anim, int duration)
{
Animation animation = AnimationUtils.loadAnimation(context, anim);
animation.setDuration(duration);
return animation;
}
public static Interpolator getFastOutSlowInInterpolator(Context context) {
if (fastOutSlowIn == null) {
fastOutSlowIn = AnimationUtils.loadInterpolator(context,
android.R.interpolator.fast_out_slow_in);
}
return fastOutSlowIn;
}
private Animation getAnimationForDirection (SpinDirection direction) {
Animation rotation = AnimationUtils.loadAnimation(getContext(), direction == SpinDirection.CLOCKWISE ? R.anim.clockwise_rotation : R.anim.counterclockwise_rotation);
rotation.setDuration(spinDurationMs);
rotation.setRepeatCount(Animation.INFINITE);
return rotation;
}
private void animator(final ImageButton imageButton){
Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.color_item);
animation.setInterpolator(new AccelerateInterpolator());
imageButton.setAnimation(animation);
imageButton.setVisibility(View.VISIBLE);
animation.start();
}
public MsgVoiceHolder(View itemView) {
super(itemView);
view1 = itemView.findViewById(R.id.view1);
voiceImg = (VoiceImg) itemView.findViewById(R.id.voicemsg);
loadImg = (ImageView) itemView.findViewById(R.id.img1);
if (loadImg != null) {
Animation animation = AnimationUtils.loadAnimation(context, R.anim.loading_white);
loadImg.startAnimation(animation);
}
}
/** 初始化数据 */
private void prepare(CoordinatorLayout parent, View child, View dependency) {
mDependStartX = (int) dependency.getX();
mDependStartY = (int) dependency.getY();
mDependStartWidth = dependency.getWidth();
mDependStartHeight = dependency.getHeight();
mStartX = (int) child.getX();
mStartY = (int) child.getY();
mStartWidth = child.getWidth();
mStartHeight = child.getHeight();
mStartAlpha = child.getAlpha();
mStartRotateX = child.getRotationX();
mStartRotateY = child.getRotationY();
//特殊处理y方向变化
if (mDependTargetY == UNSPECIFIED_INT && dependency instanceof AppBarLayout) {
mDependTargetY = ((AppBarLayout) dependency).getTotalScrollRange();
}
// 背景颜色渐变
if (child.getBackground() instanceof ColorDrawable) mStartBackgroundColor = ((ColorDrawable) child.getBackground()).getColor();
// 自定义动画
if (mAnimationId != 0) {
mAnimation = AnimationUtils.loadAnimation(child.getContext(), mAnimationId);
mAnimation.initialize(child.getWidth(), child.getHeight(), parent.getWidth(), parent.getHeight());
}
// 兼容5.0以上的沉浸模式
if (Build.VERSION.SDK_INT > 16 && parent.getFitsSystemWindows() && targetY != UNSPECIFIED_INT) {
targetY += getStatusBarHeight(parent.getContext());
}
isPrepared = true;
}
private void setAnimation(View viewToAnimate, int position) {
if (position > lastPosition) {
Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), R
.anim.item_bottom_in);
viewToAnimate.startAnimation(animation);
lastPosition = position;
}
}
public static Interpolator getLinearOutSlowInInterpolator(Context context) {
if (linearOutSlowIn == null) {
linearOutSlowIn = AnimationUtils.loadInterpolator(context,
android.R.interpolator.linear_out_slow_in);
}
return linearOutSlowIn;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater
.inflate(R.layout.zen_gallery_item, null);
}
ImageView GallerySelector = (ImageView) convertView
.findViewById(R.id.zen_gallery_selector);
final ImageView imageView = (ImageView) convertView
.findViewById(R.id.zen_gallery_item);
imageLoader.displayImage("file://" + imageUrls.get(position),
imageView, options, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(Bitmap loadedImage) {
Animation anim = AnimationUtils
.loadAnimation(ZenGalleryActivity.this,
R.anim.zen_fade_in);
imageView.setAnimation(anim);
anim.start();
}
});
int selector = (mSparseBooleanArray.get(position) ? R.drawable.checkbox_selected : R.drawable.checkbox_normal);
GallerySelector.setTag(position);
GallerySelector.setImageResource(selector);
return convertView;
}
/**
* Computes the current scroll deltas. This usually only be called after
* starting the scroller with {@link #start()}.
*
* @see #getDeltaX()
* @see #getDeltaY()
*/
public void computeScrollDelta() {
if (mDeltaTime == 0) {
throw new RuntimeException("Cannot compute scroll delta before calling start()");
}
final long currentTime = AnimationUtils.currentAnimationTimeMillis();
final float value = getValueAt(currentTime);
final float scale = interpolateValue(value);
final long elapsedSinceDelta = currentTime - mDeltaTime;
mDeltaTime = currentTime;
mDeltaX = (int) (elapsedSinceDelta * scale * mTargetVelocityX);
mDeltaY = (int) (elapsedSinceDelta * scale * mTargetVelocityY);
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (getParentFragment() != null && (getParentFragment().isDetached() || getParentFragment().isRemoving())) {
return AnimationUtils.loadAnimation(getContext(), R.anim.stay);
}
return super.onCreateAnimation(transit, enter, nextAnim);
}
private void updateEmptyView() {
if (isEmpty()) {
if (emptyView.getVisibility() == View.GONE) {
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);
if (animation != null) {
emptyView.startAnimation(animation);
}
}
emptyView.setVisibility(View.VISIBLE);
} else {
emptyView.setVisibility(View.GONE);
}
}