Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Animated LinearLayout

View AnimatedLinearLayout.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
package com.example.android_transition_samples.app;
 
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
 
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;
 
public class AnimatedLinearLayout extends LinearLayout {
private static class ChildBounds {
public Rect start = new Rect();
public Rect end = new Rect();
}
 
private enum Capture {
START,
END
}
 
private WeakHashMap<View, ChildBounds> childrenBounds;
private boolean animate = false;
 
public AnimatedLinearLayout(Context context) {
this(context, null, 0);
}
 
public AnimatedLinearLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
 
public AnimatedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
childrenBounds = new WeakHashMap<View, ChildBounds>();
}
 
@Override
public void setOrientation(int orientation) {
prepareLayoutAnimation();
super.setOrientation(orientation);
}
 
@Override
public void addView(View child) {
prepareLayoutAnimation();
super.addView(child);
}
 
@Override
public void addView(View child, int index) {
prepareLayoutAnimation();
super.addView(child, index);
}
 
@Override
public void addView(View child, int width, int height) {
prepareLayoutAnimation();
super.addView(child, width, height);
}
 
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
prepareLayoutAnimation();
super.addView(child, params);
}
 
@Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
prepareLayoutAnimation();
super.addView(child, index, params);
}
 
@Override
public void removeView(View child) {
prepareLayoutAnimation();
childrenBounds.remove(child);
super.removeView(child);
}
 
@Override
public void removeViews(int start, int count) {
prepareLayoutAnimation();
 
for (int i = 0; i < count; i++) {
childrenBounds.remove(getChildAt(i));
}
 
super.removeViews(start, count);
}
 
@Override
public void removeViewAt(int index) {
prepareLayoutAnimation();
childrenBounds.remove(getChildAt(index));
super.removeViewAt(index);
}
 
public void setAnimationsEnabled(boolean enabled) {
animate = enabled;
}
 
private void waitForNextFrame() {
ViewTreeObserver treeObserver = getViewTreeObserver();
if (treeObserver != null && treeObserver.isAlive()) {
treeObserver.addOnPreDrawListener(new PreDrawListener());
}
}
 
private ChildBounds getChildBounds(View child) {
ChildBounds bounds = childrenBounds.get(child);
if (bounds == null) {
bounds = new ChildBounds();
childrenBounds.put(child, bounds);
}
 
return bounds;
}
 
private void setRectFromView(Rect rect, View v) {
rect.set(v.getLeft(),
v.getTop(),
v.getRight(),
v.getBottom());
}
 
private void captureChildrenBounds(Capture capture) {
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
 
ChildBounds bounds = getChildBounds(child);
if (capture == Capture.START) {
setRectFromView(bounds.start, child);
bounds.end.setEmpty();
} else {
setRectFromView(bounds.end, child);
}
}
}
 
private void prepareLayoutAnimation() {
if (!animate) {
return;
}
 
captureChildrenBounds(Capture.START);
waitForNextFrame();
}
 
private Animator prepareNewChildAnimator(View child) {
child.setAlpha(0f);
return ObjectAnimator.ofFloat(child, "alpha", 0f, 1f);
}
 
private Animator prepareBoundsAnimator(View child, ChildBounds bounds) {
int startLeft = bounds.start.left;
int startTop = bounds.start.top;
int startRight = bounds.start.right;
int startBottom = bounds.start.bottom;
 
int endLeft = bounds.end.left;
int endTop = bounds.end.top;
int endRight = bounds.end.right;
int endBottom = bounds.end.bottom;
 
int changeCount = 0;
 
if (startLeft != endLeft) {
child.setLeft(startLeft);
changeCount++;
}
if (startTop != endTop) {
child.setTop(startTop);
changeCount++;
}
if (startRight != endRight) {
child.setRight(startRight);
changeCount++;
}
if (startBottom != endBottom) {
child.setBottom(startBottom);
changeCount++;
}
 
if (changeCount == 0) {
return null;
}
 
PropertyValuesHolder pvh[] = new PropertyValuesHolder[changeCount];
int pvhIndex = -1;
 
if (startLeft != endLeft) {
pvh[++pvhIndex] = PropertyValuesHolder.ofInt("left", startLeft, endLeft);
}
if (startTop != endTop) {
pvh[++pvhIndex] = PropertyValuesHolder.ofInt("top", startTop, endTop);
}
if (startRight != endRight) {
pvh[++pvhIndex] = PropertyValuesHolder.ofInt("right", startRight, endRight);
}
if (startBottom != endBottom) {
pvh[++pvhIndex] = PropertyValuesHolder.ofInt("bottom", startBottom, endBottom);
}
 
return ObjectAnimator.ofPropertyValuesHolder(child, pvh);
}
 
private void animateBounds() {
List<Animator> newChildAnimators = new ArrayList<Animator>();
List<Animator> boundsAnimators = new ArrayList<Animator>();
 
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
ChildBounds bounds = getChildBounds(child);
 
// No start bounds, this is a new child in the container
if (bounds.start.isEmpty()) {
newChildAnimators.add(prepareNewChildAnimator(child));
continue;
}
 
Animator boundsAnimator = prepareBoundsAnimator(child, bounds);
if (boundsAnimator != null) {
boundsAnimators.add(boundsAnimator);
}
}
 
AnimatorSet boundsAnimSet = new AnimatorSet();
boundsAnimSet.playTogether(boundsAnimators);
 
AnimatorSet newChildAnimSet = new AnimatorSet();
newChildAnimSet.setStartDelay(2000);
newChildAnimSet.playTogether(newChildAnimators);
 
// It's not really safe to animate the bounds of a view without suppressing
// mid-air layout requests. Android's Transition framework supresses re-layout
// on the scene root while the transition is running with a private API
// i.e. ViewGroup.supressLayout(boolean).
AnimatorSet finalSet = new AnimatorSet();
finalSet.play(boundsAnimSet).before(newChildAnimSet);
finalSet.start();
}
 
private class PreDrawListener implements ViewTreeObserver.OnPreDrawListener {
@Override
public boolean onPreDraw() {
getViewTreeObserver().removeOnPreDrawListener(this);
captureChildrenBounds(Capture.END);
animateBounds();
 
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.