Android SeekBar使用避坑指南

SeekBar简介

SeekBar是Android原生UI组件,可以用来调节进度,广泛应用于音乐、视频进度展示调控、音量、亮度调节等功能里。

SeekBar的使用很简单,这里就不再介绍了,本文着重介绍一下作者最近在使用SeekBar遇到的几个坑,希望大家以后可以避免。

问题1.纵向进度条

在这里插入图片描述
如图,如何去实现这样一个可拖动进度条呢?
期初我以为就是一个普通的SeekBar,旋转一下就好了,结果弄了半天发现并不是这样的,旋转后的宽高完全乱掉了;官方是没有这种样式的,想要实现当然可以是自定义View,但我嫌麻烦,最后通过放两张ImageView叠加在SeekBar上来展现样式的;

问题2.SeekBar间隙

在这里插入图片描述
这次是一个横向的进度条了,在我设置了左侧和某个view对齐之后,发现它并没有对齐,距离左边总是有一个距离;
原来是SeekBar默认有padding,把paddingLeft=0dp 就可以对齐了。

问题3.SeekBar高度设置不生效

同样是上面横向的进度条,按照UI给的标注设置了height之后发现和效果图有差距,经过多次调整,发现设置的高度完全不生效(即使设置了一个超大值,它也没有变大);
原来SeekBar的视觉高度需要通过设置progressDrawable里的高度来实现。
如下所示:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:height="66dp"
        android:id="@android:id/background">
        <shape android:shape="rectangle">
            <corners android:radius="16dp"/>
            <solid android:color="#1AFFFFFF"/>
        </shape>
    </item>

    <item android:id="@android:id/progress"
        android:height="66dp" android:drawable="@drawable/progressbar_progress_horizontal"/>
</layer-list>

问题4.切换页面后不展示进度

