采用WindowManager播放浮窗视频,更新窗体位置时视频还留在原处?

vantage4869 2016-05-11 08:57:59
package com.example.hello5;

import java.io.File;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;
import android.widget.VideoView;

public class VideoService extends Service {
private LayoutParams mLayout;
private WindowManager mWindowManager;
private VideoView vv2;
private long startTime;
float x, y;
int top;
private View view;
private FrameLayout layout;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
createWindowManager();
view = View.inflate(this, R.layout.vedio_window, null);
vv2 = (VideoView) view.findViewById(R.id.vv);
layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
layout.addView(view);
// MediaController mc2 = new MediaController(this);
File video = new File("/mnt/sdcard/test.mp4");
mWindowManager.addView(layout, mLayout);

if(video.exists()) {
vv2.setVideoPath(video.getAbsolutePath());
// vv2.setMediaController(mc2);
// mc2.setMediaPlayer(vv2);
// vv2.requestFocus();
vv2.start();
}
layout.setOnTouchListener(new OnTouchListener() {
float mTouchStartX;
float mTouchStartY;

@Override
public boolean onTouch(View v, MotionEvent event) {
// System.out.println("mDesktopLayout.width:" + view.getWidth() + view.getHeight());
x = event.getRawX();
y = event.getRawY() - top;
Log.i("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mTouchStartX = event.getX();
mTouchStartY = event.getY();
Log.i("startP", "startX" + mTouchStartX + "====startY"
+ mTouchStartY);
long end = System.currentTimeMillis() - startTime;
if (end < 300) {
// closeDesk();
System.out.println("双击!");
}
startTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
mLayout.x = (int) (x - mTouchStartX);
mLayout.y = (int) (y - mTouchStartY);
mWindowManager.updateViewLayout(v, mLayout);
// view.layout(mLayout.x, mLayout.y, mLayout.x+view.getWidth(), mLayout.y + view.getHeight());
break;
case MotionEvent.ACTION_UP:

mLayout.x = (int) (x - mTouchStartX);
mLayout.y = (int) (y - mTouchStartY);
mWindowManager.updateViewLayout(v, mLayout);
// view.layout(mLayout.x, mLayout.y, mLayout.x+view.getWidth(), mLayout.y + view.getHeight());
mTouchStartX = mTouchStartY = 0;
break;
}
return true;
}
});
}

private void createWindowManager() {
mWindowManager = (WindowManager)this.getSystemService(Context.WINDOW_SERVICE);

mLayout = new WindowManager.LayoutParams();

mLayout.type = WindowManager.LayoutParams.TYPE_PHONE;

mLayout.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

mLayout.format = PixelFormat.RGBA_8888;

mLayout.gravity = Gravity.TOP | Gravity.LEFT;

mLayout.width = 400;
mLayout.height = 300;
}
}

...全文
158 1 打赏 收藏 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
vantage4869 2016-05-11
  • 打赏
  • 举报
回复
为什么会这样?

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