原文网址:http://blog.csdn.net/wangjinyu501/article/details/27961303

版本:1.0

日期:2014.5.17 2014.6.1
版权:© 2014 kince 转载注明出处
 
  在介绍SwitchButton之前,先来看一下系统Button是如何实现的。源码如下:
  1. @RemoteView
  2. public class Button extends TextView {
  3. public Button(Context context) {
  4. this(context, null);
  5. }
  6. public Button(Context context, AttributeSet attrs) {
  7. this(context, attrs, com.android.internal.R.attr.buttonStyle);
  8. }
  9. public Button(Context context, AttributeSet attrs, int defStyle) {
  10. super(context, attrs, defStyle);
  11. }
  12. @Override
  13. public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  14. super.onInitializeAccessibilityEvent(event);
  15. event.setClassName(Button. class.getName());
  16. }
  17. @Override
  18. public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
  19. super.onInitializeAccessibilityNodeInfo(info);
  20. info.setClassName(Button. class.getName());
  21. }
  22. }
  是直接继承于TextView,所不同的是在构造方法中添加了Button的样式,并且在初始化可见性方面交由Button类自己来处理。虽然Button的实现比较简单,但是它的子类并不是这样。看一下:
  直接子类只有有一个,CompoundButton。它是一个抽象类,而实现这个类的控件正是CheckBoxRadioButtonSwitchToggleButton这四个,所以先重点说一下它。源码如下:

  1. /**
  2. * <p>
  3. * A button with two states, checked and unchecked. When the button is pressed
  4. * or clicked, the state changes automatically.
  5. * </p>
  6. *
  7. * <p><strong>XML attributes </strong></p>
  8. * <p>
  9. * See {@link android.R.styleable#CompoundButton
  10. * CompoundButton Attributes}, {@link android.R.styleable#Button Button
  11. * Attributes}, {@link android.R.styleable#TextView TextView Attributes}, {@link
  12. * android.R.styleable #View View Attributes}
  13. * </p>
  14. */
  15. public abstract class CompoundButton extends Button implements Checkable {
  16. private boolean mChecked ;
  17. private int mButtonResource ;
  18. private boolean mBroadcasting ;
  19. private Drawable mButtonDrawable;
  20. private OnCheckedChangeListener mOnCheckedChangeListener;
  21. private OnCheckedChangeListener mOnCheckedChangeWidgetListener ;
  22. private static final int[] CHECKED_STATE_SET = {
  23. R.attr.state_checked
  24. };
  25. public CompoundButton(Context context) {
  26. this(context, null);
  27. }
  28. public CompoundButton(Context context, AttributeSet attrs) {
  29. this(context, attrs, 0);
  30. }
  31. public CompoundButton(Context context, AttributeSet attrs, int defStyle) {
  32. super(context, attrs, defStyle);
  33. TypedArray a =
  34. context.obtainStyledAttributes(
  35. attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);
  36. Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
  37. if (d != null ) {
  38. setButtonDrawable(d);
  39. }
  40. boolean checked = a
  41. .getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);
  42. setChecked(checked);
  43. a.recycle();
  44. }
  45. public void toggle() {
  46. setChecked(! mChecked);
  47. }
  48. @Override
  49. public boolean performClick() {
  50. /*
  51. * XXX: These are tiny, need some surrounding 'expanded touch area',
  52. * which will need to be implemented in Button if we only override
  53. * performClick()
  54. */
  55. /* When clicked, toggle the state */
  56. toggle();
  57. return super .performClick();
  58. }
  59. @ViewDebug.ExportedProperty
  60. public boolean isChecked() {
  61. return mChecked ;
  62. }
  63. /**
  64. * <p>Changes the checked state of this button.</p>
  65. *
  66. * @param checked true to check the button, false to uncheck it
  67. */
  68. public void setChecked(boolean checked) {
  69. if (mChecked != checked) {
  70. mChecked = checked;
  71. refreshDrawableState();
  72. notifyViewAccessibilityStateChangedIfNeeded(
  73. AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED );
  74. // Avoid infinite recursions if setChecked() is called from a listener
  75. if (mBroadcasting ) {
  76. return;
  77. }
  78. mBroadcasting = true ;
  79. if (mOnCheckedChangeListener != null) {
  80. mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
  81. }
  82. if (mOnCheckedChangeWidgetListener != null) {
  83. mOnCheckedChangeWidgetListener .onCheckedChanged(this, mChecked);
  84. }
  85. mBroadcasting = false ;
  86. }
  87. }
  88. /**
  89. * Register a callback to be invoked when the checked state of this button
  90. * changes.
  91. *
  92. * @param listener the callback to call on checked state change
  93. */
  94. public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
  95. mOnCheckedChangeListener = listener;
  96. }
  97. /**
  98. * Register a callback to be invoked when the checked state of this button
  99. * changes. This callback is used for internal purpose only.
  100. *
  101. * @param listener the callback to call on checked state change
  102. * @hide
  103. */
  104. void setOnCheckedChangeWidgetListener(OnCheckedChangeListener listener) {
  105. mOnCheckedChangeWidgetListener = listener;
  106. }
  107. /**
  108. * Interface definition for a callback to be invoked when the checked state
  109. * of a compound button changed.
  110. */
  111. public static interface OnCheckedChangeListener {
  112. /**
  113. * Called when the checked state of a compound button has changed.
  114. *
  115. * @param buttonView The compound button view whose state has changed.
  116. * @param isChecked  The new checked state of buttonView.
  117. */
  118. void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
  119. }
  120. /**
  121. * Set the background to a given Drawable, identified by its resource id.
  122. *
  123. * @param resid the resource id of the drawable to use as the background
  124. */
  125. public void setButtonDrawable(int resid) {
  126. if (resid != 0 && resid == mButtonResource ) {
  127. return;
  128. }
  129. mButtonResource = resid;
  130. Drawable d = null;
  131. if (mButtonResource != 0) {
  132. d = getResources().getDrawable(mButtonResource );
  133. }
  134. setButtonDrawable(d);
  135. }
  136. /**
  137. * Set the background to a given Drawable
  138. *
  139. * @param d The Drawable to use as the background
  140. */
  141. public void setButtonDrawable(Drawable d) {
  142. if (d != null ) {
  143. if (mButtonDrawable != null) {
  144. mButtonDrawable.setCallback(null);
  145. unscheduleDrawable( mButtonDrawable);
  146. }
  147. d.setCallback( this);
  148. d.setVisible(getVisibility() == VISIBLE, false);
  149. mButtonDrawable = d;
  150. setMinHeight(mButtonDrawable .getIntrinsicHeight());
  151. }
  152. refreshDrawableState();
  153. }
  154. @Override
  155. public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  156. super.onInitializeAccessibilityEvent(event);
  157. event.setClassName(CompoundButton.class .getName());
  158. event.setChecked( mChecked);
  159. }
  160. @Override
  161. public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
  162. super.onInitializeAccessibilityNodeInfo(info);
  163. info.setClassName(CompoundButton.class .getName());
  164. info.setCheckable( true);
  165. info.setChecked( mChecked);
  166. }
  167. @Override
  168. public int getCompoundPaddingLeft() {
  169. int padding = super.getCompoundPaddingLeft();
  170. if (!isLayoutRtl()) {
  171. final Drawable buttonDrawable = mButtonDrawable;
  172. if (buttonDrawable != null) {
  173. padding += buttonDrawable.getIntrinsicWidth();
  174. }
  175. }
  176. return padding;
  177. }
  178. @Override
  179. public int getCompoundPaddingRight() {
  180. int padding = super.getCompoundPaddingRight();
  181. if (isLayoutRtl()) {
  182. final Drawable buttonDrawable = mButtonDrawable;
  183. if (buttonDrawable != null) {
  184. padding += buttonDrawable.getIntrinsicWidth();
  185. }
  186. }
  187. return padding;
  188. }
  189. /**
  190. * @hide
  191. */
  192. @Override
  193. public int getHorizontalOffsetForDrawables() {
  194. final Drawable buttonDrawable = mButtonDrawable ;
  195. return (buttonDrawable != null) ? buttonDrawable.getIntrinsicWidth() : 0;
  196. }
  197. @Override
  198. protected void onDraw(Canvas canvas) {
  199. super.onDraw(canvas);
  200. final Drawable buttonDrawable = mButtonDrawable ;
  201. if (buttonDrawable != null) {
  202. final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK ;
  203. final int drawableHeight = buttonDrawable.getIntrinsicHeight();
  204. final int drawableWidth = buttonDrawable.getIntrinsicWidth();
  205. int top = 0;
  206. switch (verticalGravity) {
  207. case Gravity.BOTTOM :
  208. top = getHeight() - drawableHeight;
  209. break;
  210. case Gravity.CENTER_VERTICAL :
  211. top = (getHeight() - drawableHeight) / 2;
  212. break;
  213. }
  214. int bottom = top + drawableHeight;
  215. int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
  216. int right = isLayoutRtl() ? getWidth() : drawableWidth;
  217. buttonDrawable.setBounds(left, top, right, bottom);
  218. buttonDrawable.draw(canvas);
  219. }
  220. }
  221. @Override
  222. protected int[] onCreateDrawableState(int extraSpace) {
  223. final int [] drawableState = super.onCreateDrawableState(extraSpace + 1);
  224. if (isChecked()) {
  225. mergeDrawableStates(drawableState, CHECKED_STATE_SET);
  226. }
  227. return drawableState;
  228. }
  229. @Override
  230. protected void drawableStateChanged() {
  231. super.drawableStateChanged();
  232. if (mButtonDrawable != null) {
  233. int[] myDrawableState = getDrawableState();
  234. // Set the state of the Drawable
  235. mButtonDrawable.setState(myDrawableState);
  236. invalidate();
  237. }
  238. }
  239. @Override
  240. protected boolean verifyDrawable(Drawable who) {
  241. return super .verifyDrawable(who) || who == mButtonDrawable;
  242. }
  243. @Override
  244. public void jumpDrawablesToCurrentState() {
  245. super.jumpDrawablesToCurrentState();
  246. if (mButtonDrawable != null) mButtonDrawable.jumpToCurrentState();
  247. }
  248. static class SavedState extends BaseSavedState {
  249. boolean checked ;
  250. /**
  251. * Constructor called from {@link CompoundButton#onSaveInstanceState()}
  252. */
  253. SavedState(Parcelable superState) {
  254. super(superState);
  255. }
  256. /**
  257. * Constructor called from {@link #CREATOR}
  258. */
  259. private SavedState(Parcel in) {
  260. super(in);
  261. checked = (Boolean)in.readValue( null);
  262. }
  263. @Override
  264. public void writeToParcel(Parcel out, int flags) {
  265. super.writeToParcel(out, flags);
  266. out.writeValue( checked);
  267. }
  268. @Override
  269. public String toString() {
  270. return "CompoundButton.SavedState{"
  271. + Integer.toHexString(System.identityHashCode(this))
  272. + " checked=" + checked + "}" ;
  273. }
  274. public static final Parcelable.Creator<SavedState> CREATOR
  275. = new Parcelable.Creator<SavedState>() {
  276. public SavedState createFromParcel(Parcel in) {
  277. return new SavedState(in);
  278. }
  279. public SavedState[] newArray(int size) {
  280. return new SavedState[size];
  281. }
  282. };
  283. }
  284. @Override
  285. public Parcelable onSaveInstanceState() {
  286. // Force our ancestor class to save its state
  287. setFreezesText( true);
  288. Parcelable superState = super.onSaveInstanceState();
  289. SavedState ss = new SavedState(superState);
  290. ss. checked = isChecked();
  291. return ss;
  292. }
  293. @Override
  294. public void onRestoreInstanceState(Parcelable state) {
  295. SavedState ss = (SavedState) state;
  296. super.onRestoreInstanceState(ss.getSuperState());
  297. setChecked(ss. checked);
  298. requestLayout();
  299. }
  300. }

