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-09-27 11:15:43 -07:00
|
|
|
import com.android.launcher.R;
|
|
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
import android.content.Context;
|
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;
|
|
|
|
|
import android.graphics.PixelFormat;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.view.Gravity;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
|
import android.view.WindowManager;
|
|
|
|
|
import android.view.WindowManagerImpl;
|
|
|
|
|
|
|
|
|
|
public class DragView extends View implements TweenCallback {
|
|
|
|
|
private Bitmap mBitmap;
|
|
|
|
|
private Paint mPaint;
|
|
|
|
|
private int mRegistrationX;
|
|
|
|
|
private int mRegistrationY;
|
|
|
|
|
|
2010-08-19 13:52:27 -07:00
|
|
|
private int mDragRegionLeft = 0;
|
|
|
|
|
private int mDragRegionTop = 0;
|
|
|
|
|
private int mDragRegionWidth;
|
|
|
|
|
private int mDragRegionHeight;
|
|
|
|
|
|
2009-08-04 16:04:30 -04:00
|
|
|
SymmetricalLinearTween mTween;
|
|
|
|
|
private float mScale;
|
|
|
|
|
private float mAnimationScale = 1.0f;
|
|
|
|
|
|
|
|
|
|
private WindowManager.LayoutParams mLayoutParams;
|
|
|
|
|
private WindowManager mWindowManager;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Construct the drag view.
|
|
|
|
|
* <p>
|
|
|
|
|
* The registration point is the point inside our view that the touch events should
|
|
|
|
|
* be centered upon.
|
|
|
|
|
*
|
|
|
|
|
* @param context A context
|
|
|
|
|
* @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.
|
|
|
|
|
*/
|
2009-09-03 09:39:42 -07:00
|
|
|
public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY,
|
|
|
|
|
int left, int top, int width, int height) {
|
2009-08-04 16:04:30 -04:00
|
|
|
super(context);
|
|
|
|
|
|
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
|
|
|
mWindowManager = WindowManagerImpl.getDefault();
|
|
|
|
|
|
|
|
|
|
mTween = new SymmetricalLinearTween(false, 110 /*ms duration*/, this);
|
|
|
|
|
|
|
|
|
|
Matrix scale = new Matrix();
|
|
|
|
|
float scaleFactor = width;
|
2010-09-27 11:15:43 -07:00
|
|
|
scaleFactor = mScale = (scaleFactor + dragScale) / scaleFactor;
|
2009-08-04 16:04:30 -04:00
|
|
|
scale.setScale(scaleFactor, scaleFactor);
|
|
|
|
|
|
2009-09-03 09:39:42 -07:00
|
|
|
mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
|
2010-09-17 15:00:07 -07:00
|
|
|
setDragRegion(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-09-27 11:15:43 -07:00
|
|
|
mRegistrationX = registrationX + res.getInteger(R.integer.config_dragViewOffsetX);
|
|
|
|
|
mRegistrationY = registrationY + res.getInteger(R.integer.config_dragViewOffsetY);
|
2009-08-04 16:04:30 -04:00
|
|
|
}
|
|
|
|
|
|
2010-08-19 13:52:27 -07:00
|
|
|
public void setDragRegion(int left, int top, int width, int height) {
|
|
|
|
|
mDragRegionLeft = left;
|
|
|
|
|
mDragRegionTop = top;
|
|
|
|
|
mDragRegionWidth = width;
|
|
|
|
|
mDragRegionHeight = height;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-17 15:00:07 -07:00
|
|
|
public int getScaledDragRegionXOffset() {
|
|
|
|
|
return -(int)((mScale - 1.0f) * mDragRegionWidth / 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getScaledDragRegionWidth() {
|
|
|
|
|
return (int)(mScale * mDragRegionWidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getScaledDragRegionYOffset() {
|
|
|
|
|
return -(int)((mScale - 1.0f) * mDragRegionHeight / 2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getScaledDragRegionHeight() {
|
|
|
|
|
return (int)(mScale * mDragRegionWidth);
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-19 13:52:27 -07:00
|
|
|
public int getDragRegionLeft() {
|
|
|
|
|
return mDragRegionLeft;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDragRegionTop() {
|
|
|
|
|
return mDragRegionTop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDragRegionWidth() {
|
|
|
|
|
return mDragRegionWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getDragRegionHeight() {
|
|
|
|
|
return mDragRegionHeight;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
float scale = mAnimationScale;
|
|
|
|
|
if (scale < 0.999f) { // allow for some float error
|
|
|
|
|
float width = mBitmap.getWidth();
|
|
|
|
|
float offset = (width-(width*scale))/2;
|
|
|
|
|
canvas.translate(offset, offset);
|
|
|
|
|
canvas.scale(scale, scale);
|
|
|
|
|
}
|
|
|
|
|
canvas.drawBitmap(mBitmap, 0.0f, 0.0f, mPaint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
mBitmap.recycle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onTweenValueChanged(float value, float oldValue) {
|
|
|
|
|
mAnimationScale = (1.0f+((mScale-1.0f)*value))/mScale;
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onTweenStarted() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onTweenFinished() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPaint(Paint paint) {
|
|
|
|
|
mPaint = paint;
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a window containing this view and show it.
|
|
|
|
|
*
|
|
|
|
|
* @param windowToken obtained from v.getWindowToken() from one of your views
|
|
|
|
|
* @param touchX the x coordinate the user touched in screen coordinates
|
|
|
|
|
* @param touchY the y coordinate the user touched in screen coordinates
|
|
|
|
|
*/
|
|
|
|
|
public void show(IBinder windowToken, int touchX, int touchY) {
|
|
|
|
|
WindowManager.LayoutParams lp;
|
|
|
|
|
int pixelFormat;
|
|
|
|
|
|
|
|
|
|
pixelFormat = PixelFormat.TRANSLUCENT;
|
|
|
|
|
|
|
|
|
|
lp = new WindowManager.LayoutParams(
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
|
touchX-mRegistrationX, touchY-mRegistrationY,
|
|
|
|
|
WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
|
|
|
|
|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
|
|
|
|
|
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
|
|
|
|
|
/*| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM*/,
|
|
|
|
|
pixelFormat);
|
|
|
|
|
// lp.token = mStatusBarView.getWindowToken();
|
|
|
|
|
lp.gravity = Gravity.LEFT | Gravity.TOP;
|
|
|
|
|
lp.token = windowToken;
|
|
|
|
|
lp.setTitle("DragView");
|
|
|
|
|
mLayoutParams = lp;
|
|
|
|
|
|
|
|
|
|
mWindowManager.addView(this, lp);
|
|
|
|
|
|
|
|
|
|
mAnimationScale = 1.0f/mScale;
|
|
|
|
|
mTween.start(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Move the window containing this view.
|
|
|
|
|
*
|
|
|
|
|
* @param touchX the x coordinate the user touched in screen coordinates
|
|
|
|
|
* @param touchY the y coordinate the user touched in screen coordinates
|
|
|
|
|
*/
|
|
|
|
|
void move(int touchX, int touchY) {
|
|
|
|
|
WindowManager.LayoutParams lp = mLayoutParams;
|
|
|
|
|
lp.x = touchX - mRegistrationX;
|
|
|
|
|
lp.y = touchY - mRegistrationY;
|
|
|
|
|
mWindowManager.updateViewLayout(this, lp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void remove() {
|
|
|
|
|
mWindowManager.removeView(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|