├── images
├── 0.jpg
└── 1.jpg
├── ic_launcher-web.png
├── libs
└── android-support-v4.jar
├── res
├── drawable-hdpi
│ ├── ic_launcher.png
│ ├── switch_off.png
│ ├── switch_on.png
│ ├── switch_button.png
│ ├── switch_background.png
│ └── slide_button_background.png
├── drawable-mdpi
│ └── ic_launcher.png
├── drawable-xhdpi
│ └── ic_launcher.png
├── drawable-xxhdpi
│ └── ic_launcher.png
├── values-sw600dp
│ └── dimens.xml
├── values
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── menu
│ └── main.xml
├── values-sw720dp-land
│ └── dimens.xml
├── values-v11
│ └── styles.xml
├── values-v14
│ └── styles.xml
└── layout
│ └── activity_main.xml
├── .settings
└── org.eclipse.jdt.core.prefs
├── README.md
├── src
└── com
│ └── yang
│ └── togglebutton
│ ├── IOnToggleStateChangeListener.java
│ ├── MainActivity.java
│ └── CustomToggleButton.java
├── .classpath
├── project.properties
├── proguard-project.txt
├── .project
└── AndroidManifest.xml
/images/0.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/images/0.jpg
--------------------------------------------------------------------------------
/images/1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/images/1.jpg
--------------------------------------------------------------------------------
/ic_launcher-web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/ic_launcher-web.png
--------------------------------------------------------------------------------
/libs/android-support-v4.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/libs/android-support-v4.jar
--------------------------------------------------------------------------------
/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/switch_off.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-hdpi/switch_off.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/switch_on.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-hdpi/switch_on.png
--------------------------------------------------------------------------------
/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/switch_button.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-hdpi/switch_button.png
--------------------------------------------------------------------------------
/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/switch_background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-hdpi/switch_background.png
--------------------------------------------------------------------------------
/res/drawable-hdpi/slide_button_background.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/xuningjack/IOSToggleButton/HEAD/res/drawable-hdpi/slide_button_background.png
--------------------------------------------------------------------------------
/.settings/org.eclipse.jdt.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
3 | org.eclipse.jdt.core.compiler.compliance=1.6
4 | org.eclipse.jdt.core.compiler.source=1.6
5 |
--------------------------------------------------------------------------------
/res/values-sw600dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | 16dp
5 | 16dp
6 |
7 |
8 |
--------------------------------------------------------------------------------
/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | ToggleButton
5 | Settings
6 | Hello world!
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # IOSToggleButton
2 | toggle button like in IOS original system(仿IOS原生的toggle button)
3 | 开启状态:
4 | 
5 | 关闭状态:
6 | 
7 |
--------------------------------------------------------------------------------
/res/menu/main.xml:
--------------------------------------------------------------------------------
1 |
10 |
--------------------------------------------------------------------------------
/res/values-sw720dp-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 | 128dp
8 |
9 |
10 |
--------------------------------------------------------------------------------
/res/values-v11/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/res/values-v14/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
8 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/src/com/yang/togglebutton/IOnToggleStateChangeListener.java:
--------------------------------------------------------------------------------
1 | package com.yang.togglebutton;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * IMToggleButton状态改变的回调接口
7 | * @author 徐宁
8 | * @date: 2014-10-30 上午8:42:21
9 | */
10 | public interface IOnToggleStateChangeListener {
11 |
12 | /**
13 | * 当监听状态改变时的回调方法
14 | * @author 徐宁
15 | * @param view 操作的哪个ToggleButton
16 | * @param state 操作后的状态
17 | */
18 | void onToggleStateChange(View view, boolean state);
19 | }
20 |
--------------------------------------------------------------------------------
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/project.properties:
--------------------------------------------------------------------------------
1 | # This file is automatically generated by Android Tools.
2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3 | #
4 | # This file must be checked in Version Control Systems.
5 | #
6 | # To customize properties used by the Ant build system edit
7 | # "ant.properties", and override values to adapt the script to your
8 | # project structure.
9 | #
10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12 |
13 | # Project target.
14 | target=android-19
15 |
--------------------------------------------------------------------------------
/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
14 |
15 |
16 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/proguard-project.txt:
--------------------------------------------------------------------------------
1 | # To enable ProGuard in your project, edit project.properties
2 | # to define the proguard.config property as described in that file.
3 | #
4 | # Add project specific ProGuard rules here.
5 | # By default, the flags in this file are appended to flags specified
6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt
7 | # You can edit the include path and order by changing the ProGuard
8 | # include property in project.properties.
9 | #
10 | # For more details, see
11 | # http://developer.android.com/guide/developing/tools/proguard.html
12 |
13 | # Add any project specific keep options here:
14 |
15 | # If your project uses WebView with JS, uncomment the following
16 | # and specify the fully qualified class name to the JavaScript interface
17 | # class:
18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19 | # public *;
20 | #}
21 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | ToggleButton
4 |
5 |
6 |
7 |
8 |
9 | com.android.ide.eclipse.adt.ResourceManagerBuilder
10 |
11 |
12 |
13 |
14 | com.android.ide.eclipse.adt.PreCompilerBuilder
15 |
16 |
17 |
18 |
19 | org.eclipse.jdt.core.javabuilder
20 |
21 |
22 |
23 |
24 | com.android.ide.eclipse.adt.ApkBuilder
25 |
26 |
27 |
28 |
29 |
30 | com.android.ide.eclipse.adt.AndroidNature
31 | org.eclipse.jdt.core.javanature
32 |
33 |
34 |
--------------------------------------------------------------------------------
/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
16 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
15 |
16 |
24 |
25 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/src/com/yang/togglebutton/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.yang.togglebutton;
2 |
3 | import android.app.Activity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.RelativeLayout;
7 | import android.widget.TextView;
8 | import android.widget.Toast;
9 |
10 | public class MainActivity extends Activity {
11 |
12 | private CustomToggleButton mToggleButton;
13 | private TextView mTextView;
14 | private RelativeLayout mLayout;
15 |
16 | @Override
17 | protected void onCreate(Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 |
21 | mToggleButton = (CustomToggleButton) findViewById(R.id.toggle2);
22 | mToggleButton.setIOnToggleStateChangeListener(new IOnToggleStateChangeListener() {
23 |
24 | @Override
25 | public void onToggleStateChange(View view, boolean state) {
26 | //TODO 使用者重写此方法监听开关的相应处理
27 | if (state) { //开启状态
28 |
29 | Toast.makeText(MainActivity.this, "开关开启", 0).show();
30 | } else { //关闭状态
31 |
32 | Toast.makeText(MainActivity.this, "开关关闭", 0).show();
33 | }
34 | }
35 | });
36 | mToggleButton.setToggleState(false); //初始设置关闭状态
37 |
38 | mLayout = (RelativeLayout) findViewById(R.id.layout);
39 | mLayout.setOnClickListener(new View.OnClickListener() {
40 |
41 | @Override
42 | public void onClick(View v) {
43 |
44 | mToggleButton.setToggleState(!mToggleButton.getToggleState());
45 | }
46 | });
47 |
48 | mTextView = (TextView) findViewById(R.id.textView);
49 | mTextView.setOnClickListener(new View.OnClickListener() { //单击其他view来控制开关
50 |
51 | @Override
52 | public void onClick(View v) {
53 |
54 | mToggleButton.setToggleState(!mToggleButton.getToggleState());
55 | }
56 | });
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/com/yang/togglebutton/CustomToggleButton.java:
--------------------------------------------------------------------------------
1 | package com.yang.togglebutton;
2 |
3 | import android.content.Context;
4 | import android.graphics.Bitmap;
5 | import android.graphics.BitmapFactory;
6 | import android.graphics.Canvas;
7 | import android.util.AttributeSet;
8 | import android.view.MotionEvent;
9 | import android.view.View;
10 |
11 | /**
12 | * 仿ios圆形选择按钮的ToggleButton,左侧关闭、右侧开启
13 | * @author Jack
14 | * @date: 2014-10-30 上午8:45:02
15 | */
16 | public class CustomToggleButton extends View {
17 |
18 | /** 选中状态的横条背景 */
19 | private Bitmap mBackgroundOn;
20 | /** 未选中状态的横条背景 */
21 | private Bitmap mBackgroundOff;
22 | /** 滑块的背景 */
23 | private Bitmap mSlideBarBackground;
24 | /** 当前x轴的偏移量 */
25 | private int mCurrentX;
26 | /** 开关当前所处的开关状态,true为开启状态,false为关闭状态 */
27 | private boolean mCurrentState;
28 | /** 是否正在滑动中 */
29 | private boolean mIsSliding;
30 | /** 用户设置的监听事件 */
31 | private IOnToggleStateChangeListener mIOnToggleStateChangeListener;
32 |
33 | public CustomToggleButton(Context context, AttributeSet attrs) {
34 | super(context, attrs);
35 | initBitmap();
36 | }
37 |
38 | public CustomToggleButton(Context context) {
39 | super(context);
40 | initBitmap();
41 | }
42 |
43 | /**
44 | * 初始化开关所需的图片
45 | */
46 | private void initBitmap() {
47 | mBackgroundOn = BitmapFactory.decodeResource(getResources(), R.drawable.switch_on);
48 | mBackgroundOff = BitmapFactory.decodeResource(getResources(), R.drawable.switch_off);
49 | mSlideBarBackground = BitmapFactory.decodeResource(getResources(), R.drawable.switch_button);
50 | }
51 |
52 | /**
53 | * 设置当前控件的宽和高
54 | * @see android.view.View#onMeasure(int, int)
55 | * @param widthMeasureSpec 预留的宽度
56 | * @param heightMeasureSpec 预留的高度
57 | */
58 | @Override
59 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
60 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
61 | if (mBackgroundOn != null) { // 设置开关的宽和高
62 | setMeasuredDimension(mBackgroundOn.getWidth(), mBackgroundOn.getHeight());
63 | }
64 | }
65 |
66 | /**
67 | * 绘制当前的控件
68 | * @see android.view.View#onDraw(android.graphics.Canvas)
69 | * @param canvas
70 | */
71 | @Override
72 | protected void onDraw(Canvas canvas) { // 重新绘制不同位置的滑块
73 | // 初始化绘制背景
74 | if (!mCurrentState) {
75 | canvas.drawBitmap(mBackgroundOn, 0, 0, null);
76 | } else {
77 | canvas.drawBitmap(mBackgroundOff, 0, 0, null);
78 | }
79 | // 绘制滑动块
80 | if (mIsSliding) { // 滑块正在移动中
81 |
82 | int left = mCurrentX - mSlideBarBackground.getWidth() / 2;
83 | if (left < 0) {
84 | // 当前超出了左边界, 赋值为0
85 | left = 0;
86 | } else if (left > (mBackgroundOff.getWidth() - mSlideBarBackground.getWidth())) {
87 | // 当前超出了右边界, 赋值为: 背景的宽度 - 滑动块的宽度
88 | left = mBackgroundOff.getWidth() - mSlideBarBackground.getWidth();
89 | }
90 | canvas.drawBitmap(mSlideBarBackground, left, 0, null); // 绘制滑块
91 | } else { // 没有超界
92 | if (mCurrentState) { // 开启状态,绘制滑块开的状态
93 | canvas.drawBitmap(mBackgroundOn, 0, 0, null);
94 | canvas.drawBitmap(mSlideBarBackground, 0, 0, null);
95 | } else { // 绘制关的状态
96 |
97 | canvas.drawBitmap(mBackgroundOff, 0, 0, null);
98 | int left = mBackgroundOn.getWidth()
99 | - mSlideBarBackground.getWidth();
100 | canvas.drawBitmap(mSlideBarBackground, left, 0, null); // 绘制滑块
101 | }
102 | }
103 | }
104 |
105 | /**
106 | * 捕获用户操作的事件
107 | * @see android.view.View#onTouchEvent(android.view.MotionEvent)
108 | * @param event
109 | * @return
110 | */
111 | @Override
112 | public boolean onTouchEvent(MotionEvent event) {
113 | switch (event.getAction()) {
114 | case MotionEvent.ACTION_DOWN:
115 | mCurrentX = (int) event.getX();
116 | mIsSliding = true;
117 | break;
118 | case MotionEvent.ACTION_MOVE:
119 | mCurrentX = (int) event.getX();
120 | break;
121 | case MotionEvent.ACTION_UP:
122 | mIsSliding = false;
123 | mCurrentX = (int) event.getX();
124 | int center = mBackgroundOn.getWidth() / 2;
125 | // 当前最新的状态
126 | boolean state = mCurrentX < center;
127 | // 如果两个状态不一样并且监听事件不为null
128 | if (mCurrentState != state && mIOnToggleStateChangeListener != null) {
129 | // 调用用户的回调事件
130 | mIOnToggleStateChangeListener.onToggleStateChange(this, state);
131 | }
132 | mCurrentState = state;
133 | break;
134 | default:
135 | break;
136 | }
137 | invalidate(); // 调用onDraw方法重绘
138 | return true;
139 | }
140 |
141 | /**
142 | * 设置开关的状态
143 | * @author Jack
144 | * @param state
145 | */
146 | public void setToggleState(boolean state) {
147 | if (mIOnToggleStateChangeListener != null) {
148 | mIOnToggleStateChangeListener.onToggleStateChange(this, state);
149 | }
150 | mCurrentState = state;
151 | invalidate();
152 | }
153 |
154 | /**
155 | * 获取当前的开关状态
156 | * @author Jack
157 | * @return
158 | */
159 | public boolean getToggleState() {
160 | return mCurrentState;
161 | }
162 |
163 | /**
164 | * 设置开关的状态监听器的相应处理
165 | * @author Jack
166 | * @param listener
167 | */
168 | public void setIOnToggleStateChangeListener(IOnToggleStateChangeListener listener) {
169 | mIOnToggleStateChangeListener = listener;
170 | }
171 | }
--------------------------------------------------------------------------------