seekbar(进度条)
来源:互联网 发布:php开源p2p系统源码 编辑:程序博客网 时间:2024/05/15 21:49
方法
- setProgress(int value) 设置滑块的位置
- setMax(int value) 设置进度条的最大长度
- setOnSeekBarChangeListener(OnSeekBarCHangeListener l) 设置SeekBar的进度改变事件
属性
- android:secondaryProgress[integer] 设置第二进度,通常用做显示视频等得缓冲效果
- android:thumb[drawable]设置滑块的图样
- android:progressDrawable[drawable]设置进度条的图样
- android:thumbOffset="0dip" 即可实现thumb滑块不被覆盖
事件监听
通过设置事件监听器setOnSeekBarChangeListener来获取SeekBar的当前状态,通常要监听以下3个事件
1.public voidonProgressChanged(SeekBar seekBae,int progress,boolean fromUser); 用于监听SeekBar进度值的改变
2.public voidonStartTrackingTouch(SeekBar seekBar);用于监听SeekBar开始拖动
3.public voidonStopTrackingTouch(SeekBar seekBar);用于监听SeekBar停止拖动
自定义图片的SeekBar代码如下:
第一个drawable下xml资源代码:设置进度条的属性
<?xmlversion="1.0" encoding="utf-8"?>
<layer-listxmlns:Android="http://schemas.android.com/apk/res/android" >
<!--滑竿背景-->
<item
android:id="@android:id/background"
android:drawable="@drawable/total" />
<!--第二进度条式样 -->
<item
android:id="@android:id/secondaryProgress"
android:drawable="@drawable/part2"/>
<!--进度条样式 -->
<item
android:id="@android:id/progress"
android:drawable="@drawable/part" />
</layer-list>
第二个drawable下xml资源代码:设置滑块的属性
<?xmlversion="1.0" encoding="utf-8"?>
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
<!--滑块未压下时 -->
<item
android:state_pressed="false"
android:drawable="@drawable/a" />
<!--滑块压下时 -->
<item
android:state_pressed="true"
android:drawable="@drawable/ic_launcher" />
</selector>
SeekBar布局代码:
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:minHeight="5dip"
android:maxHeight="5dip"
android:secondaryProgress="50"
android:progressDrawable="@drawable/seek_img"
android:thumb="@drawable/thumb" />
1 SeekBar简介
SeekBar是进度条。我们使用进度条时,可以使用系统默认的进度条;也可以自定义进度条的图片和滑块图片等。
2 SeekBar示例
创建一个activity,包含2个SeekBar。
第1个SeekBar是系统默认的SeekBar。
第2个SeekBar是自定义SeekBar,使用自定义的背景图和滑块图片。
应用层代码
package com.skywang.control;import android.os.Bundle;import android.app.Activity;import android.util.Log;import android.widget.TextView;import android.widget.SeekBar;import android.widget.SeekBar.OnSeekBarChangeListener;public class SeekBarTest extends Activity implements SeekBar.OnSeekBarChangeListener{ private static final String TAG = "SKYWANG"; // 与“系统默认SeekBar”对应的TextView private TextView mTvDef; // 与“自定义SeekBar”对应的TextView private TextView mTvSelf; // “系统默认SeekBar” private SeekBar mSeekBarDef; // “自定义SeekBar” private SeekBar mSeekBarSelf; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.seek_bar_test); // 与“系统默认SeekBar”对应的TextView mTvDef = (TextView) findViewById(R.id.tv_def); // “系统默认SeekBar” mSeekBarDef = (SeekBar) findViewById(R.id.seekbar_def); mSeekBarDef.setOnSeekBarChangeListener(this); // 与“自定义SeekBar”对应的TextView mTvSelf = (TextView) findViewById(R.id.tv_self); // “自定义SeekBar” mSeekBarSelf = (SeekBar) findViewById(R.id.seekbar_self); mSeekBarSelf.setOnSeekBarChangeListener(this); } /* * SeekBar停止滚动的回调函数 */ @Override public void onStopTrackingTouch(SeekBar seekBar) { } /* * SeekBar开始滚动的回调函数 */ @Override public void onStartTrackingTouch(SeekBar seekBar) { } /* * SeekBar滚动时的回调函数 */ @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Log.d(TAG, "seekid:"+seekBar.getId()+", progess"+progress); switch(seekBar.getId()) { case R.id.seekbar_def:{ // 设置“与系统默认SeekBar对应的TextView”的值 mTvDef.setText(getResources().getString(R.string.text_def)+" : "+String.valueOf(seekBar.getProgress())); break; } case R.id.seekbar_self: { // 设置“与自定义SeekBar对应的TextView”的值 mTvSelf.setText(getResources().getString(R.string.text_self)+" : "+String.valueOf(seekBar.getProgress())); break; } default: break; } }}
代码说明:
要监听SeekBar的滑动消息,通过实现“SeekBar.OnSeekBarChangeListener”接口。这个接口中包含3个方法onStartTrackingTouch()、onStopTrackingTouch()和onProgressChanged()。
layout文件
<LinearLayout 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:orientation="vertical" > <TextView android:id="@+id/tv_def" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_def" /> <!-- max=100,代表它的取值范围是0-100,共101个值; progress=10,代表默认值是10 --> <SeekBar android:id="@+id/seekbar_def" android:layout_width="620px" android:layout_height="wrap_content" android:max="100" android:progress="10" /> <TextView android:id="@+id/tv_self" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text_self" /> <!-- max=100,代表它的取值范围是0-100,共101个值; progress=20,代表默认值是20 progressDrawable,表示SeekBar的背景图片 thumbe,表示SeekBar的滑块图片 --> <SeekBar android:id="@+id/seekbar_self" android:layout_width="620px" android:layout_height="wrap_content" android:max="100" android:progress="20" android:progressDrawable="@drawable/bg_bar" android:thumb="@drawable/thumb_bar" /> </LinearLayout>
自定义SeekBar的背景定义为:android:progressDrawable="@drawable/bg_bar"。
它调用的bg_bar.xml的内容如下:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 背景图 --> <item android:id="@+android:id/background" android:drawable="@drawable/bar_dn" /> <!-- 第二进度图 --> <item android:id="@+android:id/SecondaryProgress" android:drawable="@drawable/bar_dn" /> <!-- 进度度 --> <item android:id="@+android:id/progress" android:drawable="@drawable/bar_up" /></layer-list>
bar_dn.png如下图:
bar_up.png如下图:
自定义SeekBar的滑块定义为:android:thumb="@drawable/thumb_bar"。
它调用的thumb_bar.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 --> <item android:state_pressed="true" android:drawable="@drawable/thumb_dn" /> <!-- 焦点状态 --> <item android:state_focused="true" android:drawable="@drawable/thumb_up" /> <!-- 默认状态 --> <item android:drawable="@drawable/thumb_up" /> </selector>
thumb_up.png如下图:
thumb_dn.png如下图:
manifest文件
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.skywang.control" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.skywang.control.SeekBarTest" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
点击下载:源代码
运行效果:如图
- 进度条SeekBar
- 进度条seekbar
- 进度条SeekBar
- seekbar(进度条)
- SeekBar 可拖拽进度条
- android 自定义进度条 seekbar
- android 自定义进度条 seekbar
- Android-SeekBar(拖动进度条)
- Android--进度条--ProgressBar--SeekBar
- 【进度控制】SeekBar进度条
- android 进度条seekbar样式自定义
- android 进度条seekbar样式自定义
- Android中SeekBar进度条操作
- 手动滑动的进度条 SeekBar
- Android复习之SeekBar进度条
- SeekBar(手拉进度条)应用
- 闹钟音量调节 进度条 SeekBar
- Progressbar、SeekBar进度条,滚动条
- POJ2411(状压Dp,插头Dp,数学公式)
- Problem A: 字符串类(I)
- Maven学习总结(36)——Apache Maven 3.5.0抢鲜看
- 频繁通过win32api的createfile函数打开文件句柄导致内存泄漏
- android 跳转Facebook和Instagram
- seekbar(进度条)
- Problem B: 字符串类(II)
- HTTP请求中GET与POST的区别
- soj 4559 Easy Problem
- Linux进程间通信——使用信号量
- Linux下" >/dev/null 2>&1 "相关知识说明
- 正弦与余弦曲线的波形图显示(数组与簇)
- SSD(Single Shot MultiBox Detector):create_list.sh io.cpp:187 Could not open or find file
- js判断类型 是否是数组Array