Android开发之seekbar动态显示进度
2016-08-09 16:43
656 查看
最近有做一个android项目,里面有使用到在播放音乐时可以跳播,同时动态显示播放时间。类似于下图 的效果,我只是抽取其中的一部分做展示,刚接到这个事时也是在网上一通找,哈哈请看过程:
![](http://static.oschina.net/uploads/space/2015/0209/155204_Sj47_945545.png)
![](http://static.oschina.net/uploads/space/2015/0209/161411_S7Gb_945545.png)
1:第一个类是自定义的类,也就是SeekBar的上方会跟随其一块移动的控件,其实非常简单
![](http://static.oschina.net/uploads/space/2015/0209/161256_hVkr_945545.png)
![](http://static.oschina.net/uploads/space/2015/0209/161304_aM2F_945545.png)
色值:bg_whitef5:#F5F5F5
bg_lin_95:#959595
![](http://static.oschina.net/uploads/space/2015/0209/155204_Sj47_945545.png)
![](http://static.oschina.net/uploads/space/2015/0209/161411_S7Gb_945545.png)
1:第一个类是自定义的类,也就是SeekBar的上方会跟随其一块移动的控件,其实非常简单
<span style="font-size:14px;">package com.example.textmovebyseekbar; import android.content.Context; import android.util.AttributeSet; import android.view.ViewGroup; //import android.widget.AbsoluteLayout; public class TextMoveLayout extends ViewGroup { public TextMoveLayout(Context context) { super(context); // TODO Auto-generated constructor stub } public TextMoveLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public TextMoveLayout(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub } }</span>2:第二个类就是MainActivity了,稍微有点长但也是比较简单的
<span style="font-size:14px;">package com.example.textmovebyseekbar; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.ViewGroup; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends Activity { private SeekBar seekbar = null; private String startTimeStr = "19:30:33"; private String endTimeStr = "21:23:21"; private TextView text, startTime, endTime; /** * 视频组中第一个和最后一个视频之间的总时长 */ private int totalSeconds = 0; /** * 屏幕宽度 */ private int screenWidth; /** * 自定义随着拖动条一起移动的空间 */ private TextMoveLayout textMoveLayout; private ViewGroup.LayoutParams layoutParams; /** * 托动条的移动步调 */ private float moveStep = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.video_fast_search); screenWidth = getWindowManager().getDefaultDisplay().getWidth(); text = new TextView(this); text.setBackgroundColor(Color.rgb(245, 245, 245)); text.setTextColor(Color.rgb(0, 161, 229)); text.setTextSize(16); layoutParams = new ViewGroup.LayoutParams(screenWidth, 50); textMoveLayout = (TextMoveLayout) findViewById(R.id.textLayout); textMoveLayout.addView(text, layoutParams); text.layout(0, 20, screenWidth, 80); /** * findView */ seekbar = (SeekBar) findViewById(R.id.seekbar); startTime = (TextView) findViewById(R.id.start_time); endTime = (TextView) findViewById(R.id.end_time); /** * setListener */ seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListenerImp()); searchVideos(); } public void searchVideos() { startTime.setText(startTimeStr); endTime.setText(endTimeStr); text.setText(startTimeStr); totalSeconds = totalSeconds(startTimeStr, endTimeStr); seekbar.setEnabled(true); seekbar.setMax(totalSeconds); seekbar.setProgress(0); moveStep = (float) (((float) screenWidth / (float) totalSeconds) * 0.8); } private class OnSeekBarChangeListenerImp implements SeekBar.OnSeekBarChangeListener { // 触发操作,拖动 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { text.layout((int) (progress * moveStep), 20, screenWidth, 80); text.setText(getCheckTimeBySeconds(progress, startTimeStr)); } // 表示进度条刚开始拖动,开始拖动时候触发的操作 public void onStartTrackingTouch(SeekBar seekBar) { } // 停止拖动时候 public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } } /** * 计算连个时间之间的秒数 */ private static int totalSeconds(String startTime, String endTime) { String[] st = startTime.split(":"); String[] et = endTime.split(":"); int st_h = Integer.valueOf(st[0]); int st_m = Integer.valueOf(st[1]); int st_s = Integer.valueOf(st[2]); int et_h = Integer.valueOf(et[0]); int et_m = Integer.valueOf(et[1]); int et_s = Integer.valueOf(et[2]); int totalSeconds = (et_h - st_h) * 3600 + (et_m - st_m) * 60 + (et_s - st_s); return totalSeconds; } /** * 根据当前选择的秒数还原时间点 * * @param args */ private static String getCheckTimeBySeconds(int progress, String startTime) { String return_h = "", return_m = "", return_s = ""; String[] st = startTime.split(":"); int st_h = Integer.valueOf(st[0]); int st_m = Integer.valueOf(st[1]); int st_s = Integer.valueOf(st[2]); int h = progress / 3600; int m = (progress % 3600) / 60; int s = progress % 60; if ((s + st_s) >= 60) { int tmpSecond = (s + st_s) % 60; m = m + 1; if (tmpSecond >= 10) { return_s = tmpSecond + ""; } else { return_s = "0" + (tmpSecond); } } else { if ((s + st_s) >= 10) { return_s = s + st_s + ""; } else { return_s = "0" + (s + st_s); } } if ((m + st_m) >= 60) { int tmpMin = (m + st_m) % 60; h = h + 1; if (tmpMin >= 10) { return_m = tmpMin + ""; } else { return_m = "0" + (tmpMin); } } else { if ((m + st_m) >= 10) { return_m = (m + st_m) + ""; } else { return_m = "0" + (m + st_m); } } if ((st_h + h) < 10) { return_h = "0" + (st_h + h); } else { return_h = st_h + h + ""; } return return_h + ":" + return_m + ":" + return_s; } }</span>3:接下来就是布局文件了
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/bg_whitef5" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <com.example.textmovebyseekbar.TextMoveLayout android:id="@+id/textLayout" android:layout_width="fill_parent" android:layout_height="40dp" /> <SeekBar android:id="@+id/seekbar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:focusable="true" android:maxHeight="4dp" android:minHeight="4dp" android:paddingLeft="5dp" android:paddingRight="5dp" android:progressDrawable="@drawable/po_seekbar" android:thumb="@drawable/seekbar_thumb" /> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/start_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="14dp" android:textColor="@color/bg_lin_95" /> <TextView android:id="@+id/end_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="14dp" android:textColor="@color/bg_lin_95" /> </RelativeLayout> </LinearLayout></span>4:android:progressDrawable=" @drawable/po_seekbar"这句会引用一个XML文件
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@*android:id/background"> <shape> <solid android:color="#c6c6c6" /> </shape> </item> <item android:id="@*android:id/secondaryProgress"> <clip> <shape> <solid android:color="#c6c6c6" /> </shape> </clip> </item> <item android:id="@*android:id/progress"> <clip> <shape> <solid android:color="#06a7fa" /> </shape> </clip> </item> </layer-list></span>5:android:thumb="@drawable/seekbar_thumb"也会引用一个XML文件 这其中又有用到两张
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/video_fast_search_nomal" android:state_focused="true" android:state_pressed="false"/> <item android:drawable="@drawable/video_fast_search_press" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/video_fast_search_press" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="@drawable/video_fast_search_nomal"/> </selector<code>></code></span>
![](http://static.oschina.net/uploads/space/2015/0209/161256_hVkr_945545.png)
![](http://static.oschina.net/uploads/space/2015/0209/161304_aM2F_945545.png)
色值:bg_whitef5:#F5F5F5
bg_lin_95:#959595
相关文章推荐
- Android使用SeekBar时动态显示进度且随SeekBar一起移动
- [Android开发]仿天天P图带气泡显示百分比进度的自定义SeekBar
- Android——SeekBar动态显示进度
- Android使用SeekBar时动态显示进度且随SeekBar一起移动
- Android 使用SeekBar时动态显示进度且随SeekBar一起移动
- Android使用SeekBar时动态显示进度且随SeekBar一起移动
- 【Android 开发教程】显示进度对话框
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android NumberProgressBar:动态移动显示百分比进度的进度条
- Android应用开发之(你必须知道的“文件上传时显示进度的解决方案”)
- Android多媒体开发 音乐播放(加带进度条、时间显示)以及使用SoundPool播放音效
- 进度条(SeekBar和RatingBar)—Mars Android开发视频之第一季第十八集(重)
- Android NumberProgressBar:动态移动显示百分比进度的进度条
- Android开发 自制圆形带进度显示的进度条
- Android开发之文件下载(通知栏中显示进度条,状态信息等)
- Android开发 下载显示进度
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android多媒体开发 音乐播放(加带进度条、时间显示)以及使用SoundPool播放音效
- Android开发之显示进度对话框
- Android官方开发文档Training系列课程中文版:通知用户之在通知中显示进度