从浮动窗口缩放并获取尺寸

时间:2018-10-15 20:40:33

标签: android android-layout

我有带VideoView的浮动窗口。 我正在尝试通过将其从右侧边缘之一拖动来缩放它,并通过在任何其他部分触摸它并拖动来移动它。

“移动”部分效果很好,但是当我缩放它时,videoView的大小会发生变化,但“宽度”和“高度”保持不变。

我知道“ setScale”仅在屏幕上呈现,但是我如何使它成为具有正确大小的完整视图以继续使用浮动窗口(缩放和拖动)。

这是我的代码:

private WindowManager windowManager;
private VideoView video;

@Override
public void onCreate() {
    super.onCreate();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    video = new VideoView(this);
    Uri uri = Uri.parse("http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8");
    video.setVideoURI(uri);
    video.start();

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 0;
    windowManager.addView(video, params);


    try {
        video.setOnTouchListener(new View.OnTouchListener() {
            private WindowManager.LayoutParams paramsF = params;
            private int W;
            private int H;
            private int initialX;
            private int initialY;
            private float viewX;
            private float viewY;
            private float initialTouchX;
            private float initialTouchY;
            private float scaleFactor;

            boolean rightDown = false;
            boolean rightUp = false;



            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        W = video.getWidth();
                        H = video.getHeight();

                        initialX = paramsF.x;
                        initialY = paramsF.y;

                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();

                        viewX = event.getX();
                        viewY = event.getY();

                        if (viewX > (W / 4) * 3 && viewY < H / 3) {
                            rightUp = true;
                            video.setPivotY(H);
                            video.setPivotX(0);

                        } else if (viewY > H / 3 * 2 && viewX > (W / 4) * 3) {
                            rightDown = true;
                            video.setPivotY(0);
                            video.setPivotX(0);
                        }


                        break;

                    case MotionEvent.ACTION_UP:
                        rightDown = false;
                        rightUp = false;

                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (rightDown || rightUp) {
                            scaleFactor = event.getRawX() / initialTouchX;
                            video.setScaleX(scaleFactor);
                            video.setScaleY(scaleFactor);

                        }else {
                            paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                            paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                            windowManager.updateViewLayout(video, paramsF);
                        }
                        break;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }

}

0 个答案:

没有答案