资讯专栏INFORMATION COLUMN

自定义仿 IPhone 开关控件

rottengeek / 2081人阅读

摘要:极力推荐文章欢迎收藏干货分享阅读五分钟,每日十点,和您一起终身学习,这里是程序员本篇文章主要介绍开发中的部分知识点,通过阅读本篇文章,您将收获以下内容自定义类实现自定义布局自定义素材使用自定义自定义常用于中,主要控制开关的开启与关闭。

极力推荐文章:欢迎收藏
Android 干货分享

阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

</>复制代码

  1. 自定义View类实现

  2. 自定义View 布局

  3. 自定义View 素材

  4. Activity使用自定义View

自定义ItemToggleView 常用于Settings中,主要控制开关的开启与关闭。

自定义ItemToggleView实现效果如下:

1. 自定义View类实现

</>复制代码

  1. public class SwitchControlView extends View implements OnTouchListener {
  2. private Bitmap bg_on, bg_off, slipper_btn;
  3. private float downX, nowX;
  4. private boolean onSlip = false;
  5. private boolean nowStatus = false;
  6. private OnChangedListener listener;
  7. public SwitchControlView(Context context) {
  8. super(context);
  9. init();
  10. }
  11. public SwitchControlView(Context context, AttributeSet attrs) {
  12. super(context, attrs);
  13. init();
  14. }
  15. public void init() {
  16. bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.on_btn);
  17. bg_off = BitmapFactory.decodeResource(getResources(),
  18. R.drawable.off_btn);
  19. slipper_btn = BitmapFactory.decodeResource(getResources(),
  20. R.drawable.white_btn);
  21. setOnTouchListener(this);
  22. }
  23. protected void onDraw(Canvas canvas) {
  24. super.onDraw(canvas);
  25. Matrix matrix = new Matrix();
  26. Paint paint = new Paint();
  27. float x = 0;
  28. if (bg_on != null && bg_off != null) {
  29. if (nowX < (bg_on.getWidth() / 2)) {
  30. canvas.drawBitmap(bg_off, matrix, paint);
  31. } else {
  32. canvas.drawBitmap(bg_on, matrix, paint);
  33. }
  34. }
  35. if (onSlip) {
  36. if (nowX >= bg_on.getWidth())
  37. x = bg_on.getWidth() - slipper_btn.getWidth() / 2;
  38. else
  39. x = nowX - slipper_btn.getWidth() / 2;
  40. } else {
  41. if (nowStatus) {
  42. x = bg_on.getWidth() - slipper_btn.getWidth();
  43. } else {
  44. x = 0;
  45. }
  46. }
  47. if (x < 0) {
  48. x = 0;
  49. } else if (x > bg_on.getWidth() - slipper_btn.getWidth()) {
  50. x = bg_on.getWidth() - slipper_btn.getWidth();
  51. }
  52. canvas.drawBitmap(slipper_btn, x, 0, paint);
  53. }
  54. @Override
  55. public boolean onTouch(View v, MotionEvent event) {
  56. switch (event.getAction()) {
  57. case MotionEvent.ACTION_DOWN: {
  58. if (event.getX() > bg_off.getWidth()
  59. || event.getY() > bg_off.getHeight()) {
  60. return false;
  61. } else {
  62. onSlip = true;
  63. downX = event.getX();
  64. nowX = downX;
  65. }
  66. break;
  67. }
  68. case MotionEvent.ACTION_MOVE: {
  69. nowX = event.getX();
  70. break;
  71. }
  72. case MotionEvent.ACTION_UP: {
  73. onSlip = false;
  74. if (event.getX() >= (bg_on.getWidth() / 2)) {
  75. nowStatus = true;
  76. nowX = bg_on.getWidth() - slipper_btn.getWidth();
  77. } else {
  78. nowStatus = false;
  79. nowX = 0;
  80. }
  81. if (listener != null) {
  82. listener.OnChanged(SwitchControlView.this, nowStatus);
  83. }
  84. break;
  85. }
  86. }
  87. invalidate();
  88. return true;
  89. }
  90. public void setOnChangedListener(OnChangedListener listener) {
  91. this.listener = listener;
  92. }
  93. public void setChecked(boolean checked) {
  94. if (checked) {
  95. nowX = bg_off.getWidth();
  96. } else {
  97. nowX = 0;
  98. }
  99. nowStatus = checked;
  100. }
  101. public interface OnChangedListener {
  102. public void OnChanged(SwitchControlView wiperSwitch, boolean checkState);
  103. }
  104. }
2.自定义View 布局

</>复制代码

3. 自定义View 素材


4. Activity使用自定义View

</>复制代码

  1. /**
  2. * InitSwitchView 自定义滑动开关实现
  3. */
  4. private void InitSwitchView() {
  5. // TODO Auto-generated method stub
  6. SwitchControlView wiperSwitch = (SwitchControlView) findViewById(R.id.switch_control_view);
  7. wiperSwitch.setChecked(true);
  8. wiperSwitch.setOnChangedListener(new OnChangedListener() {
  9. @Override
  10. public void OnChanged(SwitchControlView wiperSwitch,
  11. boolean checkState) {
  12. // TODO Auto-generated method stub
  13. if (checkState) {
  14. Toast.makeText(CustomViewMethods.this, "开启", 1).show();
  15. } else {
  16. Toast.makeText(CustomViewMethods.this, "关闭", 1).show();
  17. }
  18. }
  19. });
  20. }

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75898.html

相关文章

  • 「码个蛋」2017年200篇精选干货集合

    摘要:让你收获满满码个蛋从年月日推送第篇文章一年过去了已累积推文近篇文章,本文为年度精选,共计篇,按照类别整理便于读者主题阅读。本篇文章是今年的最后一篇技术文章,为了让大家在家也能好好学习,特此花了几个小时整理了这些文章。 showImg(https://segmentfault.com/img/remote/1460000013241596); 让你收获满满! 码个蛋从2017年02月20...

    wangtdgoodluck 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<