先从构造方法开始,在构造方法中,

  1. public CompoundButton(Context context, AttributeSet attrs, int defStyle) {
  2. super(context, attrs, defStyle);
  3. TypedArray a =
  4. context.obtainStyledAttributes(
  5. attrs, com.android.internal.R.styleable.CompoundButton, defStyle, 0);
  6. Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
  7. if (d != null ) {
  8. setButtonDrawable(d);
  9. }
  10. boolean checked = a
  11. .getBoolean(com.android.internal.R.styleable.CompoundButton_checked, false);
  12. setChecked(checked);
  13. a.recycle();
  14. }

先是从attrs中读取定义的属性,一个是Drawable用于设置背景;一个是布尔类型变量用于判断是否check过。设置背景使用的是setButtonDrawable()方法,代码如下:

  1. /**
  2. * Set the background to a given Drawable
  3. *
  4. * @param d The Drawable to use as the background
  5. */
  6. public void setButtonDrawable(Drawable d) {
  7. if (d != null ) {
  8. if (mButtonDrawable != null) {
  9. mButtonDrawable.setCallback(null);
  10. unscheduleDrawable( mButtonDrawable);
  11. }
  12. d.setCallback( this);
  13. d.setVisible(getVisibility() == VISIBLE, false);
  14. mButtonDrawable = d;
  15. setMinHeight(mButtonDrawable .getIntrinsicHeight());
  16. }
  17. refreshDrawableState();
  18. }

