有些直播带货app中经常会用到一个全局可悬浮的按钮、或者窗口,今天我们就来介绍一下直播带货app源码中如何制作“可跟随手指拖动的全局悬浮窗”。
一、自定义一个跟随手指滑动的View
在直播带货app源码中自定义一个跟随手指滑动的View很简单,本篇的教程中只支持简单的悬浮拖动,后期可在此基础上进行功能扩展,挺简单的,不多说了,直接上代码:
public class FloatWindowImageView extends AppCompatImageView implements View.OnTouchListener {
private WindowManager windowManager;
private WindowManager.LayoutParams layoutParams;
private Boolean isMove = false;
private float lastX;
//构造方法,初始化
Public FloatWindowImageView(WindowManager windowManager,WindowManager.LayoutParams layoutParams,Context context) {
super(context);
this.windowManager = windowManager;
this.layoutParams = layoutParams;
setOnTouchListener(this);
setOnClickListener(v -> Toast.makeText(context,"点击了",Toast.LENGTH_LONG).show());
requestSettingCanDrawOverlays();
}
public FloatWindowImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FloatWindowImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
//在直播带货app源码中设置拖动事件
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getRawX();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
isMove = false;
break;
case MotionEvent.ACTION_MOVE:
if (Math.abs(x - lastX) > ViewConfiguration.get(getContext()).getScaledTouchSlop()){
isMove = true;
layoutParams.x = (int) (event.getRawX() - getWidth() / 2);
layoutParams.y = (int) (event.getRawY() - getHeight() / 2);
//更新窗口位置
windowManager.updateViewLayout(this,layoutParams);
}
return true;
case MotionEvent.ACTION_UP:
if (isMove){
return true;
}
}
lastX = x;
return super.onTouchEvent(event);
}
// 申请悬浮权限
private void requestSettingCanDrawOverlays() {
try {
//判断当前系统版本
if (Build.VERSION.SDK_INT >= 23) {
//判断权限是否已经申请过了(加上这个判断,则使用的悬浮窗的时候;如果权限已经申请则不再跳转到权限开启界面)
if (!Settings.canDrawOverlays(getContext())) {
//申请权限
Intent intent2 = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
((Activity)getContext()).startActivityForResult(intent2, 1001);
} else {
//创建悬浮窗
windowManager.addView(this, layoutParams);
}
} else {
windowManager.addView(this, layoutParams);
}
System.out.println("Build.VERSION.SDK_INT::::" + Build.VERSION.SDK_INT);
} catch (Exception e) {
e.printStackTrace();
}
}
// 申请完权限后显示出来
public void showFloatWindow(){
windowManager.addView(this, layoutParams);
}
}
按上面的直播带货app源码,就基本实现了全局的悬浮效果,接下来就是在Activity中使用了:
WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
//根据Android版本的不同,需要设置不同的type,不设置的话,有些手机系统会报错
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//8.0
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
}
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//设置窗口的起始位置
layoutParams.x = 0;
layoutParams.y = 0;
layoutParams.gravity = Gravity.START | Gravity.TOP;
//设置悬浮窗口的大小
layoutParams.width = 200;
layoutParams.height = 200;
imageView = new FloatWindowImageView(windowManager,layoutParams,this);
//设置图片
imageView.setImageResource(R.drawable.weixin);
以上就是在直播带货app源码中实现一个简单的悬浮窗口,如果想实现更多的功能窗口,可以在此基础上进行改造。
声明:以上内容为csdn作者:云豹直播官方 原创,未经公司同意,禁止转载,否则将追究相关法律责任