@Nullable @Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_one, container, false); view.findViewById(R.id.txtContainer).setOnClickListener(this); view.findViewById(R.id.txtContainer).setOnClickListener(this); AnimatorSet animator = (AnimatorSet) AnimatorInflater.loadAnimator( getActivity().getBaseContext(), R.animator.animator_main_in); animator.setTarget(view); // animator.setDuration(700); AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator( getActivity().getBaseContext(), R.animator.animator_main_out); animatorSet.setTarget(getActivity().findViewById(R.id.customImage)); animatorSet.start(); animator.start(); return view; }
// 需要执行动画时会系统会调用,用户无需手动调用 @Override public void runPendingAnimations() { if (!mAnimationAddViewHolders.isEmpty()) { AnimatorSet animator; View target; for (final RecyclerView.ViewHolder viewHolder : mAnimationAddViewHolders) { target = viewHolder.itemView; animator = new AnimatorSet(); animator.playTogether( ObjectAnimator.ofFloat(target, "translationX", -target.getMeasuredWidth(), 0.0f), ObjectAnimator.ofFloat(target, "alpha", target.getAlpha(), 1.0f)); animator.setTarget(target); animator.setDuration(100); animator.addListener( new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { mAnimationAddViewHolders.remove(viewHolder); if (!isRunning()) { dispatchAnimationsFinished(); } } @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }); animator.start(); } } else if (!mAnimationRemoveViewHolders.isEmpty()) { } }
/** * Method that create a new frame to be drawn in the specified location * * @param r The location relative to the parent layout * @return v The new view */ private View createFrame(Rect r, boolean animate) { int padding = (int) getResources().getDimension(R.dimen.disposition_frame_padding); final ImageView v = new ImageView(getContext()); v.setImageResource(R.drawable.ic_camera); v.setScaleType(ScaleType.CENTER); // Is locked? Then change the background color v.setBackgroundColor( getResources() .getColor( mResizeFrame == null ? R.color.disposition_locked_frame_bg_color : R.color.disposition_frame_bg_color)); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(r.width() - padding, r.height() - padding); v.setX(r.left + padding); v.setY(r.top + padding); v.setOnLongClickListener(this); addView(v, params); // Animate the view if (animate) { List<Animator> animators = new ArrayList<Animator>(); animators.add(ObjectAnimator.ofFloat(v, "scaleX", 0.0f, 1.0f)); animators.add(ObjectAnimator.ofFloat(v, "scaleY", 0.0f, 1.0f)); animators.add(ObjectAnimator.ofFloat(v, "alpha", 0.0f, 1.0f)); animators.add(ObjectAnimator.ofFloat(v, "alpha", 0.0f, 1.0f)); AnimatorSet animSet = new AnimatorSet(); animSet.playTogether(animators); animSet.setDuration(getResources().getInteger(R.integer.disposition_show_anim)); animSet.setInterpolator(new BounceInterpolator()); animSet.setTarget(v); animSet.start(); } return v; }
Animator getCancelAnimator() { ObjectAnimator backToTransparent1 = (ObjectAnimator) AnimatorInflater.loadAnimator(view.getContext(), R.animator.pin_background_end); ObjectAnimator goDark = (ObjectAnimator) AnimatorInflater.loadAnimator(view.getContext(), R.animator.pin_background_go_dark); ObjectAnimator backToTransparent2 = (ObjectAnimator) AnimatorInflater.loadAnimator(view.getContext(), R.animator.pin_background_end); ViewGroup parent = (ViewGroup) view.getParent(); ArgbEvaluator evaluator = new ArgbEvaluator(); for (ObjectAnimator animator : list(backToTransparent1, goDark, backToTransparent2)) { animator.setTarget(parent); animator.setEvaluator(evaluator); } AnimatorSet animatorSet = new AnimatorSet(); animatorSet.setTarget(parent); animatorSet.playSequentially(backToTransparent1, goDark, backToTransparent2); return animatorSet; }