diff options
Diffstat (limited to 'src/com/android/launcher2/DragView.java')
-rw-r--r-- | src/com/android/launcher2/DragView.java | 112 |
1 files changed, 71 insertions, 41 deletions
diff --git a/src/com/android/launcher2/DragView.java b/src/com/android/launcher2/DragView.java index dd94175b6..017d154f7 100644 --- a/src/com/android/launcher2/DragView.java +++ b/src/com/android/launcher2/DragView.java @@ -32,7 +32,10 @@ import android.view.animation.DecelerateInterpolator; import com.android.launcher.R; public class DragView extends View { + private static float sDragAlpha = 1f; + private Bitmap mBitmap; + private Bitmap mCrossFadeBitmap; private Paint mPaint; private int mRegistrationX; private int mRegistrationY; @@ -41,13 +44,12 @@ public class DragView extends View { private Rect mDragRegion = null; private DragLayer mDragLayer = null; private boolean mHasDrawn = false; + private float mCrossFadeProgress = 0f; ValueAnimator mAnim; private float mOffsetX = 0.0f; private float mOffsetY = 0.0f; - private DragLayer.LayoutParams mLayoutParams; - /** * Construct the drag view. * <p> @@ -60,26 +62,19 @@ public class DragView extends View { * @param registrationY The y coordinate of the registration point. */ public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY, - int left, int top, int width, int height) { + int left, int top, int width, int height, final float initialScale) { super(launcher); mDragLayer = launcher.getDragLayer(); final Resources res = getResources(); - final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels); - - Matrix scale = new Matrix(); - final float scaleFactor = (width + dragScale) / width; - if (scaleFactor != 1.0f) { - scale.setScale(scaleFactor, scaleFactor); - } - - final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); - final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); + final float offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX); + final float offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY); + final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale); + final float scale = (width + scaleDps) / width; // Animate the view into the correct position mAnim = ValueAnimator.ofFloat(0.0f, 1.0f); - mAnim.setDuration(110); - mAnim.setInterpolator(new DecelerateInterpolator(2.5f)); + mAnim.setDuration(150); mAnim.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { @@ -90,19 +85,22 @@ public class DragView extends View { mOffsetX += deltaX; mOffsetY += deltaY; + setScaleX(initialScale + (value * (scale - initialScale))); + setScaleY(initialScale + (value * (scale - initialScale))); + if (sDragAlpha != 1f) { + setAlpha(sDragAlpha * value + (1f - value)); + } if (getParent() == null) { animation.cancel(); } else { - DragLayer.LayoutParams lp = mLayoutParams; - lp.x += deltaX; - lp.y += deltaY; - mDragLayer.requestLayout(); + setTranslationX(getTranslationX() + deltaX); + setTranslationY(getTranslationY() + deltaY); } } }); - mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true); + mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height); setDragRegion(new Rect(0, 0, width, height)); // The point in our scaled bitmap that the touch events are located @@ -164,9 +162,43 @@ public class DragView extends View { p.setColor(0xaaffffff); canvas.drawRect(0, 0, getWidth(), getHeight(), p); } + if (mPaint == null) { + mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); + } mHasDrawn = true; + boolean crossFade = mCrossFadeProgress > 0 && mCrossFadeBitmap != null; + if (crossFade) { + int alpha = crossFade ? (int) (255 * (1 - mCrossFadeProgress)) : 255; + mPaint.setAlpha(alpha); + } canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint); + if (crossFade) { + mPaint.setAlpha((int) (255 * mCrossFadeProgress)); + canvas.save(); + float sX = (mBitmap.getWidth() * 1.0f) / mCrossFadeBitmap.getWidth(); + float sY = (mBitmap.getHeight() * 1.0f) / mCrossFadeBitmap.getHeight(); + canvas.scale(sX, sY); + canvas.drawBitmap(mCrossFadeBitmap, 0.0f, 0.0f, mPaint); + canvas.restore(); + } + } + + public void setCrossFadeBitmap(Bitmap crossFadeBitmap) { + mCrossFadeBitmap = crossFadeBitmap; + } + + public void crossFade(int duration) { + ValueAnimator va = ValueAnimator.ofFloat(0f, 1f); + va.setDuration(duration); + va.setInterpolator(new DecelerateInterpolator(1.5f)); + va.addUpdateListener(new AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + mCrossFadeProgress = animation.getAnimatedFraction(); + } + }); + va.start(); } public void setPaint(Paint paint) { @@ -182,7 +214,7 @@ public class DragView extends View { public void setAlpha(float alpha) { super.setAlpha(alpha); if (mPaint == null) { - mPaint = new Paint(); + mPaint = new Paint(Paint.FILTER_BITMAP_FLAG); } mPaint.setAlpha((int) (255 * alpha)); invalidate(); @@ -200,14 +232,24 @@ public class DragView extends View { DragLayer.LayoutParams lp = new DragLayer.LayoutParams(0, 0); lp.width = mBitmap.getWidth(); lp.height = mBitmap.getHeight(); - lp.x = touchX - mRegistrationX; - lp.y = touchY - mRegistrationY; lp.customPosition = true; setLayoutParams(lp); - mLayoutParams = lp; + setTranslationX(touchX - mRegistrationX); + setTranslationY(touchY - mRegistrationY); mAnim.start(); } + public void cancelAnimation() { + if (mAnim != null && mAnim.isRunning()) { + mAnim.cancel(); + } + } + + public void resetLayoutParams() { + mOffsetX = mOffsetY = 0; + requestLayout(); + } + /** * Move the window containing this view. * @@ -215,26 +257,14 @@ public class DragView extends View { * @param touchY the y coordinate the user touched in DragLayer coordinates */ void move(int touchX, int touchY) { - DragLayer.LayoutParams lp = mLayoutParams; - lp.x = touchX - mRegistrationX + (int) mOffsetX; - lp.y = touchY - mRegistrationY + (int) mOffsetY; - mDragLayer.requestLayout(); + setTranslationX(touchX - mRegistrationX + (int) mOffsetX); + setTranslationY(touchY - mRegistrationY + (int) mOffsetY); } void remove() { - post(new Runnable() { - public void run() { - mDragLayer.removeView(DragView.this); - } - }); - } - - int[] getPosition(int[] result) { - DragLayer.LayoutParams lp = mLayoutParams; - if (result == null) result = new int[2]; - result[0] = lp.x; - result[1] = lp.y; - return result; + if (getParent() != null) { + mDragLayer.removeView(DragView.this); + } } } |