3

ドラッグ機能のために windowManager に挿入されたカスタム ビューがあり、OnDraw()メソッドをオーバーライドします。アニメーションを変換してアニメーション化しようとしていますが、アニメーションは開始されません (animationListener もアニメーションをキャッチしません)。代わりに、ビューがアニメーションの開始点から終了点に「ジャンプ」します。

これはカスタム ビューです。

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);
4

1 に答える 1

0

この問題の「解決策」は、WindowManager がグローバル デバイス コンポーネント (たとえば、アプリ アイコンのドラッグで使用される) であり、アニメーションをまったくサポートしないことです。

于 2013-02-06T11:27:04.553 に答える