这个方法写的就比较完善,可以作为一个学习的典范。首先判断传递过来的Drawable是否为空,如果不为空并且默认的Drawable也不为空,那么取消默认Drawable的callback,然后调用unscheduleDrawable方法。这个方法代码如下:

  1. /**
  2. * Unschedule any events associated with the given Drawable.  This can be
  3. * used when selecting a new Drawable into a view, so that the previous
  4. * one is completely unscheduled.
  5. *
  6. * @param who The Drawable to unschedule.
  7. *
  8. * @see #drawableStateChanged
  9. */
  10. public void unscheduleDrawable(Drawable who) {
  11. if (mAttachInfo != null && who != null) {
  12. mAttachInfo.mViewRootImpl .mChoreographer.removeCallbacks(
  13. Choreographer.CALLBACK_ANIMATION, null, who);
  14. }
  15. }

从方法注释中可以看出它的用途,正是更换Drawable时候使用的。接下来开始重新设置Drawable,包括回调、可见性、最小高度。最后调用refreshDrawableState()方法,这个是View类的方法,用于更新Drawable状态。

  然后再回过头看一下setChecked(checked)方法,这个用于设置check,也就是button的点击状态。代码如下:
  1. /**
  2. * <p>Changes the checked state of this button.</p>
  3. *
  4. * @param checked true to check the button, false to uncheck it
  5. */
  6. public void setChecked( boolean checked) {
  7. if (mChecked != checked) {
  8. mChecked = checked;
  9. refreshDrawableState();
  10. notifyViewAccessibilityStateChangedIfNeeded(
  11. AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED );
  12. // Avoid infinite recursions if setChecked() is called from a listener
  13. if (mBroadcasting ) {
  14. return;
  15. }
  16. mBroadcasting = true ;
  17. if (mOnCheckedChangeListener != null) {
  18. mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
  19. }
  20. if (mOnCheckedChangeWidgetListener != null) {
  21. mOnCheckedChangeWidgetListener .onCheckedChanged(this, mChecked);
  22. }
  23. mBroadcasting = false ;
  24. }
  25. }
  在这个方法中多出了一个接口,这个接口真是check的一个回调接口,代码如下:
  1. /**
  2. * Interface definition for a callback to be invoked when the checked state
  3. * of a compound button changed.
  4. */
  5. public static interface OnCheckedChangeListener {
  6. /**
  7. * Called when the checked state of a compound button has changed.
  8. *
  9. * @param buttonView The compound button view whose state has changed.
  10. * @param isChecked  The new checked state of buttonView.
  11. */
  12. void onCheckedChanged(CompoundButton buttonView, boolean isChecked);
  13. }
  这种回调接口在Android中处处可见,之前的文章也有介绍过。但是在上面的方法,它使用了一个mBroadcasting变量,进而巧妙地避免了重复递归的问题,大家自己感受一下。
  然后就是ondraw()方法了,把之前的drawable画出来。代码如下:
  1. @Override
  2. protected void onDraw(Canvas canvas) {
  3. super.onDraw(canvas);
  4. final Drawable buttonDrawable = mButtonDrawable ;
  5. if (buttonDrawable != null) {
  6. final int verticalGravity = getGravity() & Gravity.VERTICAL_GRAVITY_MASK ;
  7. final int drawableHeight = buttonDrawable.getIntrinsicHeight();
  8. final int drawableWidth = buttonDrawable.getIntrinsicWidth();
  9. int top = 0;
  10. switch (verticalGravity) {
  11. case Gravity.BOTTOM :
  12. top = getHeight() - drawableHeight;
  13. break;
  14. case Gravity.CENTER_VERTICAL :
  15. top = (getHeight() - drawableHeight) / 2;
  16. break;
  17. }
  18. int bottom = top + drawableHeight;
  19. int left = isLayoutRtl() ? getWidth() - drawableWidth : 0;
  20. int right = isLayoutRtl() ? getWidth() : drawableWidth;
  21. buttonDrawable.setBounds(left, top, right, bottom);
  22. buttonDrawable.draw(canvas);
  23. }
  24. }
  看得出来,在onDrawable()方法中,最主要的部分还是如何确定上下左右四个参数。确定完后就可以画出来了。但是,CompoundButton是一个抽象类,并不能直接使用,那看一下它的子类是如何实现的:
 
