前言:大概一週多以前(現在是2018.11.26 15:24)android平臺開發的課程結束了,要寫大作業,最後決定寫這個音樂播放器,因爲老師在課堂上講的例子也是這個,前面的作業也把一些東西實現了,我就打算把功能都做出來,UI搞得稍微好看一些。
還有很多想法沒實現,奈何期末考試臨近所以先做成這個樣子把作業交了。
先上圖,目前實現情況:
1.首先需要一個Song類用來存儲每一個歌曲對像,含參構造函數爲
public Song(long id,
long album_id,
int song_list_id,
int song_imageidid,
String song_name,
String song_author,
String song_addr,
long duration,
int isMusic,
String album) {
this.id = id;
this.album_id = album_id;//專輯id
this.song_list_id = song_list_id;在歌曲list中的位置
this.song_imageid = song_imageidid;//後面在song_item佈局中會用到的圖片的id
this.song_name = song_name;//歌曲名字
this.song_author = song_author;//歌手
this.song_addr = song_addr;//文件位置
this.duration = duration;//歌曲時長
this.isMusic = isMusic;//是否是music
this.album = album;//專輯
}
2.然後是登錄界面,這個界面是這個應用的主activity,背景是實現的動態背景,加載的MP4文件,視頻來源抖音,我自己用pr剪輯的,不太會用pr所以就隨便剪輯了一下將就用了。
2.1動態背景:整個佈局最外層使用的FrameLayout, orientation設置爲vetical
<xxx.xxxx.xxxx.musciplayer.dynamicBackGround.VideoBackground
android:id="@+id/videoview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
注:xxx.xxxx.xxx是我建立工程時設置的Company domain,不想被看到就換成了x
這一段就是動態背景,接着建立VideoBackground類,繼承自VideoView類
public class VideoBackground extends VideoView {
public VideoBackground(Context context) {
super(context);
}
public VideoBackground(Context context, AttributeSet attrs) {
super(context, attrs);
}
public VideoBackground(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
//重寫onMeasure方法,爲了更好的自適應全屏幕
//作用是返回一個默認的值,如果MeasureSpec沒有強制限制的話則使用提供的大小.
// 否則在允許範圍內可任意指定大小
//第一個參數size爲提供的默認大小,第二個參數爲測量的大小
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//我們重新計算高度
int width = getDefaultSize(0, widthMeasureSpec);
int height = getDefaultSize(0, heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
在主activity對應的類中實現方法initbackground()方法:
public void initBackground() {
//找VideoView控件
videoBackground = findViewById(R.id.videoview); //加載視頻佈局
videoBackground.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.stars));
// 播放
videoBackground.start();
//循環播放 設置一個在媒體文件播放完畢,到達終點時調用的回調
videoBackground.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoBackground.start();
}
});
}
注:視頻文件放在raw文件夾下,這個文件夾被新建且處於res目錄中
2.2 用戶名輸入框和密碼輸入框——兩個EditText控件
我給他們實現了邊框,這需要自定義佈局文件,設置solid(影響背景顏色)、corners(影響邊框邊緣弧度)、stroke(可影響邊框線寬度和顏色),使用android:backgroundddi調用自定義佈局文件即可
設置android:singleLine可以設置輸入爲單行輸入
設置android:inputType="numberPassword"可以設置輸入爲數字密碼
android:padding設置內邊距可以使得字體和邊緣看起來不那麼擁擠
2.3 記住用戶名和記住密碼單選框——兩個CheckBox控件
android:checked="true"可以使得單選框默認選中
2.4 登錄按鈕——Button
同樣實現了自定義佈局文件更改背景和邊框樣式
2.5 登錄界面的整體佈局結構
2.6 登錄界面的邏輯
寫了三個方法:
initButtonDeal();//處理登錄按鈕的事件
initBackground();//初始化動態背景
dealCheckBox();//處理複選框相關事件,有部分在匹配密碼後處理
2.6.1 initButtonDeal() 處理登錄按鈕的點擊事件,即判斷密碼是否正確,保存到本地等操作
SharedPreferences.Editor editor,editor_pas;
editor = getSharedPreferences("username_data", MODE_PRIVATE).edit();
editor_pas = getSharedPreferences("password_data", MODE_PRIVATE).edit();
在這裏用SharedPreferences使用鍵值對的方式來存儲數據,
調用putString()存入字符串數據,調用apply()將數據提交,調用clear()清除數據
MODE_PRIVATE是默認的操作模式,和直接傳入0效果是相同的,表示只有當前的應用程序纔可以對這個SharedPreferences文件進行讀寫
可以用isChecked()獲取CheckBox狀態,得知是否勾選了記住用戶名或者記住密碼
同時檢測密碼正確後會跳轉至第二個activity,並且將當前的用戶名數據傳過去
這採用顯式Intent實現
Intent intent = new Intent(LoginActivity.this, DisplayActivity.class);
intent.putExtra("userName", userStr);
startActivity(intent);
如果密碼不正確則彈出消息要求重試
2.6.2 dealCheckBox()處理的事件是在啓動程序時去檢測本地有沒有以前保存的用戶名數據和密碼數據,如果有就加載。
//加載默認用戶名
SharedPreferences pref = getSharedPreferences("username_data", MODE_PRIVATE);
String default_name = pref.getString("name", "");
if (!default_name.isEmpty()) {
user.setText(default_name);
}
加載密碼操作同上
2.6.3 initBackground() 初始化動態背景
首先把視頻佈局實例化對象,然後使用setVideoURI設置具體視頻文件,例:
Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.stars
使用start()方法啓動,setOnCompletionListener設置一個在媒體文件播放完畢,到達終點時調用的回調
同時要注意重寫Restart()方法防止重新加載這個activity時黑屏
重寫onStop()方法調用 stopPlayback() 防止切出屏幕時背景音樂還在播放
後面的內容見下一篇