向下拉的竖直的seekBar拖动条解释原理.docx
- 文档编号:9428561
- 上传时间:2023-05-19
- 格式:DOCX
- 页数:25
- 大小:115.54KB
向下拉的竖直的seekBar拖动条解释原理.docx
《向下拉的竖直的seekBar拖动条解释原理.docx》由会员分享,可在线阅读,更多相关《向下拉的竖直的seekBar拖动条解释原理.docx(25页珍藏版)》请在冰点文库上搜索。
向下拉的竖直的seekBar拖动条解释原理
在android里做一个竖着的seekbar
原理:
1.
2.
源码下载:
要做android的界面,基础的要求是熟悉那些内置的组件(prebuiltcomponments),而有时候这些组件是不够用的,这时候我们就需要自己写一个新的,或者修改内置的。
这就要求我们了解更多知识,比如各个View是怎么画出来的,UI上各种事件是如何分发和处理的等等。
如何做一个verticalseekbar,竖起来的seekbar。
实现它的过程中就会涉及一些View是如何被画出来的知识。
通过本文你可以实现一个verticalseekbar,你也可以学到一些使用seekbar的知识。
需要自定义的组件时,从framework中抠出来相关(功能相似、在继承关系的hierarchy中地位相当)的代码(类),在自己的应用程序中做修改,实现自己想要的功能。
需要的style、drawable等资源,有些是在应用程序里用不了的,就也自己做一份。
我们公司做整个系统,从硬件到android到android上的应用系统,framework我们也修改,主要是功能上的增减。
在framework里增加一个verticalseekbar我们也能做到,但是这样兼容性就被破坏了,所以这位仁兄把需要的拿出来在自己的程序中实现。
我做verticalseekbar也采取这个策略,不和大家啰嗦实现过程中太多因果逻辑,我想很多读者读这篇文章就是为了做出来一个verticalseekbar。
为了方便手头没有代码和开发环境的读者,我会贴上framework中的相关代码。
verticalseekbar当然要实现seekbar一样的功能,所以首先seekbar有的verticalseekbar都要有,直接在你的工程中新建一个类verticalseekbar,从framework中把seekbar的代码复制过来,做一些必要的修改。
我们来看看seekbar做了些什么.
seekbar代码:
1./ *
2.*Copyright (C) 2006 The Android Open Source Project
3. * Licensed under the Apache License, Version 2.0 (the "License");
4. * you may not use this file except in compliance with the License.
5. * You may obtain a copy of the License at
6. * http:
//www.apache.org/licenses/LICENSE-2.0
7. * Unless required by applicable law or agreed to in writing, software
8. * distributed under the License is distributed on an "AS IS" BASIS,
9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10. * See the License for the specific language governing permissions and
11. * limitations under the License.
12. */
13.package android.widget;
14.import android.content.Context;
15.import android.util.AttributeSet;
16./**
17. * A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch
18. * the thumb and drag left or right to set the current progress level or use the arrow keys.
19. * Placing focusable widgets to the left or right of a SeekBar is discouraged.
20. *
21. * Clients of the SeekBar can attach a {@link SeekBar.OnSeekBarChangeListener} to
22. * be notified of the user's actions.
23. *
24. * @attr ref android.R.styleable#SeekBar_thumb
25. */
26.public class SeekBar extends AbsSeekBar {
27. /**
28. * A callback that notifies clients when the progress level has been
29. * changed. This includes changes that were initiated by the user through a
30. * touch gesture or arrow key/trackball as well as changes that were initiated
31. * programmatically.
32. */
33. public interface OnSeekBarChangeListener {
34.
35. /**
36. * Notification that the progress level has changed. Clients can use the fromUser parameter
37. * to distinguish user-initiated changes from those that occurred programmatically.
38. *
39. * @param seekBar The SeekBar whose progress has changed
40. * @param progress The current progress level. This will be in the range 0..max where max
41. * was set by {@link ProgressBar#setMax(int)}. (The default value for max is 100.)
42. * @param fromUser True if the progress change was initiated by the user.
43. */
44. void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser);
45.
46. /**
47. * Notification that the user has started a touch gesture. Clients may want to use this
48. * to disable advancing the seekbar.
49. * @param seekBar The SeekBar in which the touch gesture began
50. */
51. void onStartTrackingTouch(SeekBar seekBar);
52.
53. /**
54. * Notification that the user has finished a touch gesture. Clients may want to use this
55. * to re-enable advancing the seekbar.
56. * @param seekBar The SeekBar in which the touch gesture began
57. */
58. void onStopTrackingTouch(SeekBar seekBar);
59. }
60. private OnSeekBarChangeListener mOnSeekBarChangeListener;
61.
62. public SeekBar(Context context) {
63. this(context, null);
64. }
65.
66. public SeekBar(Context context, AttributeSet attrs) {
67. this(context, attrs, com.android.internal.R.attr.seekBarStyle); //这样会出错,写成android.R.attr.seekBarStyle可以
68. }
69. public SeekBar(Context context, AttributeSet attrs, int defStyle) {
70. super(context, attrs, defStyle);
71. }
72. @Override //不能这样写,出错,将其注释就可以
73. void onProgressRefresh(float scale, boolean fromUser) {
74. super.onProgressRefresh(scale, fromUser); //报AbsSeekBar这个父类没有这个方法,将其注释就可以
75. if (mOnSeekBarChangeListener !
= null) {
76. mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), fromUser);
77. }
78. }
79. /**
80. * Sets a listener to receive notifications of changes to the SeekBar's progress level. Also
81. * provides notifications of when the user starts and stops a touch gesture within the SeekBar.
82. *
83. * @param l The seek bar notification listener
84. *
85. * @see SeekBar.OnSeekBarChangeListener
86. */
87. public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
88. mOnSeekBarChangeListener = l;
89. }
90.
91.@Override //不能这样写,出错,将其注释就可以
92. void onStartTrackingTouch() {
93. if (mOnSeekBarChangeListener !
= null) {
94. mOnSeekBarChangeListener.onStartTrackingTouch(this);
95. }
96. }
97.@Override //不能这样写,出错,将其注释就可以
98. void onStopTrackingTouch() {
99. if (mOnSeekBarChangeListener !
= null) {
100. mOnSeekBarChangeListener.onStopTrackingTouch(this);
101. }
102. }
103.}
A:
定义了一个接口OnSeekBarChangeListener,接口中定义了三个方法:
voidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser);
voidonStartTrackingTouch(SeekBarseekBar);
voidonStopTrackingTouch(SeekBarseekBar);
seekbar中声明了一个该接口的对象mOnSeekBarChangeListener,方法setOnSeekBarChangeListener()将给它赋值。
在你的程序中定义一个对象,实现OnSeekBarChangeListener,并调用setOnSeekBarChangeListener()把这个对象传给mOnSeekBarChangeListener。
B:
在seekbar中还有几个override的方法:
voidonProgressRefresh(floatscale,booleanfromUser);
voidonStartTrackingTouch();
voidonStopTrackingTouch();
这些方法会在发生了相应事件的时候被调用,而它们又相应地去调用mOnSeekBarChangeListener中的方法,于是你的程序的定义的响应各种事件的方法就被执行了。
但是这些还不够,我们要让seekbar竖起来,要让thumb上下滑动,实现这些特性的代码在什么地方呢?
我们顺着seekbar的继承关系向上找。
来看看absseekbar。
absseekbar的代码:
1./ * Copyright (C) 2007 The Android Open Source Project
2. *
3. * Licensed under the Apache License, Version 2.0 (the "License");
4. * you may not use this file except in compliance with the License.
5. * You may obtain a copy of the License at
6. *
7. * http:
//www.apache.org/licenses/LICENSE-2.0
8. *
9. * Unless required by applicable law or agreed to in writing, software
10. * distributed under the License is distributed on an "AS IS" BASIS,
11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12. * See the License for the specific language governing permissions and
13. * limitations under the License.
14. */
15.package android.widget;
16.import android.content.Context;
17.import android.content.res.TypedArray;
18.import android.graphics.Canvas;
19.import android.graphics.Rect;
20.import android.graphics.drawable.Drawable;
21.import android.util.AttributeSet;
22.import android.view.KeyEvent;
23.import android.view.MotionEvent;
24.public abstract class AbsSeekBar extends ProgressBar {
25. private Drawable mThumb;
26. private int mThumbOffset;
27.
28. /**
29. * On touch, this offset plus the scaled value from the position of the
30. * touch will form the progress value. Usually 0.
31. */
32. float mTouchProgressOffset;
33. /**
34. * Whether this is user seekable.
35. */
36. boolean mIsUserSeekable = true;
37. /**
38. * On key presses (right or left), the amount to increment/decrement the
39. * progress.
40. */
41. private int mKeyProgressIncrement = 1;
42.
43. private static final int NO_ALPHA = 0xFF;
44. private float mDisabledAlpha;
45.
46. public AbsSeekBar(Context context) {
47. super(context);
48. }
49. public AbsSeekBar(Context context, AttributeSet attrs) {
50. super(context, attrs);
51. }
52. public AbsSeekBar(Context context, AttributeSet attrs, int defStyle) {
53. super(context, attrs, defStyle);
54. TypedArray a = context.obtainStyledAttributes(attrs,
55. com.android.internal.R.styleable.SeekBar, defStyle, 0);
56. Drawable thumb = a.getDrawable(com.android.internal.R.styleable.SeekBar_thumb);
57. setThumb(thumb);
58. int thumbOffset =
59. a.getDimensionPixelOffset(com.android.internal.R.styleable.SeekBar_thumbOffset, 0);
60. setThumbOffset(thumbOffset);
61. a.recycle();
62. a = context.obtainStyledAttributes(attrs,
63. com.android.internal.R.styleable.Theme, 0, 0);
64. mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.Theme_disabledAlpha, 0.5f);
65. a.recycle();
66. }
67. /**
68. * Sets the thumb that will
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 向下 竖直 seekBar 拖动 解释 原理