1、CheckBox
  1. public class CheckBox extends CompoundButton {
  2. public CheckBox(Context context) {
  3. this(context, null);
  4. }
  5. public CheckBox(Context context, AttributeSet attrs) {
  6. this(context, attrs, com.android.internal.R.attr.checkboxStyle);
  7. }
  8. public CheckBox(Context context, AttributeSet attrs, int defStyle) {
  9. super(context, attrs, defStyle);
  10. }
  11. @Override
  12. public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  13. super.onInitializeAccessibilityEvent(event);
  14. event.setClassName(CheckBox. class.getName());
  15. }
  16. @Override
  17. public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
  18. super.onInitializeAccessibilityNodeInfo(info);
  19. info.setClassName(CheckBox. class.getName());
  20. }
  21. }
  和Button的实现差不多,使用了一个自己的样式。并且也是重写了那两个方法。再来看一下RadioButton,
  1. public class RadioButton extends CompoundButton {
  2. public RadioButton(Context context) {
  3. this(context, null);
  4. }
  5. public RadioButton(Context context, AttributeSet attrs) {
  6. this(context, attrs, com.android.internal.R.attr.radioButtonStyle);
  7. }
  8. public RadioButton(Context context, AttributeSet attrs, int defStyle) {
  9. super(context, attrs, defStyle);
  10. }
  11. /**
  12. * {@inheritDoc}
  13. * <p>
  14. * If the radio button is already checked, this method will not toggle the radio button.
  15. */
  16. @Override
  17. public void toggle() {
  18. // we override to prevent toggle when the radio is already
  19. // checked (as opposed to check boxes widgets)
  20. if (!isChecked()) {
  21. super.toggle();
  22. }
  23. }
  24. @Override
  25. public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
  26. super.onInitializeAccessibilityEvent(event);
  27. event.setClassName(RadioButton. class.getName());
  28. }
  29. @Override
  30. public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
  31. super.onInitializeAccessibilityNodeInfo(info);
  32. info.setClassName(RadioButton. class.getName());
  33. }
  34. }

