团队冲刺第六天
1.今天完善了一下。地图定位界面功能。理想的是实现倒计时之后进行跑步。之后出现了一些问题今天可虽然不多但另外有考试。总体的编写时间不是很长。新增的布局代码如下以及相关的效果图:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/layout_bg" android:id="@+id/sport_content" tools:context=".DynamicDemo"> <RelativeLayout android:id="@+id/rlMap" android:layout_width="match_parent" android:layout_height="match_parent"> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/sport_change_bg"> <LinearLayout android:layout_width="match_parent" android:layout_height="@dimen/title_height" android:layout_marginTop="@dimen/spacing_normal" android:orientation="horizontal"> <TextView android:id="@+id/tv_mode" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/margin_edge" android:drawableStart="@mipmap/map_mode" android:drawablePadding="5dp" android:gravity="center_vertical" android:text="开始运动" android:textColor="@color/red" android:textColorLink="@color/red" android:textSize="@dimen/textsize_8" android:layout_marginLeft="@dimen/margin_edge" android:drawableLeft="@mipmap/map_mode" /> </LinearLayout> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_edge_20" android:layout_marginTop="110dp" android:orientation="vertical" android:layout_marginLeft="@dimen/margin_edge_20"> <Chronometer android:id="@+id/cm_passtime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:format="00:00:00" android:gravity="center" android:textColor="@color/text_color_white" android:textSize="65sp" android:textStyle="bold" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_edge_20" android:orientation="horizontal"> <TextView android:id="@+id/tvMileage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLength="8" android:text="0.00" android:textColor="@color/text_color_white" android:textSize="26sp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginStart="6dp" android:layout_marginBottom="2.5dp" android:text="公里" android:textColor="@color/text_color_white" android:textSize="@dimen/textsize_2" android:layout_marginLeft="6dp" /> <TextView android:id="@+id/tvSpeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="@dimen/margin_edge_20" android:ellipsize="end" android:maxLength="8" android:text="0.00" android:textColor="@color/text_color_white" android:textSize="26sp" android:textStyle="bold" android:layout_marginLeft="@dimen/margin_edge_20" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginStart="6dp" android:layout_marginBottom="2.5dp" android:text="配速(分/公里)" android:textColor="@color/text_color_white" android:textSize="@dimen/textsize_2" android:layout_marginLeft="6dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:orientation="horizontal" android:paddingBottom="30dp"> <TextView android:id="@+id/tv1" android:layout_width="70dp" android:layout_height="70dp" android:background="@drawable/sport_white_round" android:gravity="center" android:text="完成" android:textColor="@color/text_color_green" android:textSize="18sp" /> <TextView android:id="@+id/tv2" android:layout_width="70dp" android:layout_height="70dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:background="@drawable/sport_white_round" android:gravity="center" android:text="暂停" android:textColor="@color/text_color_green" android:textSize="18sp" /> <TextView android:id="@+id/tv3" android:layout_width="70dp" android:layout_height="70dp" android:background="@drawable/sport_white_round" android:gravity="center" android:text="继续" android:textColor="@color/text_color_green" android:textSize="18sp" /> </LinearLayout> <FrameLayout android:id="@+id/fl_count_timer" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#77000000" android:clickable="true" android:focusable="true" android:focusableInTouchMode="true"> <!--倒数时动画--> <TextView android:id="@+id/tv_number_anim" android:layout_width="140dp" android:layout_height="140dp" android:layout_gravity="center" android:gravity="center" android:textColor="@color/text_color_white" android:textSize="100sp" android:textStyle="bold" /> </FrameLayout> <!-- <Button android:id="@+id/buttonStart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="150dip" android:layout_marginTop="10dip" android:text="开始" /> <Button android:id="@+id/buttonFinish" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="25dp" android:layout_marginTop="10dip" android:text="停止" /> --> <RelativeLayout android:id="@+id/progressBarRl" android:layout_width="match_parent" android:layout_height="80dp" android:layout_alignParentBottom="true" android:visibility="gone" android:background="@android:color/white"> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="@android:color/black" android:text="GPS信号连接中,请稍后..." /> <ProgressBar android:id="@+id/progressBar" android:layout_width="wrap_content" android:paddingLeft="20dp" android:layout_centerVertical="true" android:paddingRight="10dp" android:layout_toLeftOf="@id/info" android:layout_height="wrap_content" /> </RelativeLayout> </RelativeLayout>
2.今天完成了闹钟的添加功能,过程还算顺利,但是在最后实现长按删除闹钟事件的使用遇到了问题,无法实现长按删除,由于在闹钟列表里使用了Switch开关控件,所以我感觉和这个有关系,于是拿掉了Switch控件测试了一下,果然实现了长按删除,具体什么原因还不清楚,打算明天解决,然后再美化一下界面啥的。
package com.example.naozhong; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Calendar; public class AddAlarm extends AppCompatActivity implements View.OnClickListener { private TextView hour; private TextView minute1; private Button setTime; private EditText content; private Button save; private Calendar calendar; private AlarmOperator alarmOperator; private AlarmAdapter alarmAdapter; String hourformat; String minuteformat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addalarm); alarmOperator=new AlarmOperator(this); hour = findViewById(R.id.hour); minute1 = findViewById(R.id.minute); setTime = findViewById(R.id.set_time); setTime.setOnClickListener(this); content = findViewById(R.id.content); save = findViewById(R.id.save); save.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.set_time: addtime(); break; case R.id.save: addalarm(); break; } } private void addalarm() { String userhour=hour.getText().toString().trim(); String userminute=minute1.getText().toString().trim(); String usercontent=content.getText().toString().trim(); Alarm bean=new Alarm("2",userhour,userminute,usercontent,0); Log.d("volley", bean.hour); alarmOperator.insert(bean); Intent intent2 = new Intent(this, AlarmActivity.class); startActivity(intent2); } private void addtime() { calendar = Calendar.getInstance(); int mhour = calendar.get(Calendar.HOUR_OF_DAY); int mminute = calendar.get(Calendar.MINUTE); new TimePickerDialog(AddAlarm.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); Calendar currentTime=Calendar.getInstance(); if(calendar.getTimeInMillis()<=currentTime.getTimeInMillis()){ calendar.setTimeInMillis(calendar.getTimeInMillis()+24*60*60*1000); } hourformat = String.format(String.valueOf(hourOfDay)); minuteformat = String.format(String.valueOf(minute)); Toast.makeText(AddAlarm.this, "" + hourformat + ":" + minuteformat, Toast.LENGTH_SHORT).show(); hour.setText(hourformat); minute1.setText(minuteformat); } }, mhour, mminute, true).show(); } }
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alarm); addalarm=findViewById(R.id.addalarm); list=new ArrayList<>(); String username="2"; alarmOperator =new AlarmOperator(this); ListView alarmList=(ListView) findViewById(R.id.clock_list); alarmList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext()); builder.setMessage("确定删除?"); builder.setTitle("提示"); final Alarm[] alarms = {null}; builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alarms[0]=list.get(position); alarmOperator.deletebyuser("1",alarms[0].hour,alarms[0].minute); list.remove(position); alarmAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); return false; } }); init(username); alarmAdapter=new AlarmAdapter(this,list); alarmList.setAdapter(alarmAdapter); addalarm.setOnClickListener(l); }
3.今天已经全部完成从手机本地SD卡中读取歌曲信息,并实现了音乐的播放,暂停,上一首,下一首的功能。鉴于明天满课,所以明天打算把这个音乐播放的布局完整一下,并把一些显示信息的纰漏和不完整的信息弄好。
package com.example.keeprunning6; import android.Manifest; import android.content.ContentResolver; import android.content.pm.PackageManager; import android.database.Cursor; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; public class MainActivity extends AppCompatActivity { ImageView lastIv,playIv,nextIv,pauseIv; //音乐列表试图 private ListView listView; //音乐列表 private ArrayList<LocalMusicBean> list=new ArrayList<>(); //当前播放的音乐以及歌手 private TextView now_music_song,now_music_singer; private LocalMusicAdapter adapter; private int position; // 在播放音乐的位置 int currentPlayPosition=-1; //记录暂停音乐时进度条的位置 int currentPausePositionSong=0; MediaPlayer mediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView=findViewById(R.id.music_list); now_music_song=findViewById(R.id.local_music_botton_tv_song); now_music_singer=findViewById(R.id.local_music_bottom_tv_singer); playIv=findViewById(R.id.local_music_bottom_iv_play); lastIv=findViewById(R.id.local_music_bottom_iv_last); nextIv=findViewById(R.id.local_music_bottom_iv_next); mediaPlayer=new MediaPlayer(); //自定义数据 adapter=new LocalMusicAdapter(MainActivity.this,list); listView.setAdapter(adapter); //加载本地数据源 localMusicData(); //设置每一项的点击事件 playIv.setOnClickListener(l); lastIv.setOnClickListener(l); nextIv.setOnClickListener(l); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //上面的position参数是歌曲在列表中的索引 currentPlayPosition=position; LocalMusicBean musicBean=list.get(position); playMusicPosition(musicBean); } }); } //根据传入对象播放音乐 public void playMusicPosition(LocalMusicBean musicBean) { //底部显示的歌手名和歌曲名 now_music_singer.setText(musicBean.getSinger()); now_music_song.setText(musicBean.getSong()); stopMusic(); //重制多媒体音乐播放器 mediaPlayer.reset(); //改变地址,重新设置路径 try { mediaPlayer.setDataSource(musicBean.getPath()); playMusic(); } catch (IOException e) { e.printStackTrace(); } } //播放音乐的函数 /*根据currentPausePositionSong的数值判断 * 1.从暂停到播放 * 2.从停止到播放 * */ private void playMusic() { if (mediaPlayer!=null&&!mediaPlayer.isPlaying()) { //判断是否暂停,以判断音乐是否从头播放 if (currentPausePositionSong==0) { //从头开始播放 try { //因为之前重制了音乐,所以现在要做准备 mediaPlayer.prepare(); //准别完之后开始播放 mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } }else{ //从暂停开始播放 //将进度条挪动到指定的位置上 mediaPlayer.seekTo(currentPausePositionSong); //开始播放 mediaPlayer.start(); } playIv.setImageResource(R.mipmap.icon_pause); } } //暂停音乐的函数 private void pauseMusic() { if (mediaPlayer!=null&&mediaPlayer.isPlaying()) { currentPausePositionSong=mediaPlayer.getCurrentPosition(); mediaPlayer.pause(); playIv.setImageResource(R.mipmap.icon_play); } } //停止音乐的函数 private void stopMusic(){ if(mediaPlayer!=null){ //一暂停,改变currentPausePositionSong的数值为0 currentPausePositionSong=0; //先暂停音乐 mediaPlayer.pause(); //将暂停的音乐的播放条回到最初 mediaPlayer.seekTo(0); //再停止音乐的播放 mediaPlayer.stop(); playIv.setImageResource(R.mipmap.icon_play); } } protected void onDestroy(){ super.onDestroy(); stopMusic(); } private void localMusicData(){ //加载本地存储当中的音乐mp3文件到集合当中 //1.获取ContentResolver对象 ContentResolver resolver=getContentResolver(); //2.获取本地音乐存储的Uri地址 Uri uri= MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; //3.开始查询地址 Cursor cursor=resolver.query(uri,null,null,null,null); //4.遍历Cursor int id=0; while(cursor.moveToNext()){ String song=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); String singer=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); String album=cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); id++; //歌曲编号变成String类型 String sid=String.valueOf(id); String path=cursor.getString(cursor.getColumnIndex((MediaStore.Audio.Media.DATA))); // String duration=cursor.getString(cursor.getColumnIndex((MediaStore.Audio.Media.DURATION))); int duration=cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)); SimpleDateFormat sdf=new SimpleDateFormat("mm:ss"); String time = formatTime(duration); //将一行当中的数据封装到对象当中 LocalMusicBean bean=new LocalMusicBean(sid,song,singer,album,time,path); list.add(bean); } //数据变化,提示适配器更新 adapter.notifyDataSetChanged(); } // 转换歌曲时间的格式 public static String formatTime(int time) { if (time / 1000 % 60 < 10) { String tt = time / 1000 / 60 + ":0" + time / 1000 % 60; return tt; } else { String tt = time / 1000 / 60 + ":" + time / 1000 % 60; return tt; } } View.OnClickListener l = new View.OnClickListener() { @Override public void onClick(View v) { switch(v.getId()){ case R.id.local_music_bottom_iv_last: if (currentPlayPosition==0) { Toast.makeText(MainActivity.this,"这是第一首歌曲,没有上一曲了",Toast.LENGTH_SHORT); return; } currentPlayPosition=currentPlayPosition-1; LocalMusicBean lastBean=list.get(currentPlayPosition); playMusicPosition(lastBean); break; case R.id.local_music_bottom_iv_play: if (currentPlayPosition==-1) { //没有选中要播放的音乐 // Toast.makeText(this,"请选择要播放的音乐",Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this,"请选择要播放的音乐",Toast.LENGTH_SHORT).show(); return; } if (mediaPlayer.isPlaying()) { //此时处于播放状态,需要暂停音乐 pauseMusic(); }else{ //此时没有播放音乐,点击开始播放音乐 playMusic(); } break; case R.id.local_music_bottom_iv_next: if (currentPlayPosition==list.size()-1) { Toast.makeText(MainActivity.this,"这是最后一首歌曲,没有下一曲了",Toast.LENGTH_SHORT); return; } currentPlayPosition=currentPlayPosition+1; LocalMusicBean nextBean=list.get(currentPlayPosition); playMusicPosition(nextBean); break; } } }; }
截图: