Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | */ |
| 16 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 18 | |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 19 | import android.animation.FloatArrayEvaluator; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 20 | import android.animation.ValueAnimator; |
| 21 | import android.animation.ValueAnimator.AnimatorUpdateListener; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 22 | import android.annotation.TargetApi; |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Canvas; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 26 | import android.graphics.Color; |
| 27 | import android.graphics.ColorMatrix; |
| 28 | import android.graphics.ColorMatrixColorFilter; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 29 | import android.graphics.Paint; |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 30 | import android.graphics.Point; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 31 | import android.graphics.Rect; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 32 | import android.os.Build; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 33 | import android.view.View; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 34 | import android.view.animation.DecelerateInterpolator; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 35 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 36 | import com.android.launcher3.util.Thunk; |
| 37 | |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 38 | import java.util.Arrays; |
| 39 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 40 | public class DragView extends View { |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 41 | public static int COLOR_CHANGE_DURATION = 120; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 42 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 43 | @Thunk static float sDragAlpha = 1f; |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 44 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 45 | private Bitmap mBitmap; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 46 | private Bitmap mCrossFadeBitmap; |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 47 | @Thunk Paint mPaint; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 48 | private int mRegistrationX; |
| 49 | private int mRegistrationY; |
| 50 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 51 | private Point mDragVisualizeOffset = null; |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 52 | private Rect mDragRegion = null; |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 53 | private DragLayer mDragLayer = null; |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 54 | private boolean mHasDrawn = false; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 55 | @Thunk float mCrossFadeProgress = 0f; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 56 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 57 | ValueAnimator mAnim; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 58 | @Thunk float mOffsetX = 0.0f; |
| 59 | @Thunk float mOffsetY = 0.0f; |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 60 | private float mInitialScale = 1f; |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 61 | // The intrinsic icon scale factor is the scale factor for a drag icon over the workspace |
| 62 | // size. This is ignored for non-icons. |
| 63 | private float mIntrinsicIconScale = 1f; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 64 | |
Sunny Goyal | 316490e | 2015-06-02 09:38:28 -0700 | [diff] [blame] | 65 | @Thunk float[] mCurrentFilter; |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 66 | private ValueAnimator mFilterAnimator; |
| 67 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 68 | /** |
| 69 | * Construct the drag view. |
| 70 | * <p> |
| 71 | * The registration point is the point inside our view that the touch events should |
| 72 | * be centered upon. |
| 73 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 74 | * @param launcher The Launcher instance |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 75 | * @param bitmap The view that we're dragging around. We scale it up when we draw it. |
| 76 | * @param registrationX The x coordinate of the registration point. |
| 77 | * @param registrationY The y coordinate of the registration point. |
| 78 | */ |
Sunny Goyal | 7066003 | 2015-05-14 00:07:08 -0700 | [diff] [blame] | 79 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 80 | public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY, |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 81 | int left, int top, int width, int height, final float initialScale) { |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 82 | super(launcher); |
| 83 | mDragLayer = launcher.getDragLayer(); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 84 | mInitialScale = initialScale; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 85 | |
Patrick Dubroy | de7658b | 2010-09-27 11:15:43 -0700 | [diff] [blame] | 86 | final Resources res = getResources(); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 87 | final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale); |
| 88 | final float scale = (width + scaleDps) / width; |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 89 | |
Adam Cohen | 307fe23 | 2012-08-16 17:55:58 -0700 | [diff] [blame] | 90 | // Set the initial scale to avoid any jumps |
| 91 | setScaleX(initialScale); |
| 92 | setScaleY(initialScale); |
| 93 | |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 94 | // Animate the view into the correct position |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 95 | mAnim = LauncherAnimUtils.ofFloat(this, 0f, 1f); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 96 | mAnim.setDuration(150); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 97 | mAnim.addUpdateListener(new AnimatorUpdateListener() { |
| 98 | @Override |
| 99 | public void onAnimationUpdate(ValueAnimator animation) { |
| 100 | final float value = (Float) animation.getAnimatedValue(); |
| 101 | |
Sunny Goyal | 560616d | 2015-02-26 11:26:19 -0800 | [diff] [blame] | 102 | final int deltaX = (int) (-mOffsetX); |
| 103 | final int deltaY = (int) (-mOffsetY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 104 | |
| 105 | mOffsetX += deltaX; |
| 106 | mOffsetY += deltaY; |
Winson Chung | 72d5984 | 2012-02-22 13:51:36 -0800 | [diff] [blame] | 107 | setScaleX(initialScale + (value * (scale - initialScale))); |
| 108 | setScaleY(initialScale + (value * (scale - initialScale))); |
Winson Chung | 867ca62 | 2012-02-21 15:48:35 -0800 | [diff] [blame] | 109 | if (sDragAlpha != 1f) { |
| 110 | setAlpha(sDragAlpha * value + (1f - value)); |
| 111 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 112 | |
| 113 | if (getParent() == null) { |
| 114 | animation.cancel(); |
| 115 | } else { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 116 | setTranslationX(getTranslationX() + deltaX); |
| 117 | setTranslationY(getTranslationY() + deltaY); |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 121 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 122 | mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height); |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 123 | setDragRegion(new Rect(0, 0, width, height)); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 124 | |
| 125 | // The point in our scaled bitmap that the touch events are located |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 126 | mRegistrationX = registrationX; |
| 127 | mRegistrationY = registrationY; |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 128 | |
| 129 | // Force a measure, because Workspace uses getMeasuredHeight() before the layout pass |
| 130 | int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); |
| 131 | measure(ms, ms); |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame] | 132 | mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
Sunny Goyal | 75deaf3 | 2015-05-07 16:13:12 -0700 | [diff] [blame] | 133 | |
| 134 | if (Utilities.isLmpOrAbove()) { |
| 135 | setElevation(getResources().getDimension(R.dimen.drag_elevation)); |
| 136 | } |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Winson Chung | eeb5bbc | 2013-11-13 15:47:05 -0800 | [diff] [blame] | 139 | /** Sets the scale of the view over the normal workspace icon size. */ |
| 140 | public void setIntrinsicIconScaleFactor(float scale) { |
| 141 | mIntrinsicIconScale = scale; |
| 142 | } |
| 143 | |
| 144 | public float getIntrinsicIconScaleFactor() { |
| 145 | return mIntrinsicIconScale; |
| 146 | } |
| 147 | |
Patrick Dubroy | 62bbb3c | 2011-01-17 15:29:27 -0800 | [diff] [blame] | 148 | public float getOffsetY() { |
| 149 | return mOffsetY; |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 150 | } |
| 151 | |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 152 | public int getDragRegionLeft() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 153 | return mDragRegion.left; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | public int getDragRegionTop() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 157 | return mDragRegion.top; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | public int getDragRegionWidth() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 161 | return mDragRegion.width(); |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | public int getDragRegionHeight() { |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 165 | return mDragRegion.height(); |
| 166 | } |
| 167 | |
Winson Chung | b8c69f3 | 2011-10-19 21:36:08 -0700 | [diff] [blame] | 168 | public void setDragVisualizeOffset(Point p) { |
| 169 | mDragVisualizeOffset = p; |
| 170 | } |
| 171 | |
| 172 | public Point getDragVisualizeOffset() { |
| 173 | return mDragVisualizeOffset; |
| 174 | } |
| 175 | |
Adam Cohen | e3e27a8 | 2011-04-15 12:07:39 -0700 | [diff] [blame] | 176 | public void setDragRegion(Rect r) { |
| 177 | mDragRegion = r; |
| 178 | } |
| 179 | |
| 180 | public Rect getDragRegion() { |
| 181 | return mDragRegion; |
Michael Jurka | a63c452 | 2010-08-19 13:52:27 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 184 | public float getInitialScale() { |
| 185 | return mInitialScale; |
| 186 | } |
| 187 | |
| 188 | public void updateInitialScaleToCurrentScale() { |
| 189 | mInitialScale = getScaleX(); |
| 190 | } |
| 191 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 192 | @Override |
| 193 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Joe Onorato | eebd924 | 2009-11-04 13:48:32 -0500 | [diff] [blame] | 194 | setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight()); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | protected void onDraw(Canvas canvas) { |
Michael Jurka | 3a9fced | 2012-04-13 14:44:29 -0700 | [diff] [blame] | 199 | @SuppressWarnings("all") // suppress dead code warning |
| 200 | final boolean debug = false; |
| 201 | if (debug) { |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 202 | Paint p = new Paint(); |
| 203 | p.setStyle(Paint.Style.FILL); |
Winson Chung | eecf02d | 2012-03-02 17:14:58 -0800 | [diff] [blame] | 204 | p.setColor(0x66ffffff); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 205 | canvas.drawRect(0, 0, getWidth(), getHeight(), p); |
| 206 | } |
Patrick Dubroy | a669d79 | 2010-11-23 14:40:33 -0800 | [diff] [blame] | 207 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 208 | mHasDrawn = true; |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 209 | boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null; |
| 210 | if (crossFade) { |
| 211 | int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255; |
| 212 | mPaint.setAlpha(alpha); |
| 213 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 214 | canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 215 | if (crossFade) { |
| 216 | mPaint.setAlpha((int) (255 * mCrossFadeProgress)); |
| 217 | canvas.save(); |
| 218 | float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth(); |
| 219 | float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight(); |
| 220 | canvas.scale(sX, sY); |
| 221 | canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint); |
| 222 | canvas.restore(); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | public void setCrossFadeBitmap(Bitmap crossFadeBitmap) { |
| 227 | mCrossFadeBitmap = crossFadeBitmap; |
| 228 | } |
| 229 | |
| 230 | public void crossFade(int duration) { |
Michael Jurka | f1ad608 | 2013-03-13 12:55:46 +0100 | [diff] [blame] | 231 | ValueAnimator va = LauncherAnimUtils.ofFloat(this, 0f, 1f); |
Adam Cohen | ed66b2b | 2012-01-23 17:28:51 -0800 | [diff] [blame] | 232 | va.setDuration(duration); |
| 233 | va.setInterpolator(new DecelerateInterpolator(1.5f)); |
| 234 | va.addUpdateListener(new AnimatorUpdateListener() { |
| 235 | @Override |
| 236 | public void onAnimationUpdate(ValueAnimator animation) { |
| 237 | mCrossFadeProgress = animation.getAnimatedFraction(); |
| 238 | } |
| 239 | }); |
| 240 | va.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 243 | public void setColor(int color) { |
Michael Jurka | 6cfafb9 | 2012-02-23 18:25:43 -0800 | [diff] [blame] | 244 | if (mPaint == null) { |
| 245 | mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); |
| 246 | } |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 247 | if (color != 0) { |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 248 | ColorMatrix m1 = new ColorMatrix(); |
| 249 | m1.setSaturation(0); |
| 250 | |
| 251 | ColorMatrix m2 = new ColorMatrix(); |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 252 | setColorScale(color, m2); |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 253 | m1.postConcat(m2); |
| 254 | |
| 255 | if (Utilities.isLmpOrAbove()) { |
| 256 | animateFilterTo(m1.getArray()); |
| 257 | } else { |
| 258 | mPaint.setColorFilter(new ColorMatrixColorFilter(m1)); |
| 259 | invalidate(); |
| 260 | } |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 261 | } else { |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 262 | if (!Utilities.isLmpOrAbove() || mCurrentFilter == null) { |
| 263 | mPaint.setColorFilter(null); |
| 264 | invalidate(); |
| 265 | } else { |
| 266 | animateFilterTo(new ColorMatrix().getArray()); |
| 267 | } |
Winson Chung | 61967cb | 2012-02-28 18:11:33 -0800 | [diff] [blame] | 268 | } |
Sunny Goyal | fe0d1f2 | 2015-04-28 22:01:31 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
| 272 | private void animateFilterTo(float[] targetFilter) { |
| 273 | float[] oldFilter = mCurrentFilter == null ? new ColorMatrix().getArray() : mCurrentFilter; |
| 274 | mCurrentFilter = Arrays.copyOf(oldFilter, oldFilter.length); |
| 275 | |
| 276 | if (mFilterAnimator != null) { |
| 277 | mFilterAnimator.cancel(); |
| 278 | } |
| 279 | mFilterAnimator = ValueAnimator.ofObject(new FloatArrayEvaluator(mCurrentFilter), |
| 280 | oldFilter, targetFilter); |
| 281 | mFilterAnimator.setDuration(COLOR_CHANGE_DURATION); |
| 282 | mFilterAnimator.addUpdateListener(new AnimatorUpdateListener() { |
| 283 | |
| 284 | @Override |
| 285 | public void onAnimationUpdate(ValueAnimator animation) { |
| 286 | mPaint.setColorFilter(new ColorMatrixColorFilter(mCurrentFilter)); |
| 287 | invalidate(); |
| 288 | } |
| 289 | }); |
| 290 | mFilterAnimator.start(); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Adam Cohen | fc53cd2 | 2011-07-20 15:45:11 -0700 | [diff] [blame] | 293 | public boolean hasDrawn() { |
| 294 | return mHasDrawn; |
| 295 | } |
| 296 | |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 297 | @Override |
| 298 | public void setAlpha(float alpha) { |
| 299 | super.setAlpha(alpha); |
Adam Cohen | 3e8f811 | 2011-07-02 18:03:00 -0700 | [diff] [blame] | 300 | mPaint.setAlpha((int) (255 * alpha)); |
| 301 | invalidate(); |
| 302 | } |
| 303 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 304 | /** |
| 305 | * Create a window containing this view and show it. |
| 306 | * |
| 307 | * @param windowToken obtained from v.getWindowToken() from one of your views |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 308 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 309 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 310 | */ |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 311 | public void show(int touchX, int touchY) { |
| 312 | mDragLayer.addView(this); |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 313 | |
Winson Chung | 043f2af | 2012-03-01 16:09:54 -0800 | [diff] [blame] | 314 | // Start the pick-up animation |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 315 | DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); |
| 316 | lp.width = mBitmap.getWidth(); |
| 317 | lp.height = mBitmap.getHeight(); |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 318 | lp.customPosition = true; |
| 319 | setLayoutParams(lp); |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 320 | setTranslationX(touchX - mRegistrationX); |
| 321 | setTranslationY(touchY - mRegistrationY); |
Michael Jurka | ca99383 | 2012-06-29 15:17:04 -0700 | [diff] [blame] | 322 | // Post the animation to skip other expensive work happening on the first frame |
| 323 | post(new Runnable() { |
| 324 | public void run() { |
| 325 | mAnim.start(); |
| 326 | } |
| 327 | }); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 328 | } |
Adam Cohen | 716b51e | 2011-06-30 12:09:54 -0700 | [diff] [blame] | 329 | |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 330 | public void cancelAnimation() { |
| 331 | if (mAnim != null && mAnim.isRunning()) { |
| 332 | mAnim.cancel(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | public void resetLayoutParams() { |
| 337 | mOffsetX = mOffsetY = 0; |
| 338 | requestLayout(); |
| 339 | } |
| 340 | |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 341 | /** |
| 342 | * Move the window containing this view. |
| 343 | * |
Adam Cohen | 8dfcba4 | 2011-07-07 16:38:18 -0700 | [diff] [blame] | 344 | * @param touchX the x coordinate the user touched in DragLayer coordinates |
| 345 | * @param touchY the y coordinate the user touched in DragLayer coordinates |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 346 | */ |
| 347 | void move(int touchX, int touchY) { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 348 | setTranslationX(touchX - mRegistrationX + (int) mOffsetX); |
| 349 | setTranslationY(touchY - mRegistrationY + (int) mOffsetY); |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void remove() { |
Winson Chung | 7bd1bbb | 2012-02-13 18:29:29 -0800 | [diff] [blame] | 353 | if (getParent() != null) { |
| 354 | mDragLayer.removeView(DragView.this); |
| 355 | } |
Patrick Dubroy | 6f13342 | 2011-02-24 12:16:12 -0800 | [diff] [blame] | 356 | } |
Sunny Goyal | 3a644ed | 2015-05-21 10:28:02 -0700 | [diff] [blame] | 357 | |
| 358 | public static void setColorScale(int color, ColorMatrix target) { |
| 359 | target.setScale(Color.red(color) / 255f, Color.green(color) / 255f, |
| 360 | Color.blue(color) / 255f, Color.alpha(color) / 255f); |
| 361 | } |
Joe Onorato | 00acb12 | 2009-08-04 16:04:30 -0400 | [diff] [blame] | 362 | } |