欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android 浮动按钮

程序员文章站 2022-04-29 11:30:40
...
两个半透明的浮动按钮


private WindowManager wm = null;
private WindowManager.LayoutParams wmParams = null;
private Button play1;
private Button cache1;
private int mAlpha = 0;
private ViewFlipper viewFlipper = null;



@Override
protected void onResume() {
// TODO Auto-generated method stub
//启动的方法不用写在oncreate中
super.onResume();
System.out.println("resume");
initFloatview();
play1 = new Button(this);
cache1 = new Button(this);
createLeftButton();
createRightButton();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//退出时删除
wm.removeView(play1);
wm.removeView(cache1);
}

private void initFloatview() {
wm = (WindowManager) getApplicationContext().getSystemService("window");
wmParams = new WindowManager.LayoutParams();
// wmParams=new WindowManager.LayoutParams();
// wmParams.type=LayoutParams.TYPE_PHONE;
// wmParams.format=PixelFormat.RGBA_8888;
// wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_NOT_FOCUSABLE;
// wmParams.x=0;
// wmParams.y=0;
// wmParams.width=100;
// wmParams.height=100;
}

// 左悬浮键
private void createLeftButton() {

wmParams.type = LayoutParams.TYPE_PHONE;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
play1.setBackgroundResource(R.drawable.tab_selected);
play1.getBackground().setAlpha(100);
// play1.setAlpha(0);
play1.setText("播放");
play1.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
play1.setBackgroundResource(R.drawable.tab_selected_focs);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
play1
.setBackgroundResource(R.drawable.tab_selected_pressed);
cache1.setBackgroundResource(R.drawable.tab_selected);
cache1.getBackground().setAlpha(100);
}

return false;
}

});

play1.setOnClickListener(listener);
wmParams.width = 210;
wmParams.height = 60;
wmParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
wmParams.x = 30;
wmParams.y = 10;
wm.addView(play1, wmParams);
}
// 右悬浮键
private void createRightButton() {
wmParams.type = LayoutParams.TYPE_PHONE;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
cache1.setBackgroundResource(R.drawable.tab_selected);
cache1.getBackground().setAlpha(100);
cache1.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cache1.setBackgroundResource(R.drawable.tab_selected_focs);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
cache1
.setBackgroundResource(R.drawable.tab_selected_pressed);
play1.setBackgroundResource(R.drawable.tab_selected);
play1.getBackground().setAlpha(100);
}
return false;
}
});
// cache1.setAlpha(0);
cache1.setText("缓存");
cache1.setOnClickListener(listener);
wmParams.width = 210;
wmParams.height = 60;
wmParams.gravity = Gravity.RIGHT | Gravity.BOTTOM;
wmParams.x = 30;
wmParams.y = 10;
wm.addView(cache1, wmParams);
}