这个问题很奇怪,第一次加载的时候界面显示正常,但切换到其他视图之后再切换回来,seekbar的进度就没了。
解决办法:在设置progress之前,先设置progress=0,然后再设置具体的progress数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1 Getting started........................................................................................................................7 1.1 Installing Basic4Android and Android SDK ....................................................................7 1.1.1 Install and configure Basic4Android ........................................................................9 1.2 Installing Android Emulator........................................................................................... 10 2 My first program (MyFirstProgram.b4a)............................................................................... 15 3 Second program (SecondProgram.b4a) ................................................................................. 33 4 The IDE ............................................................................................................................... 46 4.1 Menu and Toolbar.......................................................................................................... 47 4.1.1 Toolbar .................................................................................................................. 47 4.1.2 File menu ............................................................................................................... 48 4.1.3 Edit menu............................................................................................................... 48 4.1.4 Project menu .......................................................................................................... 49 4.1.5 Tools menu ............................................................................................................ 50 4.2 Code area....................................................................................................................... 52 4.2.1 Undo – Redo ............................................................................................. 52 4.2.2 Collapse a subroutine ............................................................................................. 52 4.2.3 Collapse a Region .................................................................................................. 53 4.2.4 Collapse the whole code......................................................................................... 54 4.2.5 Commenting and uncommenting code ...................................................... 55 4.2.6 Bookmarks .................................................................................. 55 4.2.7 Indentation ............................................................................................... 56 4.2.8 Autocomplete ................................................................................................. 58 4.2.9 Jump to a subroutine .............................................................................................. 60 4.2.10 Highlighting occurences of words .......................................................................... 61 4.2.11 Debugging ............................................................................................................. 62 4.2.12 Breakpoints............................................................................................................ 64 4.3 Tabs.............................................................................................................................. 67 4.3.1 Module and subroutine lists ............................................................... 67 4.3.2 Files ....................................................................................................... 68 4.3.3 Logs ...................................................................................................... 69 4.3.4 Libs ........................................................................................................ 70 5 Screen sizes and resolutions................................................................................................... 71 5.1 Specilal functions like 50%x, 50dip ............................................................................. 76 5.1.1 PerXToCurrent, PerYToCurrent - 50%x................................................................. 76 5.1.2 DipToCurrent - 50dip............................................................................................. 76 6 The Emulator........................................................................................................................ 77 6.1 Launch an Emulator....................................................................................................... 77 6.2 Generate a new Emulator............................................................................................... 79 6.3 Emulator problems......................................................................................................... 81 6.3.1 Process timeout ...................................................................................................... 82 7 B4A Bridge ........................................................................................................................... 83 8 The Designer ......................................................................................................................... 86 8.1 The menu....................................................................................................................... 87 8.1.1 File menu ............................................................................................................... 87 8.1.2 AddView menu ...................................................................................................... 87 8.1.3 The Tools menu ..................................................................................................... 88 8.2 Tools ............................................................................................................................. 89 8.2.1 Generate Members ................................................................................................. 89 8.2.2 BringTo Front ........................................................................................................ 90 Table of contents 3 Basic4Android Beginner's Guide 8.2.3 Duplicate Selected View ........................................................................................ 90 8.2.4 Remove Selected View .......................................................................................... 91 8.2.5 Change grid............................................................................................................ 91 8.2.6 Connect device or emulator.................................................................................... 92 8.3 General settings ............................................................................................................. 92 8.4 Image files ..................................................................................................................... 93 8.5 Properties list ................................................................................................................. 94 8.6 Layout variants .............................................................................................................. 96 8.7 The Abstract Designer ................................................................................................. 101 8.7.1 The menu ............................................................................................................. 102 8.7.2 Example............................................................................................................... 103 8.8 Adding views by code.................................................................................................. 106 9 Process and Activity life cycle............................................................................................. 109 10 Variables and objects....................................................................................................... 113 10.1 Variable Types............................................................................................................. 113 10.2 Names of variables ...................................................................................................... 114 10.3 Declaring variables ...................................................................................................... 114 10.3.1 Simple variables................................................................................................... 114 10.3.2 Array variables..................................................................................................... 115 10.3.3 Array of views (objects) ....................................................................................... 117 10.3.4 Type variables...................................................................................................... 118 10.4 Casting ........................................................................................................................ 119 10.5 Scope.......................................................................................................................... 120 10.5.1 Process variables.................................................................................................. 120 10.5.2 Activity variables ................................................................................................. 120 10.5.3 Local variables..................................................................................................... 120 10.6 Tips ............................................................................................................................. 121 11 Modules.......................................................................................................................... 122 11.1 Activity modules.......................................................................................................... 123 11.2 Code modules .............................................................................................................. 124 11.3 Service modules........................................................................................................... 125 12 Example programs........................................................................................................... 126 12.1 User interfaces ............................................................................................................. 126 12.1.1 Menu example (UserInterfaceMenu.b4a)............................................................. 127 12.1.2 TabHost example (UserInterfaceTabHost.b4a) ................................................... 128 12.1.3 Button toolbox example (UserInterfaceButtonToolbox.b4a)................................ 129 12.2 Program with 3 Activities (ThreeActivityExample.b4a) .............................................. 130 12.3 ScrollView examples ................................................................................................... 137 12.3.1 ScrollView example program............................................................................... 138 13 SQLite Database.............................................................................................................. 149 13.1 SQLite Database basics................................................................................................ 149 13.2 SQLite Database example program.............................................................................. 152 13.2.1 Editing ................................................................................................................. 154 13.2.2 Filtering ............................................................................................................... 155 13.2.3 Beginning of the code .......................................................................................... 156 13.2.4 Read the table ...................................................................................................... 159 13.2.5 Modify a data set.................................................................................................. 161 13.2.6 Add a data set to the database............................................................................... 162 13.2.7 Delete a data set from the database....................................................................... 164 13.2.8 Positioning of the EditText Views in the Edit panel.............................................. 165 14 GPS................................................................................................................................ 166 14.1 GPS Library................................................................................................................. 166 14.1.1 GPS Object .......................................................................................................... 166 Table of contents 4 Basic4Android Beginner's Guide 14.1.2 GPS Satellite........................................................................................................ 167 14.1.3 GPS Location....................................................................................................... 167 14.2 GPS Program............................................................................................................... 168 14.2.1 General explanations............................................................................................ 171 14.2.2 Setup.................................................................................................................... 172 14.2.3 GPS display ......................................................................................................... 173 14.2.4 Satellites .............................................................................................................. 174 14.2.5 Map display ......................................................................................................... 175 14.2.6 GPS path.............................................................................................................. 177 14.3 GPS Program Code...................................................................................................... 181 14.3.1 Initialization of the GPS....................................................................................... 182 14.3.2 Button with tooltip ............................................................................................... 183 14.3.3 Button with tooltip and additional buttons ............................................................ 186 14.3.4 GPS Calculate distance scales .............................................................................. 189 14.3.5 Drawing GPS position.......................................................................................... 190 15 Basic language................................................................................................................. 193 15.1 Program flow............................................................................................................... 193 15.1.1 Process_Globals routine ....................................................................................... 193 15.1.2 Globals routine..................................................................................................... 194 15.1.3 Activity_Create (FirstTime As Boolean) routine .................................................. 194 15.1.4 Activity_Resume routine...................................................................................... 194 15.1.5 Activity_Pause (UserClosed As Boolean) routine................................................. 194 15.2 Expressions.................................................................................................................. 196 15.2.1 Mathematical expressions .................................................................................... 196 15.2.2 Relational expressions.......................................................................................... 197 15.2.3 Boolean expressions............................................................................................. 197 15.3 Conditional statements................................................................................................. 197 15.3.1 If – Then – End If................................................................................................. 198 15.3.2 Select – Case........................................................................................................ 199 15.4 Loop structures ............................................................................................................ 201 15.4.1 For – Next............................................................................................................ 201 15.4.2 Do - Loop ............................................................................................................ 202 15.5 Subs............................................................................................................................ 203 15.5.1 Declaring ............................................................................................................. 203 15.5.2 Calling a Sub........................................................................................................ 203 15.5.3 Naming ................................................................................................................ 203 15.5.4 Parameters ........................................................................................................... 204 15.5.5 Returned value ..................................................................................................... 204 15.6 Events......................................................................................................................... 205 15.7 Libraries ...................................................................................................................... 208 15.7.1 Standard libraries ................................................................................................. 208 15.7.2 Additional libraries folder .................................................................................... 208 15.7.3 Error message "Are you missing a library reference?" .......................................... 209 15.8 String manipulation ..................................................................................................... 210 15.9 Timers ......................................................................................................................... 211 15.10 Files......................................................................................................................... 212 15.10.1 File object ........................................................................................................ 212 15.10.2 TextWriter........................................................................................................ 214 15.10.3 TextReader....................................................................................................... 215 15.10.4 Text encoding................................................................................................... 216 16 Graphics / Drawing.......................................................................................................... 218 16.1 Overview..................................................................................................................... 218 16.2 Drawing test programs................................................................................................. 220 Table of contents 5 Basic4Android Beginner's Guide 16.2.1 Drawing rotating bitmaps / RotatingNeedle.......................................................... 220 16.2.2 Simple draw functions.......................................................................................... 225 17 Widgets, home screen widgets ......................................................................................... 234 17.1 Widgets Part I............................................................................................................. 234 17.2 Widgets Part II............................................................................................................ 237 18 B4A Keywords................................................................................................................ 240 18.1 Bit ............................................................................................................................... 240 18.2 DateTime..................................................................................................................... 241 18.3 Exception..................................................................................................................... 244 18.4 Keywords .................................................................................................................... 245 18.5 LayoutValues............................................................................................................... 257 18.6 String.......................................................................................................................... 258 18.7 StringBuilder ............................................................................................................... 260 18.8 Timer.......................................................................................................................... 262 19 Views .............................................................................................................................. 264 19.1 Activity....................................................................................................................... 264 19.2 Button......................................................................................................................... 268 19.3 CheckBox.................................................................................................................... 271 19.4 EditText....................................................................................................................... 274 19.5 ImageView .................................................................................................................. 279 19.6 Label ........................................................................................................................... 282 19.7 ListView...................................................................................................................... 285 19.8 Panel........................................................................................................................... 290 19.9 ProgressBar ................................................................................................................. 293 19.10 RadioButton............................................................................................................. 295 19.11 ScrollView............................................................................................................... 298 19.12 SeekBar ................................................................................................................... 301 19.13 Spinner .................................................................................................................... 304 19.14 TabHost ................................................................................................................... 308 19.15 ToggleButton........................................................................................................... 312 19.16 View........................................................................................................................ 315 19.17 WebView................................................................................................................. 317 20 VB6 versus B4A.............................................................................................................. 321 21 FAQ ............................................................................................................................... 326 21.1 "Please save project first" message............................................................................. 326 21.2 "Are you missing a library reference" message........................................................... 326 21.3 How loading / updating a library.................................................................................. 327 21.4 When do we need to 'Initialize' and when not............................................................. 327 21.5 Split a long line into two or more lines......................................................................... 328 21.6 Avoid closing an application........................................................................................ 328 21.7 Unwanted events like Click, Touch or others ............................................................... 329 21.8 Adding a Menu item.................................................................................................... 329 21.9 How do I remove a View with the Designer................................................................. 330 21.10 "Process has timeout" message............................................................................... 330 21.11 Getting a picture from the gallery............................................................................. 331 21.12 How to delete x.bal files or other files from a project ............................................. 331 21.13 Block a screen orientation ........................................................................................ 332 21.14 Close second Activity .............................................................................................. 333 21.15 Taking a screenshot programaticaly ......................................................................... 333 21.16 After compiling, where are the files ......................................................................... 333 21.17 Run an application from another one........................................................................ 334 21.18 How to pass an Array to a Sub ................................................................................. 334 21.19 Getting language and country from device ............................................................... 335 Table of contents 6 Basic4Android Beginner's Guide 22 Glossary .......................................................................................................................... 336
要在 Android SeekBar 中实时更新视频进度,你需要在代码中实现以下步骤: 1. 初始化 SeekBar 和 MediaPlayer。 2. 在 SeekBar 上设置 OnSeekBarChangeListener,以监听拖动事件。 3. 在拖动事件中,更新 MediaPlayer 的当前位置。 4. 在 MediaPlayer 中设置 OnPreparedListener,以获取视频的总长度。 5. 使用 Handler 定期更新 SeekBar 的进度,以反映当前视频的进度。 下面是一些示例代码,用于演示如何在 Android SeekBar 中实时更新视频进度: ``` // 初始化 SeekBar 和 MediaPlayer SeekBar seekBar = findViewById(R.id.seek_bar); MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.video); // 监听 SeekBar 的拖动事件 seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { // 如果是用户拖动 SeekBar,更新 MediaPlayer 的当前位置 mediaPlayer.seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) {} @Override public void onStopTrackingTouch(SeekBar seekBar) {} }); // 获取视频的总长度 mediaPlayer.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { int duration = mediaPlayer.getDuration(); seekBar.setMax(duration); } }); // 定期更新 SeekBar 的进度 final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { int currentPosition = mediaPlayer.getCurrentPosition(); seekBar.setProgress(currentPosition); handler.postDelayed(this, 1000); } }; handler.postDelayed(runnable, 1000); ``` 上面的代码中,我们使用了 Handler 定期更新 SeekBar 的进度,间隔为 1 秒钟。在实际应用中,你可以根据需要调整更新的频率。另外,由于视频播放需要消耗大量的资源,建议在退出页面时及时释放 MediaPlayer。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值