和CheckBox实现差不多,区别在于多重写了一个方法,用于防止按钮被重复点击。另外还有ToggleButton以及Switch,前者实现也比较简单,后者稍微麻烦了一些,感兴趣可以自己分析。

  最后切入正题,看看滑动Button要如何实现呢?首先看一下效果图:
 图1-1
  
 图1-2
  图1-1所示的滑动Button实现的思路是这样的,背景图片有开和关的文字,一个按钮在其上面左右滑动,遮住相应的部分,使其在一个位置时候只能看到一个开关。
  如图1-3,在实现的时候,先画一个开关背景图片只,然后在其上面画一个按钮,滑动开关的时候对上面的按钮进行处理即可。
  准备:
 
    1、按钮图片
       
      
    2、背景图片       
       
 编码:
    在自定义滑动按钮控件的时候,可以有多种选择,可以继承于Button,也可以继承于Button的子类,也可以继承于View类等。我们知道滑动按钮是一个很简单的控件,就是左右滑动改变显示内容,不需要其他的额外东西在里面,所以直接继承于View来实现即可。如果继承于系统的一些控件,那么有很多东西用不到,会造成浪费。
    1、定义一个类继承于View,初始化构造方法,在构造方法中加载图片及其信息。
    2、重写onMeasure()方法,计算控件的大小。
    3、重写onTouchEvent()方法,对滑动事件进行判别处理。
    4、定义接口,实现回调。
    5、重写onDraw()方法,动态画出按钮。
