短视频系统源码,关于悬浮窗的缩放、拖动等应用
短视频系统源码,关于悬浮窗的缩放、拖动等应用实现的相关代码
缩放方法
缩放activity需要使用WindowManager.LayoutParams,控制window的宽高
在activity中调用
android.view.WindowManager.LayoutParams p = getWindow().getAttributes();p.height = 480; // 高度p.width = 360; // 宽度p.dimAmount = 0.0f; // 不让下面的界面变暗getWindow().setAttributes(p);
dim:
adj. 暗淡的; 昏暗的; 微弱的; 不明亮的; 光线暗淡的;
v. (使)变暗淡,变微弱,变昏暗; (使)减弱,变淡漠,失去光泽;
修改了WindowManager.LayoutParams的宽高,activity的window大小会发生变化。
要变回默认大小,在activity中调用
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
如果缩小时改变了位置,需要把window的位置置为0
WindowManager.LayoutParams lp = getWindow().getAttributes();lp.x = 0;lp.y = 0;getWindow().setAttributes(lp);
activity变小时,后面可能是黑色的背景。这需要进行下面的操作。
悬浮样式#
在styles.xml
里新建一个MeTranslucentAct
。
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="windowNoTitle">true</item> </style> <style name="TranslucentAct" parent="AppTheme"> <item name="android:windowBackground">#80000000</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowAnimationStyle">@android:style/Animation.Translucent</item> </style></resources>
主要style是AppCompat的。
指定一个window的背景android:windowBackground
使用的Activity继承自androidx.appcompat.app.AppCompatActivity
activity缩小后,背景是透明的,可以看到后面的其他页面
点击穿透空白#
activity缩小后,点击旁边空白处,其他组件能接到点击事件
在onCreate
方法的setContentView
之前,给WindowManager.LayoutParams添加标记FLAG_LAYOUT_NO_LIMITS
和FLAG_NOT_TOUCH_MODAL
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();layoutParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;mBinding = DataBindingUtil.setContentView(this, R.layout.act_float_scale);
移动悬浮窗#
监听触摸事件,计算出手指移动的距离,然后移动悬浮窗。
private boolean mIsSmall = false; // 当前是否小窗口private float mLastTx = 0; // 手指的上一个位置xprivate float mLastTy = 0;// .... mBinding.root.setOnTouchListener((v, event) -> { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(TAG, "down " + event); mLastTx = event.getRawX(); mLastTy = event.getRawY(); return true; case MotionEvent.ACTION_MOVE: Log.d(TAG, "move " + event); float dx = event.getRawX() - mLastTx; float dy = event.getRawY() - mLastTy; mLastTx = event.getRawX(); mLastTy = event.getRawY(); Log.d(TAG, " dx: " + dx + ", dy: " + dy); if (mIsSmall) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.x += dx; lp.y += dy; getWindow().setAttributes(lp); } break; case MotionEvent.ACTION_UP: Log.d(TAG, "up " + event); return true; case MotionEvent.ACTION_CANCEL: Log.d(TAG, "cancel " + event); return true; } return false; });
mIsSmall
用来记录当前activity是否变小(悬浮)。
在触摸监听器中返回true,表示消费这个触摸事件。
event.getX()
和event.getY()
获取到的是当前View的触摸坐标。event.getRawX()
和event.getRawY()
获取到的是屏幕的触摸坐标。即触摸点在屏幕上的位置。
以上就是 短视频系统源码,关于悬浮窗的缩放、拖动等应用实现的相关代码,更多内容欢迎关注之后的文章
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界