利用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
浙公网安备 33010602011771号