Android音樂播放器制作 加入控制臺(tái)(三)
Android音樂播放器的運(yùn)行效果

這篇博客還是接著上一篇Android音樂播放器制作寫的,沒看過的可以去看看。
其中這個(gè)效果(圓形ImageView和控件勻速旋轉(zhuǎn)):

我前面的博客中寫到過我就不一一細(xì)說了:
圖片變成圓形:android圖片處理,讓圖片變成圓形
旋轉(zhuǎn):android圖片處理:讓圖片一直勻速旋轉(zhuǎn)
文字跑馬燈:TextView的跑馬燈效果以及TextView的一些屬性
具體實(shí)現(xiàn)
首先是布局文件中添加了如下代碼,這些代碼就是實(shí)現(xiàn)控制臺(tái)的,給整體設(shè)置了一個(gè)invisible,為了讓他點(diǎn)擊有音樂播放的時(shí)候控制臺(tái)才顯示出來:
<RelativeLayout
android:id="@+id/main_control_rl"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_alignParentBottom="true"
android:background="@drawable/bottom_control_shape"
android:visibility="invisible">
<com.duanlian.mymusicplayerdemo.view.CircleImageView
android:id="@+id/control_imageview"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@mipmap/duanlian" />
<TextView
android:id="@+id/control_singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/control_imageview"
android:text="歌手名"
android:textSize="15sp" />
/>
<TextView
android:id="@+id/control_song"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:layout_toRightOf="@+id/control_singer"
android:text="歌曲的名字是不是很長(zhǎng)"
android:textSize="16sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/control_imageview">
<Button
android:id="@+id/playing_btn_previous"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="30dp"
android:background="@drawable/last_select"
android:onClick="control" />
<Button
android:id="@+id/playing_btn_pause"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
android:background="@drawable/play_press"
android:onClick="control" />
<Button
android:id="@+id/playing_btn_next"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:background="@drawable/next_select"
android:onClick="control" />
</RelativeLayout>
</RelativeLayout>
其中的
<com.duanlian.mymusicplayerdemo.view.CircleImageView android:id="@+id/control_imageview" android:layout_width="65dp" android:layout_height="65dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@mipmap/duanlian" />
這個(gè)是自定義圓形圖片,之前的博客已經(jīng)說過了,具體可以去看,然后控制的這種效果是背景添加了一個(gè)shap

代碼如下:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="200.0dip" /> <solid android:color="#84C3D1" /> <stroke android:width="1.0dip" android:color="#ffff6000" /> </shape>
點(diǎn)擊上一曲下一期的變化效果:

