android activity背景视频,android 浮动窗口实例(视频播放)

public classMainActivity extendsActivity {

privateWebView web= null;

privateWindowManager mWindowManager, wm;

privateWindowManager.LayoutParams mLayoutParams;

privateLayoutInflater mLayoutInflater;

privateView mFloatView;

private intmCurrentX;

private intmCurrentY;

private static intmFloatViewWidth= 200;

private static intmFloatViewHeight= 200;

privateVideoView videoView= null;

private booleanisFull= false;

privateDisplayMetrics screen;

privateButton btn_floatView;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

web= (WebView)findViewById(R.id.web);

web.loadUrl("http://www.sohu.com");

Log.d("wangzq", "OnCreate called");

//覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开web.setWebViewClient(newWebViewClient()

{

@Overridepublic booleanshouldOverrideUrlLoading(WebView view, String url) {

//TODO Auto-generated method stub//返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器view.loadUrl(url);

return true;

}

});

mWindowManager= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

mLayoutInflater= LayoutInflater.from(this);

createView();

}

private voidcreateFloatView()

{

//final Button btn_floatView ;btn_floatView= newButton(getApplicationContext());

btn_floatView.setText("悬浮窗悬浮窗悬浮窗悬浮窗悬浮窗悬浮窗悬浮窗");

mFloatView= mLayoutInflater.inflate(R.layout.play_view, null);

wm= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

finalWindowManager.LayoutParams params = newWindowManager.LayoutParams();

// 设置window typeparams.type= WindowManager.LayoutParams.TYPE_PHONE;

/** 如果设置为params.type = WindowManager.LayoutParams.TYPE_PHONE; 那么优先级会降低一些,* 即拉下通知栏不可见*/params.format= PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明// 设置Window flagparams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

/** 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。* wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL |* LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;*/// 设置悬浮窗的长得宽params.width= 500;

params.height= 500;

// 设置悬浮窗的Touch监听btn_floatView.setOnTouchListener(newView.OnTouchListener()

{

intlastX, lastY;

intparamX, paramY;

public booleanonTouch(View v, MotionEvent event)

{

switch(event.getAction())

{

caseMotionEvent.ACTION_DOWN:

lastX= (int) event.getRawX();

lastY= (int) event.getRawY();

paramX= params.x;

paramY= params.y;

break;

caseMotionEvent.ACTION_MOVE:

intdx = (int) event.getRawX() - lastX;

intdy = (int) event.getRawY() - lastY;

params.x= paramX+ dx;

params.y= paramY+ dy;

// 更新悬浮窗位置wm.updateViewLayout(btn_floatView, params);

break;

}

return true;

}

});

wm.addView(btn_floatView, params);

}

private voidcreateButtonView()

{

btn_floatView= newButton(getApplicationContext());

btn_floatView.setText("悬浮窗");

mFloatView= mLayoutInflater.inflate(R.layout.play_view, null);

wm= (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

finalWindowManager.LayoutParams params = newWindowManager.LayoutParams();

// 设置window typeparams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

/** 如果设置为params.type = WindowManager.LayoutParams.TYPE_PHONE; 那么优先级会降低一些,* 即拉下通知栏不可见*/params.format= PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明// 设置Window flagparams.flags= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

/** 下面的flags属性的效果形同“锁定”。 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。* wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL |* LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE;*/// 设置悬浮窗的长得宽params.width= 100;

params.height= 100;

// 设置悬浮窗的Touch监听btn_floatView.setOnTouchListener(newView.OnTouchListener()

{

intlastX, lastY;

intparamX, paramY;

public booleanonTouch(View v, MotionEvent event)

{

switch(event.getAction())

{

caseMotionEvent.ACTION_DOWN:

lastX= (int) event.getRawX();

lastY= (int) event.getRawY();

paramX= params.x;

paramY= params.y;

break;

caseMotionEvent.ACTION_MOVE:

intdx = (int) event.getRawX() - lastX;

intdy = (int) event.getRawY() - lastY;

params.x= paramX+ dx;

params.y= paramY+ dy;

// 更新悬浮窗位置wm.updateViewLayout(btn_floatView, params);

break;

}

return true;

}

});

wm.addView(btn_floatView, params);

}

