利用Android 自定义的悬浮播放器实例
在这个工程中最为重要的就是player.xml和ObjectActivity了
首先是播放器这个控件的布局,开始我试着用各种布局都没有能完成我要的效果,因为我用的进度显示是自定义的seekbar似乎它默认会在上下留一定的空白,所以我最后只好用绝对布局了,因为播放器的大小是固定的而且它不依赖于父控件的大小所以这种布局效果还不错
player.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/slidingDrawer1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:baselineAligned="false" android:orientation="horizontal" > <AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_x="0dp" android:layout_y="8dp" > <LinearLayout android:id="@+id/content" android:layout_width="wrap_content" android:layout_height="52dp" android:background="@drawable/img_player_bacakground" android:gravity="center_vertical" android:orientation="horizontal" android:visibility="visible" > <ImageButton android:id="@+id/ibn_start_suspend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dip" android:background="@drawable/bnx_suspend" /> <ImageButton android:id="@+id/ibn_previous" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dip" android:background="@drawable/bnx_previous" /> <ImageButton android:id="@+id/ibn_next" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dip" android:background="@drawable/bnx_next" /> <TextView android:id="@+id/tv_songname" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_marginLeft="5dip" android:layout_marginRight="5dip" android:background="@drawable/img_player_kuang" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:gravity="center" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:singleLine="true" android:textSize="16sp" /> <ImageButton android:id="@+id/ibn_mode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/bnx_cycle" /> </LinearLayout> <ImageButton android:id="@+id/hand" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_toRightOf="@id/content" android:background="@drawable/img_arrow_left" /> </RelativeLayout> <SeekBar android:id="@+id/pb" style="@style/SeekBar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_x="0dp" android:layout_y="-5dp" /> </AbsoluteLayout> </LinearLayout> </RelativeLayout>
接下来是ObjectActivity
它继承于Activity添加这么一层是为了让希望拥有这个悬浮mini播放器的类获得而不需要有的类不需要继承此类
最后是自定义seekbar所使用的style
看一下效果图:
源码下载:悬浮mini音乐播放器.zip
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构