3
我有一個自定義視圖插入到窗口管理器的拖動功能,並覆蓋了OnDraw()
方法。我正在嘗試使用翻譯動畫對其進行動畫製作,但動畫無法啓動(也就是animationListener不捕捉動畫),而是從動畫的起點到終點的視圖「跳轉」。添加到WindowManager中的自定義視圖的動畫
這是自定義視圖:
public class DragView extends View {
@Override
protected void onAnimationEnd() {
// TODO Auto-generated method stub
super.onAnimationEnd();
}
@Override
protected void onAnimationStart() {
// TODO Auto-generated method stub
super.onAnimationStart();
}
// Number of pixels to add to the dragged item for scaling. Should be even
// for pixel alignment.
private static final int DRAG_SCALE = 0; // In Launcher, value is 40
public Bitmap mBitmap;
public Paint mPaint;
public int mRegistrationX;
public int mRegistrationY;
public Context context;
private float mAnimationScale = 1.0f;
private WindowManager.LayoutParams mLayoutParams;
private WindowManager mWindowManager;
public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, int left, int top, int width, int height,boolean animated) {
super(context);
if (animated)
return;
this.context = context;
// mWindowManager = WindowManagerImpl.getDefault();
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Matrix scale = new Matrix();
float scaleFactor = width;
scaleFactor = (scaleFactor + DRAG_SCALE)/scaleFactor;
scale.setScale(scaleFactor, scaleFactor);
mBitmap = Bitmap.createBitmap(bitmap, left, top, width, height, scale, true);
// The point in our scaled bitmap that the touch events are located
mRegistrationX = registrationX + (DRAG_SCALE/2);
mRegistrationY = registrationY + (DRAG_SCALE/2);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(mBitmap.getWidth(), mBitmap.getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
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 setPaint(Paint paint) {
mPaint = paint;
invalidate();
}
public void show(IBinder windowToken, int touchX, int touchY) {
final 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, pixelFormat);
lp.gravity = Gravity.LEFT | Gravity.TOP;
lp.token = windowToken;
lp.setTitle("DragView");
mLayoutParams = lp;
((FragmentActivity)context).runOnUiThread(new Runnable() {
//@Override
public void run() {
mWindowManager.addView(DragView.this, lp);
}
});
}
void move(int touchX, int touchY) {
// This is what was done in the Launcher code.
WindowManager.LayoutParams lp = mLayoutParams;
lp.x = touchX - mRegistrationX;
lp.y = touchY - mRegistrationY;
mWindowManager.updateViewLayout(this, lp);
}
void remove() {
mWindowManager.removeView(this);
}
這是我如何啓動動畫:
TranslateAnimation translate = new TranslateAnimation
(0, outOfScreenX, 0,outOfScreenY);
translate.setDuration(300);
translate.setFillAfter(true);
mDragView.clearAnimation();
mDragView.startAnimation(translate);
這不是一個解決方案...實際上它甚至不是一個完整的句子。 'WindowManager'支持動畫......它可能需要更多的工作(你是否嘗試過使用'WindowManager#updateWindowLayout()'?) –
我沒有解決這個問題,我只是沒有使用WindowManager。如果您可以提供有關此主題的有用信息,則您更受歡迎。 – Nativ