安卓仿苹果音量调节_Android 模仿苹果虚拟悬浮按钮(自动靠边、可浮现任何界面上)...

public classFloatViewService extends Service {private static final String TAG = "FloatViewService";//定义浮动窗口布局

privateLinearLayout mFloatLayout;privateWindowManager.LayoutParams wmParams;//创建浮动窗口设置布局参数的对象

privateWindowManager mWindowManager;privateImageButton mFloatView;private intscreenHeight;private intscreenWidth;privateMyCountDownTimer myCountDownTimer;

@Overridepublic voidonCreate() {

super.onCreate();

Log.i(TAG,"onCreate");

screenHeight= ScreenParam.getInstance().height;//自己写的工具类用来获取屏幕宽高。

screenWidth =ScreenParam.getInstance().width;

createFloatView();

myCountDownTimer= new MyCountDownTimer(2500, 1000); //设置计时2.5s

myCountDownTimer.start();

}

@SuppressWarnings("static-access")

@SuppressLint("InflateParams")private voidcreateFloatView() {

wmParams= newWindowManager.LayoutParams();//通过getApplication获取的是WindowManagerImpl.CompatModeWrapper

mWindowManager =(WindowManager) getApplication().getSystemService(

getApplication().WINDOW_SERVICE);//设置window type

wmParams.type =LayoutParams.TYPE_PHONE;//设置图片格式,效果为背景透明

wmParams.format =PixelFormat.RGBA_8888;//设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)

wmParams.flags =LayoutParams.FLAG_NOT_FOCUSABLE;//调整悬浮窗显示的停靠位置为右侧底部

wmParams.gravity = Gravity.RIGHT |Gravity.BOTTOM;//以屏幕左上角为原点,设置x、y初始值,相对于gravity

wmParams.x = 0;

wmParams.y= 150;//设置悬浮窗口长宽数据

wmParams.width =WindowManager.LayoutParams.WRAP_CONTENT;

wmParams.height=WindowManager.LayoutParams.WRAP_CONTENT;

LayoutInflater inflater= LayoutInflater.from(getApplication());//获取浮动窗口视图所在布局

mFloatLayout =(LinearLayout) inflater.inflate(

R.layout.alert_window_menu,null);//添加mFloatLayout

mWindowManager.addView(mFloatLayout, wmParams);//浮动窗口按钮

mFloatView =(ImageButton) mFloatLayout

.findViewById(R.id.alert_window_imagebtn);

mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,

View.MeasureSpec.UNSPECIFIED), View.MeasureSpec

.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));//设置监听浮动窗口的触摸移动

mFloatView.setOnTouchListener(newOnTouchListener() {

boolean isClick;private intleftDistance;private floatrawX;private floatrawY;

@SuppressLint("ClickableViewAccessibility")

@Overridepublic boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {caseMotionEvent.ACTION_DOWN:

mFloatLayout.setAlpha(1.0f);

myCountDownTimer.cancel();取消计时

rawX= event.getRawX();

rawY= event.getRawY();break;caseMotionEvent.ACTION_MOVE://getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标

int distanceX = (int) (event.getRawX()-rawX);int distanceY = (int) (event.getRawY()-rawY);

leftDistance= (int) event.getRawX()+ mFloatView.getMeasuredWidth() / 2;

wmParams.x= wmParams.x-distanceX;

wmParams.y= wmParams.y-distanceY;//刷新

mWindowManager.updateViewLayout(mFloatLayout, wmParams);

rawX= event.getRawX();

rawY= event.getRawY();break;caseMotionEvent.ACTION_UP:

myCountDownTimer.start();重新开始计时if(wmParams.x>leftDistance){

wmParams.x= screenWidth-mFloatView.getMeasuredWidth() / 2;

}else{

wmParams.x= 0;

}

mWindowManager.updateViewLayout(mFloatLayout, wmParams);break;

}return false;

}

});

mFloatView.setOnClickListener(newOnClickListener() {

@Overridepublic voidonClick(View v) {

AudioManager audioManager=(AudioManager) getSystemService(Service.AUDIO_SERVICE);

audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,

audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), AudioManager.FLAG_SHOW_UI);//Toast.makeText(FloatViewService.this, "hello!",//Toast.LENGTH_SHORT).show();

}

});

}

@Overridepublic voidonDestroy() {

super.onDestroy();if (mFloatLayout != null) {//移除悬浮窗口

mWindowManager.removeView(mFloatLayout);

}

}

@OverridepublicIBinder onBind(Intent intent) {return null;

}public classMyCountDownTimer extends CountDownTimer {public MyCountDownTimer(long millisInFuture, longcountDownInterval) {

super(millisInFuture, countDownInterval);

}

@Overridepublic void onTick(longmillisUntilFinished) {

}

@Overridepublic voidonFinish() {

mFloatLayout.setAlpha(0.4f);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值