代码如下:
  1. /**
  2. *
  3. */
  4. package com.kince.slidebutton;
  5. import android.content.Context;
  6. import android.graphics.Bitmap;
  7. import android.graphics.BitmapFactory;
  8. import android.graphics.Canvas;
  9. import android.util.AttributeSet;
  10. import android.util.Log;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13. /**
  14. * @author kince
  15. * @category 左右手势滑动button
  16. * @serial 1.0.0
  17. * @since 2014.5.17
  18. * @see http://blog.csdn.net/wangjinyu501
  19. *
  20. */
  21. public class SlideButton extends View {
  22. private Bitmap slideBitMap;// 滑动图片
  23. private Bitmap switchBitMap;// 背景图片
  24. private int slideBitMapWidth;// 滑动图片宽度
  25. private int switchBitMapWidth;// 背景图片宽度
  26. private int switchBitMapHeight;// 背景图片高度
  27. private boolean currentState;// 开关状态
  28. private boolean isSliding = false; // 是否正在滑动中
  29. private int currentX; // 当前开关的位置
  30. private OnToggleStateChangedListener mChangedListener;// 回调接口
  31. /**
  32. * @param context
  33. *            在java代码中直接调用使用此构造方法
  34. */
  35. public SlideButton(Context context) {
  36. this(context, null);
  37. // TODO Auto-generated constructor stub
  38. }
  39. /**
  40. * @param context
  41. * @param attrs
  42. *            在xml中使用要用到这个方法
  43. */
  44. public SlideButton(Context context, AttributeSet attrs) {
  45. this(context, attrs, 0);
  46. // TODO Auto-generated constructor stub
  47. }
  48. /**
  49. * @param context
  50. * @param attrs
  51. * @param defStyleAttr
  52. *            指定一个样式
  53. */
  54. public SlideButton(Context context, AttributeSet attrs, int defStyleAttr) {
  55. super(context, attrs, defStyleAttr);
  56. initBitmap();
  57. }
  58. /**
  59. * @category 加载背景图片以及开关图片 然后获取各自的宽高
  60. *
  61. */
  62. private void initBitmap() {
  63. // TODO Auto-generated method stub
  64. slideBitMap = BitmapFactory.decodeResource(getResources(),
  65. R.drawable.slide_button_background);
  66. switchBitMap = BitmapFactory.decodeResource(getResources(),
  67. R.drawable.switch_background);
  68. slideBitMapWidth = slideBitMap.getWidth();
  69. switchBitMapWidth = switchBitMap.getWidth();
  70. switchBitMapHeight = switchBitMap.getHeight();
  71. Log.i("switchBitMapWidth", switchBitMapWidth + "");
  72. }
  73. @Override
  74. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  75. // TODO Auto-generated method stub
  76. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  77. setMeasuredDimension(switchBitMapWidth, switchBitMapHeight);// 设置控件的宽高
  78. }
  79. @Override
  80. protected void onDraw(Canvas canvas) {
  81. // 绘制button背景图片
  82. canvas.drawBitmap(switchBitMap, 0, 0, null);
  83. // 绘制滑动开关
  84. if (isSliding) {// 如果当前状态是滑动中 则动态绘制开关
  85. int dis = currentX - slideBitMapWidth / 2;
  86. if (dis < 0) {
  87. dis = 0;
  88. } else if (dis > switchBitMapWidth - slideBitMapWidth) {
  89. dis = switchBitMapWidth - slideBitMapWidth;
  90. }
  91. canvas.drawBitmap(slideBitMap, dis, 0, null);
  92. } else {
  93. if (currentState) { // 绘制开关为开的状态
  94. canvas.drawBitmap(slideBitMap, switchBitMapWidth
  95. - slideBitMapWidth, 0, null);
  96. } else { // 绘制开关为关的状态
  97. canvas.drawBitmap(slideBitMap, 0, 0, null);
  98. }
  99. }
  100. super.onDraw(canvas);
  101. }
  102. @Override
  103. public boolean onTouchEvent(MotionEvent event) {
  104. // 手势识别 判断滑动方向
  105. int action = event.getAction();
  106. switch (action) {
  107. case MotionEvent.ACTION_DOWN:
  108. isSliding = true;
  109. currentX = (int) event.getX();
  110. break;
  111. case MotionEvent.ACTION_MOVE:
  112. currentX = (int) event.getX();
  113. Log.i("currentX", currentX + "");
  114. break;
  115. case MotionEvent.ACTION_UP:
  116. isSliding = false;
  117. int bgCenter = switchBitMapWidth / 2;
  118. boolean state = currentX > bgCenter; // 改变后的状态
  119. if (state != currentState && mChangedListener != null) {// 添加回调
  120. mChangedListener.onToggleStateChanged(state);
  121. }
  122. currentState = state;
  123. break;
  124. default:
  125. break;
  126. }
  127. invalidate();
  128. return true;
  129. }
  130. public OnToggleStateChangedListener getmChangedListener() {
  131. return mChangedListener;
  132. }
  133. public void setmChangedListener(
  134. OnToggleStateChangedListener mChangedListener) {
  135. this.mChangedListener = mChangedListener;
  136. }
  137. public boolean isToggleState() {
  138. return currentState;
  139. }
  140. public void setToggleState(boolean currentState) {
  141. this.currentState = currentState;
  142. }
  143. }

