下面列出了怎么用android.view.animation.Animation的API类实例代码及写法,或者点击链接到github查看源代码。
private Animation getAnimationSet(boolean fromXML) {
if (fromXML) {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.view_animation);
return animation;
} else {
AnimationSet innerAnimationSet = new AnimationSet(true);
innerAnimationSet.setInterpolator(new BounceInterpolator());
innerAnimationSet.setStartOffset(1000);
innerAnimationSet.addAnimation(getScaleAnimation());
innerAnimationSet.addAnimation(getTranslateAnimation());
AnimationSet animationSet = new AnimationSet(true);
animationSet.setInterpolator(new LinearInterpolator());
animationSet.addAnimation(getAlphaAnimation());
animationSet.addAnimation(getRotateAnimation());
animationSet.addAnimation(innerAnimationSet);
return animationSet;
}
}
@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
if (!enter) {
// React Native will flush the UI cache as soon as we unmount it. This will cause the view to
// disappear unless we delay it until after the fragment animation.
if (transit == FragmentTransaction.TRANSIT_NONE && nextAnim == 0) {
reactRootView.unmountReactApplication();
} else {
contentContainer.unmountReactApplicationAfterAnimation(reactRootView);
}
reactRootView = null;
}
if (getActivity() instanceof ScreenCoordinatorComponent) {
ScreenCoordinator screenCoordinator =
((ScreenCoordinatorComponent) getActivity()).getScreenCoordinator();
if (screenCoordinator != null) {
// In some cases such as TabConfig, the screen may be loaded before there is a screen
// coordinator but it doesn't live inside of any back stack and isn't visible.
return screenCoordinator.onCreateAnimation(transit, enter, nextAnim);
}
}
return null;
}
private void initView(Context context) {
// Initial set header view height 0
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0);
mContainer = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.vw_header, null);
addView(mContainer, lp);
setGravity(Gravity.BOTTOM);
mArrowImageView = (ImageView) findViewById(R.id.header_arrow);
mHintTextView = (TextView) findViewById(R.id.header_hint_text);
mProgressBar = (ProgressBar) findViewById(R.id.header_progressbar);
mRotateUpAnim = new RotateAnimation(0.0f, -180.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateUpAnim.setFillAfter(true);
mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
mRotateDownAnim.setFillAfter(true);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
final SimpleDraweeView draweeView = (SimpleDraweeView) view.findViewById(R.id.drawee_view);
draweeView.setActualImageResource(R.drawable.logo);
draweeView.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
final RotateAnimation rotateAnimation =
new RotateAnimation(
0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
draweeView.startAnimation(rotateAnimation);
}
});
final Button buttonGitHub = (Button) view.findViewById(R.id.button_github);
setUriIntent(buttonGitHub, URL_GITHUB);
final Button buttonDocumentation = (Button) view.findViewById(R.id.button_documentation);
setUriIntent(buttonDocumentation, URL_DOCUMENTATION);
}
/**
* 折叠
*
* @param view 视图
*/
private void collapse(final View view) {
final int measuredHeight = view.getMeasuredHeight();
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.e("TAG", "interpolatedTime = " + interpolatedTime);
if (interpolatedTime == 1) {
mContentRelativeLayout.setVisibility(GONE);
} else {
view.getLayoutParams().height = measuredHeight - (int) (measuredHeight * interpolatedTime);
view.requestLayout();
}
}
};
animation.setDuration(duration);
view.startAnimation(animation);
}
/**
* <p>对 View 做透明度变化的进场动画。</p>
* <p>相关方法 {@link #fadeOut(View, int, Animation.AnimationListener, boolean)}</p>
*
* @param view 做动画的 View
* @param duration 动画时长(毫秒)
* @param listener 动画回调
* @param isNeedAnimation 是否需要动画
*/
public static AlphaAnimation fadeIn(View view, int duration, Animation.AnimationListener listener, boolean isNeedAnimation) {
if (view == null) {
return null;
}
if (isNeedAnimation) {
view.setVisibility(View.VISIBLE);
AlphaAnimation alpha = new AlphaAnimation(0, 1);
alpha.setInterpolator(new DecelerateInterpolator());
alpha.setDuration(duration);
alpha.setFillAfter(true);
if (listener != null) {
alpha.setAnimationListener(listener);
}
view.startAnimation(alpha);
return alpha;
} else {
view.setAlpha(1);
view.setVisibility(View.VISIBLE);
return null;
}
}
public void startAnimate(int count)
{
Animation fadeIn = new AlphaAnimation(0.2f, 1f);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1f, 0.2f);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
final AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(fadeIn);
animationSet.addAnimation(fadeOut);
animationSet.setRepeatCount(count);
animationSet.setRepeatMode(Animation.REVERSE);
setAnimation(animationSet);
animationSet.start();
}
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 FlipLoadingLayout(Context context, final Mode mode, final Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
final int rotateAngle = mode == Mode.PULL_FROM_START ? -180 : 180;
mRotateAnimation = new RotateAnimation(0, rotateAngle, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
mRotateAnimation.setFillAfter(true);
mResetRotateAnimation = new RotateAnimation(rotateAngle, 0, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mResetRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mResetRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
mResetRotateAnimation.setFillAfter(true);
}
public FlipLoadingLayout(Context context, final Mode mode, final Orientation scrollDirection, TypedArray attrs) {
super(context, mode, scrollDirection, attrs);
final int rotateAngle = mode == Mode.PULL_FROM_START ? -180 : 180;
mRotateAnimation = new RotateAnimation(0, rotateAngle, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
mRotateAnimation.setFillAfter(true);
mResetRotateAnimation = new RotateAnimation(rotateAngle, 0, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mResetRotateAnimation.setInterpolator(ANIMATION_INTERPOLATOR);
mResetRotateAnimation.setDuration(FLIP_ANIMATION_DURATION);
mResetRotateAnimation.setFillAfter(true);
}
private void initView() {
scanPreview = (SurfaceView) findViewById(R.id.capture_preview);
scanContainer = (RelativeLayout) findViewById(R.id.capture_container);
scanCropView = (RelativeLayout) findViewById(R.id.capture_crop_view);
scanLine = (ImageView) findViewById(R.id.capture_scan_line);
findViewById(R.id.capture_imageview_back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
isHasSurface = false;
beepManager = new BeepManager(this);
TranslateAnimation animation = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation
.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT,
0.9f);
animation.setDuration(3000);
animation.setRepeatCount(-1);
animation.setRepeatMode(Animation.RESTART);
scanLine.startAnimation(animation);
}
public ExProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.GREEN);
mRect = new Rect();
mRectWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
getContext().getResources().getDisplayMetrics());
mRectHeight = 4 * mRectWidth;
mGap = (int) (mRectWidth * 0.8f);
ObjectAnimator animator = ObjectAnimator.ofInt(this, mIndex, 0, 1, 2, 3, 4, 5);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(750);
animator.setRepeatMode(Animation.RESTART);
animator.setRepeatCount(Animation.INFINITE);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
invalidate();
}
});
animator.start();
}
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 void onAnimationEnd(Animation animation) {
animation.setAnimationListener(null);
final Displayable oldView = this.oldView.get();
final Animator animator = this.animator.get();
if (oldView != null && animator != null) {
// Make sure to post actions on UI thread
oldView.getView().getHandler().post(new Runnable() {
public void run() {
animator.clearAnimation();
oldView.destroy();
animator.setAnimation();
}
});
}
}
/**
* Same animation that FloatingActionButton.Behavior
* uses to show the FAB when the AppBarLayout enters
*
* @param floatingActionButton FAB
*/
//
private void animateIn(FloatingActionButton floatingActionButton) {
if (SmartphonePreferencesHandler.getUseOptionsMenuInsteadOfFAB()) {
return;
}
floatingActionButton.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= 14) {
ViewCompat.animate(floatingActionButton).scaleX(1.0F).scaleY(1.0F).alpha(1.0F)
.setInterpolator(INTERPOLATOR).withLayer().setListener(null)
.start();
} else {
Animation anim = AnimationUtils.loadAnimation(floatingActionButton.getContext(), android.R.anim.fade_in);
anim.setDuration(200L);
anim.setInterpolator(INTERPOLATOR);
floatingActionButton.startAnimation(anim);
}
}
/**
* Collapse a view which has already expanded
*
* @param v
*/
public static void collapseViews(final View v) {
final int initialHeight = v.getMeasuredHeight();
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
} else {
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime);
v.requestLayout();
}
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density) * 1);
v.startAnimation(a);
}
@Override
protected void onSizeChanged(int width, int height, int oldw, int oldh) {
super.onSizeChanged(width, height, oldw, oldh);
Random random = new Random();
Interpolator interpolator = new LinearInterpolator();
money_count = Math.max(width, height) / 30;
coords = new int[money_count][];
drawables.clear();
for (int i = 0; i < money_count; i++) {
Animation animation = new TranslateAnimation(0, height / 10
- random.nextInt(height / 5), 0, height + 30);
animation.setDuration(10 * height + random.nextInt(5 * height));
animation.setRepeatCount(-1);
animation.initialize(10, 10, 10, 10);
animation.setInterpolator(interpolator);
coords[i] = new int[] { random.nextInt(width - 30), -80 };
drawables.add(new AnimateDrawable(money_sign, animation));
animation.setStartOffset(random.nextInt(20 * height));
animation.startNow();
}
}
private void displayViews(ViewGroup mainView) {
activity.addContentView(mainView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
if (mainView != null) {
Animation fadeInAnimation;
if (fromTop) {
fadeInAnimation = AnimationUtils.loadAnimation(activity, R.anim.fade_in_from_top);
} else {
fadeInAnimation = AnimationUtils.loadAnimation(activity, R.anim.fade_in);
}
mainView.startAnimation(fadeInAnimation);
}
if (onShowListener != null) onShowListener.onRateAppShowing(this, mainView);
}
public void startIconsEditing() {
mDockIconsEditAnimation = new AlphaAnimation(0.4f, 1.0f);
mDockIconsEditAnimation.setDuration(260);
mDockIconsEditAnimation.setRepeatMode(Animation.REVERSE);
mDockIconsEditAnimation.setRepeatCount(Animation.INFINITE);
iterateOverDockIcons((dockItem, dockIconColumn) -> {
dockItem.setOnClickListener(view -> showSelectAppForIconAtColumn(dockIconColumn));
dockItem.startAnimation(mDockIconsEditAnimation);
});
}
private void initAnims() {
animShow = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0);
animShow.setDuration(300);
animHide = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1);
animHide.setDuration(300);
}
@Override
public void show() {
super.show();
RotateAnimation anim = new RotateAnimation(0.0f, 359.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(3000);
rotatingImageView.setAnimation(anim);
rotatingImageView.startAnimation(anim);
}
public static Animation getBlinkAnimation(){
Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
animation.setDuration(300); // duration - half a second
animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
return animation;
}
@Override
public boolean isRunning() {
final ArrayList<Animation> animators = mAnimators;
final int N = animators.size();
for (int i = 0; i < N; i++) {
final Animation animator = animators.get(i);
if (animator.hasStarted() && !animator.hasEnded()) {
return true;
}
}
return false;
}
private void hide(boolean animate, Animation anim) {
this.setVisibility(View.GONE);
if (animate) {
this.startAnimation(anim);
}
isShown = false;
}
/**
* 晃动动画
* @param counts 0.5秒钟晃动多少下
* @return
*/
private Animation shakeAnimation(int counts){
Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
translateAnimation.setInterpolator(new CycleInterpolator(counts));
translateAnimation.setDuration(500);
return translateAnimation;
}
private void setUpAnimationLisForClose(Animation openAnimation) {
openAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
super.onAnimationEnd(animation);
onClose();
isAnimating = false;
}
});
}
private void animateFromOffscreenToLeftPane(View view) {
((MarginLayoutParams)view.getLayoutParams()).width = mLeftPaneWidth;
view.requestLayout();
Animation animation = new LeftMarginAnimation(view, -mLeftPaneWidth, 0);
view.startAnimation(animation);
}
@OnClick(R.id.trigger_delete)
public void trigger_delete(){
Context context = getActivity();
final Animation myAnim = AnimationUtils.loadAnimation(context, R.anim.bounce);
MyBounceInterpolator interpolator = new MyBounceInterpolator(0.2, 20);
myAnim.setInterpolator(interpolator);
deleteButton.startAnimation(myAnim);
BackendClient.of(this).deleteTriggers(new DeleteTriggerCallback(this) ,trigger.getId());
}
@Override
public void showLoadingView(boolean isShow, int loadingTxtRes) {
if (mLoadingView == null) {
return;
}
if (loadingTxtRes != 0) {
mLoadingTxt.setText(loadingTxtRes);
}
mLoadingView.setVisibility(isShow ? View.VISIBLE : View.GONE);
ImageView imgProgress = mLoadingView.findViewById(R.id.personal_show_loading_img);
Animation set = AnimationUtils.loadAnimation(this, R.anim.anim_center_rotate);
set.setInterpolator(new LinearInterpolator());
imgProgress.startAnimation(set);
}
/**
* 扫描动画
*
* @param delay
*/
public void startSearchAnim(long delay) {
scanView.setVisibility(VISIBLE);
ObjectAnimator oa = ObjectAnimator.ofFloat(scanView, "Rotation", 0, 360);
oa.setDuration(2000).setRepeatCount(Animation.INFINITE);
oa.setInterpolator(new LinearInterpolator());
oa.setStartDelay(delay);
oa.start();
}