2009-08-04 16:04:30 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package com.android.launcher2;
|
|
|
|
|
|
2010-11-23 14:40:33 -08:00
|
|
|
import android.animation.ValueAnimator;
|
|
|
|
|
import android.animation.ValueAnimator.AnimatorUpdateListener;
|
2010-09-27 11:15:43 -07:00
|
|
|
import android.content.res.Resources;
|
2009-08-04 16:04:30 -04:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Matrix;
|
|
|
|
|
import android.graphics.Paint;
|
2011-04-15 12:07:39 -07:00
|
|
|
import android.graphics.Rect;
|
2009-08-04 16:04:30 -04:00
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.WindowManager;
|
|
|
|
|
import android.view.WindowManagerImpl;
|
2010-11-23 14:40:33 -08:00
|
|
|
import android.view.animation.DecelerateInterpolator;
|
2009-08-04 16:04:30 -04:00
|
|
|
|
2010-12-08 11:05:37 -08:00
|
|
|
import com.android.launcher.R;
|
|
|
|
|
|
2010-11-23 14:40:33 -08:00
|
|
|
public class DragView extends View {
|
2009-08-04 16:04:30 -04:00
|
|
|
private Bitmap mBitmap;
|
|
|
|
|
private Paint mPaint;
|
|
|
|
|
private int mRegistrationX;
|
|
|
|
|
private int mRegistrationY;
|
|
|
|
|
|
2011-04-15 12:07:39 -07:00
|
|
|
private Rect mDragRegion = null;
|
2011-07-07 16:38:18 -07:00
|
|
|
private DragLayer mDragLayer = null;
|
2011-07-20 15:45:11 -07:00
|
|
|
private boolean mHasDrawn = false;
|
2010-08-19 13:52:27 -07:00
|
|
|
|
2010-11-23 14:40:33 -08:00
|
|
|
ValueAnimator mAnim;
|
|
|
|
|
private float mOffsetX = 0.0f;
|
|
|
|
|
private float mOffsetY = 0.0f;
|
2009-08-04 16:04:30 -04:00
|
|
|
|
2011-07-07 16:38:18 -07:00
|
|
|
private DragLayer.LayoutParams mLayoutParams;
|
2009-08-04 16:04:30 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the drag view.
|
|
|
|
|
* <p>
|
|
|
|
|
* The registration point is the point inside our view that the touch events should
|
|
|
|
|
* be centered upon.
|
|
|
|
|
*
|
2011-07-07 16:38:18 -07:00
|
|
|
* @param launcher The Launcher instance
|
2009-08-04 16:04:30 -04:00
|
|
|
* @param bitmap The view that we're dragging around. We scale it up when we draw it.
|
|
|
|
|
* @param registrationX The x coordinate of the registration point.
|
|
|
|
|
* @param registrationY The y coordinate of the registration point.
|
|
|
|
|
*/
|
2011-07-07 16:38:18 -07:00
|
|
|
public DragView(Launcher launcher, Bitmap bitmap, int registrationX, int registrationY,
|
2009-09-03 09:39:42 -07:00
|
|
|
int left, int top, int width, int height) {
|
2011-07-07 16:38:18 -07:00
|
|
|
super(launcher);
|
|
|
|
|
mDragLayer = launcher.getDragLayer();
|
2009-08-04 16:04:30 -04:00
|
|
|
|
2010-09-27 11:15:43 -07:00
|
|
|
final Resources res = getResources();
|
|
|
|
|
final int dragScale = res.getInteger(R.integer.config_dragViewExtraPixels);
|
|
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
Matrix scale = new Matrix();
|
2010-11-23 14:40:33 -08:00
|
|
|
final float scaleFactor = (width + dragScale) / width;
|
|
|
|
|
if (scaleFactor != 1.0f) {
|
|
|
|
|
scale.setScale(scaleFactor, scaleFactor);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-28 13:18:42 -07:00
|
|
|
final int offsetX = res.getDimensionPixelSize(R.dimen.dragViewOffsetX);
|
|
|
|
|
final int offsetY = res.getDimensionPixelSize(R.dimen.dragViewOffsetY);
|
2010-11-23 14:40:33 -08:00
|
|
|
|
|
|
|
|
// Animate the view into the correct position
|
|
|
|
|
mAnim = ValueAnimator.ofFloat(0.0f, 1.0f);
|
|
|
|
|
mAnim.setDuration(110);
|
|
|
|
|
mAnim.setInterpolator(new DecelerateInterpolator(2.5f));
|
|
|
|
|
mAnim.addUpdateListener(new AnimatorUpdateListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onAnimationUpdate(ValueAnimator animation) {
|
|
|
|
|
final float value = (Float) animation.getAnimatedValue();
|
|
|
|
|
|
|
|
|
|
final int deltaX = (int) ((value * offsetX) - mOffsetX);
|
|
|
|
|
final int deltaY = (int) ((value * offsetY) - mOffsetY);
|
|
|
|
|
|
|
|
|
|
mOffsetX += deltaX;
|
|
|
|
|
mOffsetY += deltaY;
|
|
|
|
|
|
|
|
|
|
if (getParent() == null) {
|
|
|
|
|
animation.cancel();
|
|
|
|
|
} else {
|
2011-07-07 16:38:18 -07:00
|
|
|
DragLayer.LayoutParams lp = mLayoutParams;
|
2010-11-23 14:40:33 -08:00
|
|
|
lp.x += deltaX;
|
|
|
|
|
lp.y += deltaY;
|
2011-07-07 16:38:18 -07:00
|
|
|
mDragLayer.requestLayout();
|
2010-11-23 14:40:33 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2009-08-04 16:04:30 -04:00
|
|
|
|
2009-09-03 09:39:42 -07:00
|
|
|
mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
|
2011-04-15 12:07:39 -07:00
|
|
|
setDragRegion(new Rect(0, 0, width, height));
|
2009-08-04 16:04:30 -04:00
|
|
|
|
|
|
|
|
// The point in our scaled bitmap that the touch events are located
|
2010-11-23 14:40:33 -08:00
|
|
|
mRegistrationX = registrationX;
|
|
|
|
|
mRegistrationY = registrationY;
|
2011-01-17 15:29:27 -08:00
|
|
|
|
|
|
|
|
// Force a measure, because Workspace uses getMeasuredHeight() before the layout pass
|
|
|
|
|
int ms = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
|
|
|
|
|
measure(ms, ms);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public float getOffsetY() {
|
|
|
|
|
return mOffsetY;
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
|
|
|
|
|
2010-08-19 13:52:27 -07:00
|
|
|
public int getDragRegionLeft() {
|
2011-04-15 12:07:39 -07:00
|
|
|
return mDragRegion.left;
|
2010-08-19 13:52:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDragRegionTop() {
|
2011-04-15 12:07:39 -07:00
|
|
|
return mDragRegion.top;
|
2010-08-19 13:52:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDragRegionWidth() {
|
2011-04-15 12:07:39 -07:00
|
|
|
return mDragRegion.width();
|
2010-08-19 13:52:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDragRegionHeight() {
|
2011-04-15 12:07:39 -07:00
|
|
|
return mDragRegion.height();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setDragRegion(Rect r) {
|
|
|
|
|
mDragRegion = r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Rect getDragRegion() {
|
|
|
|
|
return mDragRegion;
|
2010-08-19 13:52:27 -07:00
|
|
|
}
|
|
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
@Override
|
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
2009-11-04 13:48:32 -05:00
|
|
|
setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDraw(Canvas canvas) {
|
|
|
|
|
if (false) {
|
|
|
|
|
// for debugging
|
|
|
|
|
Paint p = new Paint();
|
|
|
|
|
p.setStyle(Paint.Style.FILL);
|
|
|
|
|
p.setColor(0xaaffffff);
|
|
|
|
|
canvas.drawRect(0, 0, getWidth(), getHeight(), p);
|
|
|
|
|
}
|
2010-11-23 14:40:33 -08:00
|
|
|
|
2011-07-20 15:45:11 -07:00
|
|
|
mHasDrawn = true;
|
2009-08-04 16:04:30 -04:00
|
|
|
canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPaint(Paint paint) {
|
|
|
|
|
mPaint = paint;
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-20 15:45:11 -07:00
|
|
|
public boolean hasDrawn() {
|
|
|
|
|
return mHasDrawn;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-02 18:03:00 -07:00
|
|
|
@Override
|
|
|
|
|
public void setAlpha(float alpha) {
|
|
|
|
|
super.setAlpha(alpha);
|
|
|
|
|
if (mPaint == null) {
|
|
|
|
|
mPaint = new Paint();
|
|
|
|
|
}
|
|
|
|
|
mPaint.setAlpha((int) (255 * alpha));
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
/**
|
|
|
|
|
* Create a window containing this view and show it.
|
|
|
|
|
*
|
|
|
|
|
* @param windowToken obtained from v.getWindowToken() from one of your views
|
2011-07-07 16:38:18 -07:00
|
|
|
* @param touchX the x coordinate the user touched in DragLayer coordinates
|
|
|
|
|
* @param touchY the y coordinate the user touched in DragLayer coordinates
|
2009-08-04 16:04:30 -04:00
|
|
|
*/
|
2011-07-07 16:38:18 -07:00
|
|
|
public void show(int touchX, int touchY) {
|
|
|
|
|
mDragLayer.addView(this);
|
|
|
|
|
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);
|
2009-08-04 16:04:30 -04:00
|
|
|
mLayoutParams = lp;
|
2010-11-23 14:40:33 -08:00
|
|
|
mAnim.start();
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
2011-06-30 12:09:54 -07:00
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
/**
|
|
|
|
|
* Move the window containing this view.
|
|
|
|
|
*
|
2011-07-07 16:38:18 -07:00
|
|
|
* @param touchX the x coordinate the user touched in DragLayer coordinates
|
|
|
|
|
* @param touchY the y coordinate the user touched in DragLayer coordinates
|
2009-08-04 16:04:30 -04:00
|
|
|
*/
|
|
|
|
|
void move(int touchX, int touchY) {
|
2011-07-07 16:38:18 -07:00
|
|
|
DragLayer.LayoutParams lp = mLayoutParams;
|
2010-11-23 14:40:33 -08:00
|
|
|
lp.x = touchX - mRegistrationX + (int) mOffsetX;
|
|
|
|
|
lp.y = touchY - mRegistrationY + (int) mOffsetY;
|
2011-07-07 16:38:18 -07:00
|
|
|
mDragLayer.requestLayout();
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void remove() {
|
2011-07-02 18:03:00 -07:00
|
|
|
post(new Runnable() {
|
|
|
|
|
public void run() {
|
2011-07-07 16:38:18 -07:00
|
|
|
mDragLayer.removeView(DragView.this);
|
2011-07-02 18:03:00 -07:00
|
|
|
}
|
|
|
|
|
});
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
2011-02-24 12:16:12 -08:00
|
|
|
|
|
|
|
|
int[] getPosition(int[] result) {
|
2011-07-07 16:38:18 -07:00
|
|
|
DragLayer.LayoutParams lp = mLayoutParams;
|
2011-02-24 12:16:12 -08:00
|
|
|
if (result == null) result = new int[2];
|
|
|
|
|
result[0] = lp.x;
|
|
|
|
|
result[1] = lp.y;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
|
|
|
|
|