TransitionAnimationView.java revision 11a2cb156aedd48424651508689e56374fb10a25
1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package com.android.contacts.widget; 17 18import com.android.contacts.R; 19 20import android.animation.Animator; 21import android.animation.Animator.AnimatorListener; 22import android.animation.AnimatorInflater; 23import android.content.Context; 24import android.content.res.TypedArray; 25import android.graphics.Bitmap; 26import android.graphics.Canvas; 27import android.graphics.Color; 28import android.graphics.Paint; 29import android.graphics.Rect; 30import android.graphics.drawable.BitmapDrawable; 31import android.util.AttributeSet; 32import android.view.View; 33import android.view.ViewParent; 34import android.widget.FrameLayout; 35 36/** 37 * A container for a view that needs to have exit/enter animations when rebinding data. 38 * This layout should have a single child. Just before rebinding data that child 39 * should make this call: 40 * <pre> 41 * TransitionAnimationView.startAnimation(this); 42 * </pre> 43 */ 44public class TransitionAnimationView extends FrameLayout implements AnimatorListener { 45 46 private View mPreviousStateView; 47 private Bitmap mPreviousStateBitmap; 48 private int mEnterAnimationId; 49 private int mExitAnimationId; 50 private int mAnimationDuration; 51 private Rect mClipMargins = new Rect(); 52 private Rect mClipRect = new Rect(); 53 private Animator mEnterAnimation; 54 private Animator mExitAnimation; 55 56 public TransitionAnimationView(Context context) { 57 this(context, null, 0); 58 } 59 60 public TransitionAnimationView(Context context, AttributeSet attrs) { 61 this(context, attrs, 0); 62 } 63 64 public TransitionAnimationView(Context context, AttributeSet attrs, int defStyle) { 65 super(context, attrs, defStyle); 66 67 TypedArray a = getContext().obtainStyledAttributes( 68 attrs, R.styleable.TransitionAnimationView); 69 70 mEnterAnimationId = a.getResourceId(R.styleable.TransitionAnimationView_enterAnimation, 71 android.R.anim.animator_fade_in); 72 mExitAnimationId = a.getResourceId(R.styleable.TransitionAnimationView_exitAnimation, 73 android.R.anim.animator_fade_out); 74 mClipMargins.left = a.getDimensionPixelOffset( 75 R.styleable.TransitionAnimationView_clipMarginLeft, 0); 76 mClipMargins.top = a.getDimensionPixelOffset( 77 R.styleable.TransitionAnimationView_clipMarginTop, 0); 78 mClipMargins.right = a.getDimensionPixelOffset( 79 R.styleable.TransitionAnimationView_clipMarginRight, 0); 80 mClipMargins.bottom = a.getDimensionPixelOffset( 81 R.styleable.TransitionAnimationView_clipMarginBottom, 0); 82 mAnimationDuration = a.getInt( 83 R.styleable.TransitionAnimationView_animationDuration, 100); 84 85 a.recycle(); 86 87 mPreviousStateView = new View(context); 88 mPreviousStateView.setVisibility(View.INVISIBLE); 89 addView(mPreviousStateView); 90 91 mEnterAnimation = AnimatorInflater.loadAnimator(getContext(), mEnterAnimationId); 92 if (mEnterAnimation == null) { 93 throw new IllegalArgumentException("Invalid enter animation: " + mEnterAnimationId); 94 } 95 mEnterAnimation.addListener(this); 96 mEnterAnimation.setDuration(mAnimationDuration); 97 98 mExitAnimation = AnimatorInflater.loadAnimator(getContext(), mExitAnimationId); 99 if (mExitAnimation == null) { 100 throw new IllegalArgumentException("Invalid exit animation: " + mExitAnimationId); 101 } 102 103 } 104 105 @Override 106 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 107 super.onLayout(changed, left, top, right, bottom); 108 if (changed) { 109 int width = right - left; 110 int height = bottom - top; 111 if (width > 0 && height > 0) { 112 mPreviousStateBitmap = Bitmap.createBitmap( 113 width, height, Bitmap.Config.ARGB_8888); 114 mPreviousStateView.setBackgroundDrawable( 115 new BitmapDrawable(getContext().getResources(), mPreviousStateBitmap)); 116 mClipRect.set(mClipMargins.left, mClipMargins.top, 117 width - mClipMargins.right, height - mClipMargins.bottom); 118 } else { 119 mPreviousStateBitmap = null; 120 mPreviousStateView = null; 121 } 122 } 123 } 124 125 public static void startAnimation(View view, boolean closing) { 126 TransitionAnimationView container = null; 127 ViewParent parent = view.getParent(); 128 while (parent instanceof View) { 129 if (parent instanceof TransitionAnimationView) { 130 container = (TransitionAnimationView) parent; 131 break; 132 } 133 parent = parent.getParent(); 134 } 135 136 if (container != null) { 137 container.start(view, closing); 138 } 139 } 140 141 private void start(View view, boolean closing) { 142 if (mEnterAnimation.isRunning()) { 143 mEnterAnimation.end(); 144 } 145 if (mExitAnimation.isRunning()) { 146 mExitAnimation.end(); 147 } 148 if (view.getVisibility() != View.VISIBLE) { 149 if (!closing) { 150 mEnterAnimation.setTarget(view); 151 mEnterAnimation.start(); 152 } 153 } else if (closing) { 154 mExitAnimation.setTarget(view); 155 mExitAnimation.start(); 156 } else { 157 if (mPreviousStateBitmap == null) { 158 return; 159 } 160 161 Canvas canvas = new Canvas(mPreviousStateBitmap); 162 Paint paint = new Paint(); 163 paint.setColor(Color.TRANSPARENT); 164 canvas.drawRect(0, 0, mPreviousStateBitmap.getWidth(), mPreviousStateBitmap.getHeight(), 165 paint); 166 canvas.clipRect(mClipRect); 167 view.draw(canvas); 168 mPreviousStateView.setVisibility(View.VISIBLE); 169 170 mEnterAnimation.setTarget(view); 171 mEnterAnimation.start(); 172 } 173 } 174 175 @Override 176 public void onAnimationEnd(Animator animation) { 177 mPreviousStateView.setVisibility(View.INVISIBLE); 178 } 179 180 @Override 181 public void onAnimationCancel(Animator animation) { 182 } 183 184 @Override 185 public void onAnimationStart(Animator animation) { 186 } 187 188 @Override 189 public void onAnimationRepeat(Animator animation) { 190 } 191} 192