android浮标制作
浮标项目
浮标特性
- 触摸浮标移动特性
- 点击浮标展开特性
- 3s内无操作,浮标自动掩藏在两侧(由于浮标只用了一张图片,所以两侧掩藏时只是靠边了)
效果图
实现原理
创建浮标
- 获取WindowManager系统服务
- 设置浮标显示的参数WindowManager.LayoutParams
- 获取浮标布局并添加到WindowManager里面去
- 代码示例:
mMansger = (WindowManager)context.getApplicationContext().getSystemService(Activity.WINDOW_SERVICE);
mParams = new WindowManager.LayoutParams();
screen_widht = mMansger.getDefaultDisplay().getWidth();
screen_height = mMansger.getDefaultDisplay().getHeight();
mParams.format = PixelFormat.RGBA_8888; //图片格式为透明
mParams.type = WindowManager.LayoutParams.TYPE_PHONE; //处于所有应用顶端,状态栏之下
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; //不获取焦点
mParams.gravity = Gravity.LEFT | Gravity.TOP; //左上对齐
mParams.x = float_x;
mParams.y = float_y; //相对于上面的原点
mParams.width = LayoutParams.WRAP_CONTENT;
mParams.height = LayoutParams.WRAP_CONTENT;
mFloatLayout = (LinearLayout)LayoutInflater.from(context).inflate(R.layout.float_window, null);
mMansger.addView(mFloatLayout, mParams);
触摸移动
设置浮标移动监听OnTuchListener,监听工作:
1. 获取当前触摸位置,并将此位置更新至布局updateViewLayout
2. 代码示例:
floatImage.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
isMove = true;
if(event.getAction() == MotionEvent.ACTION_DOWN){
return false;
}
//滑动操作
if(mFloatLayout != null){
float_x = (int)event.getRawX();
float_y = (int)event.getRawY();
int width = mFloatLayout.getWidth();
int height = mFloatLayout.getHeight();
if(float_x + width > screen_widht){
float_x = screen_height - width;
}
if(float_y + height > screen_height){
float_y = screen_height - height;
}
//作画默认是从view的左上角画,导致在移动过程中摸不着view 所以要向上和左位置前移就能摸到了
mParams.x = (float_x - width/2) > 0 ? float_x - width/2 : 0;
mParams.y = (float_y - height/2) > 0 ? float_y - height/2 : 0;
mMansger.updateViewLayout(mFloatLayout, mParams);
}
/**
* 抬起默认滑动结束
*/
if(event.getAction() == MotionEvent.ACTION_UP){
isMove = false;
task_restore.postDelayed(retoreFloatView, 3000); //3s内无操作进行隐藏工作
}
return false;
}
});
点击监听
- 点击就是setOnclickListener,这个很简单就不进行说明了
自动掩藏
- 原理:
利用Handler延时操作执行浮标掩藏工作,其原理就是在滑动时抬起动作和点击事件后,执行掩藏,刷新的最后一步判断浮标是否还有操作,有操作就不掩藏了,反之则掩藏 - 代码示例:
private Runnable retoreFloatView = new Runnable() {
@Override
public void run() {
account_left.setVisibility(View.GONE);
account_right.setVisibility(View.GONE);
if(float_x > screen_widht / 2){
floatImage.setImageDrawable(mContext.getDrawable(R.drawable.xy_icon)); //设置右边浮标icon
float_x = screen_widht - mFloatLayout.getWidth();
}else{
floatImage.setImageDrawable(mContext.getDrawable(R.drawable.xy_icon));
float_x = 0;
}
mParams.x = float_x;
if(isMove){
return;
}
mMansger.updateViewLayout(mFloatLayout, mParams);
}
};
使用方法
- clone工程至本地
- import as moudle
- 调用FloatUtil类即可使用
FloatUtil.getInstance().createFloatView(this);
FloatUtil.getInstance().removeFloatView();
github项目链接:
[“Click here点击这里”](https://github.com/JackZhous/FloatView
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】