android - 添加到 WindowManager 的自定义 View 上的动画

标签 android custom-view window-managers translate-animation

我有一个自定义 View ,它被插入到 windowManager 中以实现拖动功能,并覆盖了 OnDraw() 方法。我正在尝试使用翻译动画为其设置动画但动画没有开始(而且 animationListener 没有捕捉到动画),而是 View 从动画的起点“跳转”到它的终点。

这是自定义 View :

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 是一个全局设备组件(例如应用程序图标的拖动使用它),它根本不支持动画。

关于android - 添加到 WindowManager 的自定义 View 上的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231725/

相关文章:

javascript - CKEDITOR.replace(elm[0]) 在 Android 上的 Chrome 上返回 null

Android - 获取当前时间而不依赖于设备的时钟

android数据绑定(bind)与自定义 View

为 Linux 创建一个窗口管理器

rust - xcb:在 EnterNotify 之后立即收到 LeaveNotify

java - 我可以使用 Spring 框架进行 Android 开发吗?

android - 更新后如何自动打开启动器应用程序?

android - ListView 在另一个 ListView 中

ios - 在 Swift 中使用 UITextView 属性自定义 UIView 的加载时间缓慢

c++ - Xlib 这个(去掉窗饰)是怎么工作的?