添加了一個(gè)點(diǎn)擊的selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_window_focused="false" android:drawable="@drawable/last_normal" /> <item android:state_focused="true" android:drawable="@drawable/last_normal" /> <item android:state_pressed="true" android:drawable="@drawable/last_press" /> </selector>
布局文件搞定,下面是代碼中的實(shí)現(xiàn)
首先就是聲明的控件和一些變量增加了 這幾個(gè):
private int playPosition;//當(dāng)前播放歌曲的序號(hào) private boolean IsPlay = false;//是否有歌曲在播放 private Button playPause;//暫停和播放按鈕 private TextView song;//歌曲名 private TextView singer;//歌手名 private ImageView imageView;//控制臺(tái)的圖片 private Animation animation;//動(dòng)畫
點(diǎn)擊ListView的一下改變:
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//創(chuàng)建一個(gè)播放音頻的方法,把點(diǎn)擊到的地址傳過去
//list.get(i).path這個(gè)就是歌曲的地址
play(list.get(i).path);
////播放暫停按鈕圖片變成播放狀態(tài)
playPause.setBackgroundResource(R.drawable.pause_press);
//把當(dāng)前點(diǎn)擊的item的position拿到,知道當(dāng)前播放歌曲的序號(hào)
playPosition = i;
//播放音樂的時(shí)候把是否在播放賦值為true
IsPlay = true;
//點(diǎn)擊item讓控制臺(tái)顯示出來
findViewById(R.id.main_control_rl).setVisibility(View.VISIBLE);
}
});
然后就是幾個(gè)button的點(diǎn)擊事件了:
/**
* 底部控制欄的點(diǎn)擊事件
*
* @param view
*/
public void control(View view) {
switch (view.getId()) {
case R.id.playing_btn_previous://上一曲
//如果播放歌曲的序號(hào)小于或者等于0的話點(diǎn)擊上一曲就提示已經(jīng)是第一首了
if (playPosition <= 0) {
Toast.makeText(MainActivity.this, "已經(jīng)是第一首歌了", Toast.LENGTH_SHORT).show();
} else {
//讓歌曲的序號(hào)減一
playPosition--;
//播放
play(list.get(playPosition).path);
playPause.setBackgroundResource(R.drawable.pause_press);
}
break;
case R.id.playing_btn_pause://暫停和播放
if (IsPlay == false) {
//播放暫停按鈕圖片變成播放狀態(tài)
playPause.setBackgroundResource(R.drawable.pause_press);
//繼續(xù)播放
mediaPlayer.start();
imageView.startAnimation(animation);
IsPlay = true;//是否在播放賦值為true
animation.start();
Toast.makeText(MainActivity.this, "播放" + list.get(playPosition).song, Toast.LENGTH_SHORT).show();
} else {
//播放暫停按鈕圖片變成暫停狀態(tài)
playPause.setBackgroundResource(R.drawable.play_press);
//暫停歌曲
mediaPlayer.pause();
imageView.clearAnimation();//停止動(dòng)畫
IsPlay = false;//是否在播放賦值為false
Toast.makeText(MainActivity.this, "暫停" + list.get(playPosition).song, Toast.LENGTH_SHORT).show();
}
break;
case R.id.playing_btn_next://下一曲
//歌曲序號(hào)大于或者等于歌曲列表的大小-1時(shí),讓歌曲序號(hào)為第一首
if (playPosition >= list.size() - 1) {
playPosition = 0;
} else {
//點(diǎn)擊下一曲讓歌曲的序號(hào)加一
playPosition++;
}
//播放
play(list.get(playPosition).path);
//播放暫停按鈕圖片變成播放狀態(tài)
playPause.setBackgroundResource(R.drawable.pause_press);
break;
}
}
最后還有設(shè)置歌曲名和歌手名的:
/**
* 控制歌曲和歌手TextView的方法
*/
private void setText() {
song.setText(list.get(playPosition).song);
song.setSelected(true);//當(dāng)歌曲名字太長(zhǎng)是讓其滾動(dòng)
singer.setText(list.get(playPosition).singer);
}
就是這個(gè)簡(jiǎn)單
demo下載地址:音樂播放器
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式的兩種方法
這篇文章主要介紹了使用adb?or?fastboot命令進(jìn)入高通的9008(edl)模式,兩種方式通過命令給大家寫的非常詳細(xì),文中又給大家補(bǔ)充介紹了高通手機(jī)?進(jìn)入?高通9008模式的兩種方法,需要的朋友可以參考下2023-01-01
Android Mms之:聯(lián)系人管理的應(yīng)用分析
本篇文章是對(duì)Android中的聯(lián)系人管理進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Android仿視頻加載旋轉(zhuǎn)小球動(dòng)畫效果的實(shí)例代碼
這篇文章主要介紹了Android仿視頻加載旋轉(zhuǎn)小球動(dòng)畫效果的實(shí)例代碼,文中給大家提到了PathMeasure的用法,介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-09-09
Android開發(fā)中的錯(cuò)誤及解決辦法總結(jié)
本文屬于個(gè)人平時(shí)項(xiàng)目開發(fā)過程遇到的一些問題,記錄下來并總結(jié)解決方案,希望能幫到大家解決問題,需要的朋友可以參考下2022-02-02
Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能
這篇文章主要介紹了Android簡(jiǎn)單實(shí)現(xiàn)菜單拖拽排序的功能,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)價(jià)值,需要的朋友可以參考一下2022-07-07
Compose?for?Desktop?鼠標(biāo)事件示例demo
這篇文章主要為大家介紹了Compose?for?Desktop?鼠標(biāo)事件示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android 從底部彈出Dialog(橫向滿屏)的實(shí)例代碼
在android開發(fā)中經(jīng)常會(huì)遇到底部彈出框的功能,今天小編抽時(shí)間給大家整理一個(gè)底部彈出橫向滿屏的dialog,需要的朋友參考下2016-11-11