回调接口,

  1. package com.kince.slidebutton;
  2. **
  3. * @author kince
  4. *
  5. */
  6. ublic interface OnToggleStateChangedListener {
  7. /**
  8. * @category
  9. * @param state
  10. */
  11. public void onToggleStateChanged(boolean state);

Activity代码,

  1. package com.kince.slidebutton;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.support.v7.app.ActionBar;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.Toast;
  13. import android.os.Build;
  14. public class MainActivity extends ActionBarActivity {
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. if (savedInstanceState == null) {
  20. getSupportFragmentManager().beginTransaction()
  21. .add(R.id.container, new PlaceholderFragment()).commit();
  22. }
  23. }
  24. @Override
  25. public boolean onCreateOptionsMenu(Menu menu) {
  26. // Inflate the menu; this adds items to the action bar if it is present.
  27. getMenuInflater().inflate(R.menu.main, menu);
  28. return true;
  29. }
  30. @Override
  31. public boolean onOptionsItemSelected(MenuItem item) {
  32. // Handle action bar item clicks here. The action bar will
  33. // automatically handle clicks on the Home/Up button, so long
  34. // as you specify a parent activity in AndroidManifest.xml.
  35. int id = item.getItemId();
  36. if (id == R.id.action_settings) {
  37. return true;
  38. }
  39. return super.onOptionsItemSelected(item);
  40. }
  41. /**
  42. * A placeholder fragment containing a simple view.
  43. */
  44. public static class PlaceholderFragment extends Fragment implements
  45. OnToggleStateChangedListener {
  46. private SlideButton slidebutton;
  47. public PlaceholderFragment() {
  48. }
  49. @Override
  50. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  51. Bundle savedInstanceState) {
  52. View rootView = inflater.inflate(R.layout.fragment_main, container,
  53. false);
  54. slidebutton = (SlideButton) rootView.findViewById(R.id.slidebutton1);
  55. // 设置一下开关的状态
  56. slidebutton.setToggleState(true); // 设置开关的状态为打开
  57. slidebutton.setmChangedListener(this);
  58. return rootView;
  59. }
  60. @Override
  61. public void onToggleStateChanged(boolean state) {
  62. // TODO Auto-generated method stub
  63. FragmentActivity activity = getActivity();
  64. if (state) {
  65. Toast.makeText(activity, "开关打开", 0).show();
  66. } else {
  67. Toast.makeText(activity, "开关关闭", 0).show();
  68. }
  69. }
  70. }
  71. }

未完待续。