private voidcreateView() {

//TODO Auto-generated method stub//加载布局文件mFloatView= mLayoutInflater.inflate(R.layout.play_view, null);

videoView= (VideoView)mFloatView.findViewById(R.id.videoView);

//为View设置监听,以便处理用户的点击和拖动mFloatView.setOnTouchListener(newOnFloatViewTouchListener());

/*为View设置参数*/mLayoutParams= newWindowManager.LayoutParams();

//设置View默认的摆放位置mLayoutParams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

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

//设置window typemLayoutParams.type= WindowManager.LayoutParams.TYPE_PHONE;//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!//设置背景为透明// mLayoutParams.format = PixelFormat.RGBA_8888;//注意该属性的设置很重要,FLAG_NOT_FOCUSABLE使浮动窗口不获取焦点,若不设置该属性,屏幕的其它位置点击无效,应为它们无法获取焦点mLayoutParams.flags= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

//设置视图的显示位置,通过WindowManager更新视图的位置其实就是改变(x,y)的值mCurrentX= mLayoutParams.x= 0;

mCurrentY= mLayoutParams.y= 0;

//设置视图的宽、高mLayoutParams.width= 700;

mLayoutParams.height= 700;

//将视图添加到Window中mWindowManager.addView(mFloatView, mLayoutParams);

mWindowManager.getDefaultDisplay();

screen= getResources().getDisplayMetrics();

videoView.setVideoPath("/sdcard/test1.ts");

videoView.start();

}

private classOnFloatViewTouchListener implementsView.OnTouchListener {

@Overridepublic booleanonTouch(View v, MotionEvent event) {

//TODO Auto-generated method stubLog.i("baiyuliang", "mCurrentX: "+ mCurrentX+ ",mCurrentY: "+ mCurrentY+ ",mFloatViewWidth: "+ mFloatViewWidth+ ",mFloatViewHeight: "+ mFloatViewHeight);

if(event.getAction() == MotionEvent.ACTION_DOWN)

{

if(!isFull)

{

createButtonView();

}

else{

wm.removeView(btn_floatView);

}

isFull= !isFull;

/*if (!isFull) {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);isFull = true;mLayoutParams.width = screen.heightPixels;mLayoutParams.height = screen.widthPixels;mWindowManager.updateViewLayout(mFloatView, mLayoutParams);videoView.getHolder().setFixedSize(mLayoutParams.width, mLayoutParams.height);} else {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);mLayoutParams.width = 300;mLayoutParams.height = 300;mWindowManager.updateViewLayout(mFloatView, mLayoutParams);videoView.getHolder().setFixedSize(300, 300);isFull = false;}*/}

/** getRawX(),getRawY()这两个方法很重要。通常情况下,我们使用的是getX(),getY()来获得事件的触发点坐标,* 但getX(),getY()获得的是事件触发点相对与视图左上角的坐标;而getRawX(),getRawY()获得的是事件触发点* 相对与屏幕左上角的坐标。由于LayoutParams中的x,y是相对与屏幕的,所以需要使用getRawX(),getRawY()。*/mCurrentX= (int) event.getRawX() - mFloatViewWidth;

mCurrentY= (int) event.getRawY() - mFloatViewHeight;

intaction = event.getAction();

switch(action) {

caseMotionEvent.ACTION_DOWN:

break;

caseMotionEvent.ACTION_MOVE:

//updateFloatView();break;

caseMotionEvent.ACTION_UP:

break;

}

return true;

}

}

@OverridepublicObject onRetainNonConfigurationInstance() {

return super.onRetainNonConfigurationInstance();

}

@Overridepublic voidonConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

}

private voidupdateFloatView() {

mLayoutParams.x= mCurrentX;

mLayoutParams.y= mCurrentY;

mWindowManager.updateViewLayout(mFloatView, mLayoutParams);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值