Animating the drag view scale up and down when dragging items.

Change-Id: Ic97d74a14964c6bdc23305b2d378b13a1f2e3664
This commit is contained in:
Winson Chung
2012-02-13 18:29:29 -08:00
parent d83f5f4db3
commit 7bd1bbb509
13 changed files with 111 additions and 132 deletions

View File

@@ -32,6 +32,8 @@ import android.view.animation.DecelerateInterpolator;
import com.android.launcher.R;
public class DragView extends View {
private static float sDragAlpha = 0.8f;
private Bitmap mBitmap;
private Bitmap mCrossFadeBitmap;
private Paint mPaint;
@@ -48,8 +50,6 @@ public class DragView extends View {
private float mOffsetX = 0.0f;
private float mOffsetY = 0.0f;
private DragLayer.LayoutParams mLayoutParams;
/**
* Construct the drag view.
* <p>
@@ -67,20 +67,14 @@ public class DragView extends View {
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.setDuration(150);
mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
mAnim.addUpdateListener(new AnimatorUpdateListener() {
@Override
@@ -92,19 +86,20 @@ public class DragView extends View {
mOffsetX += deltaX;
mOffsetY += deltaY;
setScaleX(1f + (value * (scale - 1f)));
setScaleY(1f + (value * (scale - 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
@@ -236,14 +231,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.
*
@@ -251,22 +256,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() {
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);
}
}
}