[Android学习笔记]SeekBar的使用
一.SeekBar滑动条的使用
xml声明:
<SeekBar android:id="@+id/seekbar"
android:layout_width="200dip"
android:layout_height="wrap_content"/>
xml声明
主要事件监听:OnSeekBarChangeListener
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub
// 当Progress发生变化时触发
} @Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
// 用户手势开始
} @Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
// 用户手势结束 }
OnSeekBarChangeListener
二.自定义Seekbar样式:
使用selector自定义seekbar样式:
进度条样式:seekbar_progress.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/seekbar_background" /> <!-- 进度度 -->
<item android:id="@+android:id/progress"
android:drawable="@drawable/seekbar_foreground" />
</layer-list>
进度条样式:seekbar_progress.xml
按钮样式:seekbar_thumb.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 按下状态 -->
<item android:drawable="@drawable/seekbar_thumb"
android:state_pressed="true"/> <!-- 普通无焦点状态 -->
<item android:drawable="@drawable/seekbar_thumb"
android:state_focused="false"
android:state_pressed="false"/> </selector>
按钮样式:seekbar_thumb.xml
使用自定义样式,分别添加在android:progressDrawable和android:thumb两个属性上:
<SeekBar android:id="@+id/seekbar"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:progressDrawable="@drawable/seekbar_progress"
android:thumb="@drawable/seekbar_thumb" />
使用自定义样式
三.创建竖直方向的Seekbar:
原理:
扩展Seekbar,重写onDraw方法,把Canvas旋转90度
VerticalSeekbar:
public class VerticalSeekbar extends AbsSeekBar {
private Drawable mThumb;
private int height;
private int width; public interface OnSeekBarChangeListener {
void onProgressChanged(VerticalSeekbar VerticalSeekBar, int progress,
boolean fromUser); void onStartTrackingTouch(VerticalSeekbar VerticalSeekBar); void onStopTrackingTouch(VerticalSeekbar VerticalSeekBar);
} private OnSeekBarChangeListener mOnSeekBarChangeListener; public VerticalSeekbar(Context context) {
this(context, null);
} public VerticalSeekbar(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.seekBarStyle);
} public VerticalSeekbar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
mOnSeekBarChangeListener = l;
} void onStartTrackingTouch() {
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onStartTrackingTouch(this);
}
} void onStopTrackingTouch() {
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onStopTrackingTouch(this);
}
} void onProgressRefresh(float scale, boolean fromUser) { Drawable thumb = mThumb;
if (thumb != null) {
setThumbPos(getHeight(), thumb, scale, Integer.MIN_VALUE);
invalidate();
}
if (mOnSeekBarChangeListener != null) {
mOnSeekBarChangeListener.onProgressChanged(this, getProgress(),
fromUser);
}
} private void setThumbPos(int w, Drawable thumb, float scale, int gap) { int available = w + getPaddingLeft() - getPaddingRight();
int thumbWidth = thumb.getIntrinsicWidth();
int thumbHeight = thumb.getIntrinsicHeight();
available -= thumbWidth;
// The extra space for the thumb to move on the track
available += getThumbOffset() * 2;
int thumbPos = (int) (scale * available);
int topBound, bottomBound;
if (gap == Integer.MIN_VALUE) {
Rect oldBounds = thumb.getBounds();
topBound = oldBounds.top;
bottomBound = oldBounds.bottom;
} else {
topBound = gap;
bottomBound = gap + thumbHeight;
}
thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);
} protected void onDraw(Canvas c) { //这里控制的是seekbar旋转的方向,用隐去的这两行配合下边注释掉的trackTouchEvent,可以实现
//初始位置在上边的seekbar,现在默认的是初始位置在最下边,及0在下边
/*c.rotate(90);
c.translate(0, -width);*/ //更改方向
c.rotate(-90);
c.translate(-this.getHeight(), 0); super.onDraw(c);
} private void trackTouchEvent(MotionEvent event) { final int height = getHeight(); int y = (int)event.getY(); float scale = 0; float progress = 0; if(y > height) {
scale = 0.0f;
} else if( y>0 ) { scale = 1-(float)y / (float)height; } else { scale= 1.0f; } final int max = getMax(); progress = scale * max; setProgress(Math.round(progress)); setThumbPos(getHeight(), mThumb, scale, 0) ; } protected synchronized void onMeasure(int widthMeasureSpec,
int heightMeasureSpec) {
height = View.MeasureSpec.getSize(heightMeasureSpec);
width = View.MeasureSpec.getSize(widthMeasureSpec);
this.setMeasuredDimension(width, height); } @Override
public void setThumb(Drawable thumb) {
mThumb = thumb;
super.setThumb(thumb);
} protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(h, w, oldw, oldh);
} public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
setPressed(true);
onStartTrackingTouch();
trackTouchEvent(event);
break; case MotionEvent.ACTION_MOVE:
trackTouchEvent(event);
attemptClaimDrag();
break; case MotionEvent.ACTION_UP:
trackTouchEvent(event);
onStopTrackingTouch();
setPressed(false);
break; case MotionEvent.ACTION_CANCEL:
onStopTrackingTouch();
setPressed(false);
break;
}
return true;
} /* private void trackTouchEvent(MotionEvent event) {
final int Height = getHeight();
final int available = Height - getPaddingBottom() - getPaddingTop();
int Y = (int) event.getY();
Log.d("6", "trackTouchEvent==>Y"+Y);
float scale;
float progress = 0;
if (Y > Height - getPaddingBottom()) {
scale = 1.0f;
} else if (Y < getPaddingTop()) {
scale = 0.0f;
} else {
scale = (float) (Y)
/ (float) available;
} final int max = getMax();
progress = scale * max; setProgress((int) progress);
}*/ private void attemptClaimDrag() {
if (getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(true);
}
} public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
KeyEvent newEvent = null;
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_UP:
newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DPAD_RIGHT);
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DPAD_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DPAD_DOWN);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_DPAD_UP);
break;
default:
newEvent = new KeyEvent(KeyEvent.ACTION_DOWN, event
.getKeyCode());
break;
}
return newEvent.dispatch(this);
}
return false;
}
}
VerticalSeekbar
使用VerticalSeekbar:
<com.haidilao.views.VerticalSeekbar
android:id="@+id/seekbar"
android:layout_width="25dip"
android:layout_height="130dip"
android:paddingTop="1dip"
android:layout_marginBottom="12dip"
android:progressDrawable="@drawable/verticalseekbar_progressdrawable"
android:thumb="@drawable/verticalseekbar_thumb"
android:thumbOffset="0dip"
android:max="100" />
VerticalSeekbar
[Android学习笔记]SeekBar的使用的更多相关文章
- 【转】Pro Android学习笔记(二三):用户界面和控制(11):其他控件
目录(?)[-] Chronometer计时器控件 倒计时CountDownTimer Switch控件 Space控件 其他控件 Android提供了很多控件,基本上都是view的扩展. Chron ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- udacity android 学习笔记: lesson 4 part b
udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
- Android学习笔记之Activity详解
1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...
- Pro Android学习笔记 ActionBar(1):Home图标区
Pro Android学习笔记(四八):ActionBar(1):Home图标区 2013年03月10日 ⁄ 综合 ⁄ 共 3256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 ActionBar在A ...
随机推荐
- cocos2d-x环境的搭建之xcode-本人亲历成功搭建!
cocos2d-x环境的搭建之xcode-本人亲历成功搭建! 写给大家的前言,在学习cocos2d-x的时候自己走了很多的弯路,也遇到了很多很多问题,不管是简单的还是困难的现在都慢慢的一步一步克服了, ...
- boost uuid 学习笔记
#include <vector>#include <iostream>#include <boost/uuid/uuid.hpp>#include <boo ...
- Emmet插件
p{font-size: 18px; color: #666;} body{background-color:#F3F3F3} .code{color:#3974C3;font-size: 14px; ...
- HTML常用标签属性图
- asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师
创建用户: MembershipCreateStatus mc; Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQ ...
- EDStarRating(IOS星级评定)
链接地址:https://github.com/erndev/EDStarRating EDStarRating
- OCP-1Z0-042-V12.39-47题
47.Which two database operations can be performed at the mount stage of database startup? 题目解析: A和E在 ...
- [cocos2d-x]HelloWorldDemo
实现一个demo,具备以下功能: 1.让几个字分别位于中间和四个角落. 2.中间的字体改变,并且带有闪烁功能. 3.单点触摸和多点触摸,并且能够实现滑动效果,滑动的话必须使用带有bool返回值的单点触 ...
- 程序实现LayoutAnimationController
在res/anim下新建anim_set.xml: <?xml version="1.0" encoding="utf-8"?> <set x ...
- JQuery Ajax实例总结
jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写JavaScript代码的习惯. 废话少说,直接进入正题,我们先来看一些简单的方法,这些方法都是对 ...