【转】Android SwitchButton(滑动开关)的更多相关文章

  1. Android SwitchButton(滑动开关)

    @RemoteView public class Button extends TextView { public Button(Context context) { this(context, nu ...

  2. Android第三方开源SwitchButton

    Android第三方开源SwitchButton Android SwitchButton是github上的一个第三方开源项目,其项目主页是:https://github.com/kyleduo/Sw ...

  3. 【Android】自己定义控件实现可滑动的开关(switch)

    ~转载请注明来源:http://blog.csdn.net/u013015161/article/details/46704745 介绍 昨天晚上写了一个Android的滑动开关, 即SlideSwi ...

  4. Android 三档自定义滑动开关,禁止点击功能的实现,用默认的seekbar组件实现

    android三档自定义滑动开关,禁止点击功能的实现,普通开关网上有很多例子,三档滑动开关的则找了整天都没有相关例子,开始用普通开关的源码修改了自己实现了一个类,但效果不如人意,各种边界情况的算法很难 ...

  5. android 自己定义开关(SwitchButton)

    近期心血来潮,写了一个自己定义仿iPhone的开关. 有须要的同学能够来下载啦.支持点击自己主动滚动,速率能够自己依据须要改动.触摸滚动,大小自己定义,支持改动样式.就不录制动画,就上传了两张图给大家 ...

  6. Android实现Material Design风格的设置页面(滑动开关控件)

    前言 本文链接 http://blog.csdn.net/never_cxb/article/details/50763271 转载请注明出处 參考了这篇文章 Material Design 风格的设 ...

  7. android自定义控件——以滑动开关为例

    0.引言 (1)Android从4.0开始提供了switch的滑动开关效果组件,但是之前版本却没有 (2)很多时候我们写程序,都希望把有用的通用的通用的东西封装起来,以便以后重用. 本文根据组件开发思 ...

  8. Android 自定义的开关按钮——SwitchButton

    本文转自:http://blog.csdn.net/swust_chenpeng/article/details/19967501 我将原文的控件进行了一些修改,去掉了原来控件的外边框,只留下重要的遮 ...

  9. Android自己定义View基础篇(三)之SwitchButton开关

    自己定义View基础篇(二) 自己定义View基础篇(一) 自己定义View原理 我在解说之前,先来看看效果图,有图有真相:(转换gif图片效果太差) 那来看看真实图片: 假设你要更改样式,请改动例如 ...

随机推荐

  1. app被Rejected 的各种原因翻译

    1. Terms and conditions(法律与条款) 1.1 As a developer of applications for the App Store you are bound by ...

  2. [转载]实战Linux下VMware虚拟机根目录空间扩充

    [转载]实战Linux下VMware虚拟机根目录空间扩充 (2011-07-31 21:34:34) 转载▼ 标签: 转载   原文地址:实战Linux下VMware虚拟机根目录空间扩充作者:shar ...

  3. Selenium如何使用自定义的Firefox配置文件?

    一.自动保存文件 import os from selenium import webdriver fp = webdriver.FirefoxProfile() fp.set_preference( ...

  4. java数据结构学习(一)之二分查找

      二分查找法与我们在孩童时期玩过的猜数的游戏一样,这个游戏里一个朋友会让你猜他正想的一个1至100的数,当你猜了一个数后,他会告诉你三种选择的一个:你猜的比她想的大,或小,或猜中了.为了能用最少的次 ...

  5. 创建和编辑 crontab 文件

    http://docs.oracle.com/cd/E24847_01/html/819-6951/sysrescron-24589.html 创建和编辑 crontab 文件 创建 crontab  ...

  6. hdu 1370 Biorhythms

    中国剩余定理……. 链接http://acm.hdu.edu.cn/showproblem.php?pid=1370 /**************************************** ...

  7. Linqer工具

    这些天写Linq挺烦人的,就上网搜搜可有什么好的sql转Linq的工具,咦,马上就看上了Linqer. 哈哈,介绍一下使用方法吧: 官方下载网站:http://sqltolinq.com/downlo ...

  8. hdu 3972 1 M possible

    一般做法: 显然的超内存 #include<stdio.h> #include<algorithm> using namespace std; ],ans[]; int mai ...

  9. 【mongoDB高级篇①】聚集运算之group,aggregate

    group 语法 db.collection.group({ key:{field:1},//按什么字段进行分组 initial:{count:0},//进行分组前变量初始化,该处声明的变量可以在以下 ...

  10. sizeof 和strlen的区别

    1. 编译时计算运算符sizeof,可用类型或变量做参数,计算占用内存的大小.sizeof后若是类型必须加括弧,若是变量名可不加括弧.sizeof(x)可用来定义数组维数.如:printf(" ...