| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.transition; |
| 18 |
|
| 19 |
import android.animation.Animator; |
| 20 |
import android.animation.AnimatorSet; |
| 21 |
import android.animation.TypeEvaluator; |
| 22 |
import android.graphics.Bitmap; |
| 23 |
import android.graphics.Canvas; |
| 24 |
import android.graphics.Matrix; |
| 25 |
import android.graphics.Picture; |
| 26 |
import android.graphics.Rect; |
| 27 |
import android.graphics.RectF; |
| 28 |
import android.graphics.drawable.BitmapDrawable; |
| 29 |
import android.graphics.drawable.Drawable; |
| 30 |
import android.view.View; |
| 31 |
import android.view.ViewGroup; |
| 32 |
import android.widget.ImageView; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TransitionUtils { |
| 40 |
private static int MAX_IMAGE_SIZE = (1024 * 1024); |
| 41 |
|
| 42 |
static Animator mergeAnimators(Animator animator1, Animator animator2) { |
| 43 |
if (animator1 == null) { |
| 44 |
return animator2; |
| 45 |
} else if (animator2 == null) { |
| 46 |
return animator1; |
| 47 |
} else { |
| 48 |
AnimatorSet animatorSet = new AnimatorSet(); |
| 49 |
animatorSet.playTogether(animator1, animator2); |
| 50 |
return animatorSet; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
public static Transition mergeTransitions(Transition... transitions) { |
| 55 |
int count = 0; |
| 56 |
int nonNullIndex = -1; |
| 57 |
for (int i = 0; i < transitions.length; i++) { |
| 58 |
if (transitions[i] != null) { |
| 59 |
count++; |
| 60 |
nonNullIndex = i; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
if (count == 0) { |
| 65 |
return null; |
| 66 |
} |
| 67 |
|
| 68 |
if (count == 1) { |
| 69 |
return transitions[nonNullIndex]; |
| 70 |
} |
| 71 |
|
| 72 |
TransitionSet transitionSet = new TransitionSet(); |
| 73 |
for (int i = 0; i < transitions.length; i++) { |
| 74 |
if (transitions[i] != null) { |
| 75 |
transitionSet.addTransition(transitions[i]); |
| 76 |
} |
| 77 |
} |
| 78 |
return transitionSet; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static View copyViewImage(ViewGroup sceneRoot, View view, View parent) { |
| 90 |
Matrix matrix = new Matrix(); |
| 91 |
matrix.setTranslate(-parent.getScrollX(), -parent.getScrollY()); |
| 92 |
view.transformMatrixToGlobal(matrix); |
| 93 |
sceneRoot.transformMatrixToLocal(matrix); |
| 94 |
RectF bounds = new RectF(0, 0, view.getWidth(), view.getHeight()); |
| 95 |
matrix.mapRect(bounds); |
| 96 |
int left = Math.round(bounds.left); |
| 97 |
int top = Math.round(bounds.top); |
| 98 |
int right = Math.round(bounds.right); |
| 99 |
int bottom = Math.round(bounds.bottom); |
| 100 |
|
| 101 |
ImageView copy = new ImageView(view.getContext()); |
| 102 |
copy.setScaleType(ImageView.ScaleType.CENTER_CROP); |
| 103 |
Bitmap bitmap = createViewBitmap(view, matrix, bounds, sceneRoot); |
| 104 |
if (bitmap != null) { |
| 105 |
copy.setImageBitmap(bitmap); |
| 106 |
} |
| 107 |
int widthSpec = View.MeasureSpec.makeMeasureSpec(right - left, View.MeasureSpec.EXACTLY); |
| 108 |
int heightSpec = View.MeasureSpec.makeMeasureSpec(bottom - top, View.MeasureSpec.EXACTLY); |
| 109 |
copy.measure(widthSpec, heightSpec); |
| 110 |
copy.layout(left, top, right, bottom); |
| 111 |
return copy; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static Bitmap createDrawableBitmap(Drawable drawable, View hostView) { |
| 118 |
int width = drawable.getIntrinsicWidth(); |
| 119 |
int height = drawable.getIntrinsicHeight(); |
| 120 |
if (width <= 0 || height <= 0) { |
| 121 |
return null; |
| 122 |
} |
| 123 |
float scale = Math.min(1f, ((float)MAX_IMAGE_SIZE) / (width * height)); |
| 124 |
if (drawable instanceof BitmapDrawable && scale == 1f) { |
| 125 |
|
| 126 |
return ((BitmapDrawable) drawable).getBitmap(); |
| 127 |
} |
| 128 |
int bitmapWidth = (int) (width * scale); |
| 129 |
int bitmapHeight = (int) (height * scale); |
| 130 |
final Picture picture = new Picture(); |
| 131 |
final Canvas canvas = picture.beginRecording(width, height); |
| 132 |
|
| 133 |
Rect existingBounds = drawable.getBounds(); |
| 134 |
int left = existingBounds.left; |
| 135 |
int top = existingBounds.top; |
| 136 |
int right = existingBounds.right; |
| 137 |
int bottom = existingBounds.bottom; |
| 138 |
drawable.setBounds(0, 0, bitmapWidth, bitmapHeight); |
| 139 |
drawable.draw(canvas); |
| 140 |
drawable.setBounds(left, top, right, bottom); |
| 141 |
picture.endRecording(); |
| 142 |
return Bitmap.createBitmap(picture); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public static Bitmap createViewBitmap(View view, Matrix matrix, RectF bounds, |
| 161 |
ViewGroup sceneRoot) { |
| 162 |
final boolean addToOverlay = !view.isAttachedToWindow(); |
| 163 |
ViewGroup parent = null; |
| 164 |
int indexInParent = 0; |
| 165 |
if (addToOverlay) { |
| 166 |
if (sceneRoot == null || !sceneRoot.isAttachedToWindow()) { |
| 167 |
return null; |
| 168 |
} |
| 169 |
parent = (ViewGroup) view.getParent(); |
| 170 |
indexInParent = parent.indexOfChild(view); |
| 171 |
sceneRoot.getOverlay().add(view); |
| 172 |
} |
| 173 |
Bitmap bitmap = null; |
| 174 |
int bitmapWidth = Math.round(bounds.width()); |
| 175 |
int bitmapHeight = Math.round(bounds.height()); |
| 176 |
if (bitmapWidth > 0 && bitmapHeight > 0) { |
| 177 |
float scale = Math.min(1f, ((float) MAX_IMAGE_SIZE) / (bitmapWidth * bitmapHeight)); |
| 178 |
bitmapWidth *= scale; |
| 179 |
bitmapHeight *= scale; |
| 180 |
matrix.postTranslate(-bounds.left, -bounds.top); |
| 181 |
matrix.postScale(scale, scale); |
| 182 |
|
| 183 |
final Picture picture = new Picture(); |
| 184 |
final Canvas canvas = picture.beginRecording(bitmapWidth, bitmapHeight); |
| 185 |
canvas.concat(matrix); |
| 186 |
view.draw(canvas); |
| 187 |
picture.endRecording(); |
| 188 |
bitmap = Bitmap.createBitmap(picture); |
| 189 |
} |
| 190 |
if (addToOverlay) { |
| 191 |
sceneRoot.getOverlay().remove(view); |
| 192 |
parent.addView(view, indexInParent); |
| 193 |
} |
| 194 |
return bitmap; |
| 195 |
} |
| 196 |
|
| 197 |
public static class MatrixEvaluator implements TypeEvaluator<Matrix> { |
| 198 |
|
| 199 |
float[] mTempStartValues = new float[9]; |
| 200 |
|
| 201 |
float[] mTempEndValues = new float[9]; |
| 202 |
|
| 203 |
Matrix mTempMatrix = new Matrix(); |
| 204 |
|
| 205 |
@Override |
| 206 |
public Matrix evaluate(float fraction, Matrix startValue, Matrix endValue) { |
| 207 |
startValue.getValues(mTempStartValues); |
| 208 |
endValue.getValues(mTempEndValues); |
| 209 |
for (int i = 0; i < 9; i++) { |
| 210 |
float diff = mTempEndValues[i] - mTempStartValues[i]; |
| 211 |
mTempEndValues[i] = mTempStartValues[i] + (fraction * diff); |
| 212 |
} |
| 213 |
mTempMatrix.setValues(mTempEndValues); |
| 214 |
return mTempMatrix; |
| 215 |
} |
| 216 |
} |
| 217 |
} |