Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4d9581c

Browse files
delldell
dell
authored and
dell
committed
10.24 添加了打开和关闭的回调
1 parent c5fad34 commit 4d9581c

File tree

5 files changed

+72
-24
lines changed

5 files changed

+72
-24
lines changed

‎.idea/markdown-navigator/profiles_settings.xml‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/misc.xml‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
package com.xie.brad.myswitchbutton;
22

3-
import android.support.v7.app.AppCompatActivity;
43
import android.os.Bundle;
4+
import android.support.v7.app.AppCompatActivity;
5+
import android.widget.Toast;
6+
7+
import com.xie.brad.myswitchbutton.MyButton.SwitchButton;
58

6-
public class MainActivity extends AppCompatActivity {
9+
public class MainActivity extends AppCompatActivity implementsSwitchButton.OnSwitchListener{
710

811
@Override
912
protected void onCreate(Bundle savedInstanceState) {
1013
super.onCreate(savedInstanceState);
1114
setContentView(R.layout.activity_main);
15+
SwitchButton switchButton = (SwitchButton) findViewById(R.id.switchbutton);
16+
switchButton.setOnSwitchListener(this);
17+
}
18+
19+
@Override
20+
public void openbutton() {
21+
Toast.makeText(this,"开",Toast.LENGTH_SHORT).show();
22+
}
23+
24+
@Override
25+
public void closebutton() {
26+
Toast.makeText(this,"关",Toast.LENGTH_SHORT).show();
1227
}
1328
}

‎app/src/main/java/com/xie/brad/myswitchbutton/MyButton/SwitchButton.java‎

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public class SwitchButton extends View {
6060
private float bStrokWidth;
6161
private float sScale;
6262
private float sScaleCenterX;
63+
private OnSwitchListener switchListener;
64+
private boolean firstState;
6365

6466
public SwitchButton(Context context) {
6567
super(context, null);
@@ -83,6 +85,7 @@ public SwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
8385
private void init(Context context) {
8486
paint = new Paint();
8587
date = new Date();
88+
firstState = false;
8689
}
8790

8891
//自己确定大小~ 截图量了一下 算上阴影宽高比例是 149:92 。即 height = width * 0.65 左右
@@ -100,8 +103,8 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
100103
}
101104

102105

103-
104-
@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {
106+
@Override
107+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
105108
super.onSizeChanged(w, h, oldw, oldh);
106109
mWidth = w; // 视图自身宽度
107110
mHeight = h; // 视图自身高度
@@ -118,7 +121,6 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
118121
sRectF.right = sRight;
119122
sPath.arcTo(sRectF, 270, 180);
120123
sPath.close(); // path准备田径场的路径
121-
122124
bLeft = bTop = 0;
123125
bRight = bBottom = sBottom; // 和田径场同高,同宽的节奏, 没错包裹圆形的肯定是个正方形是小孩子都知道的。
124126
bWidth = bRight - bLeft;
@@ -132,15 +134,14 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
132134
}
133135

134136

135-
136137
@Override
137138
protected void onDraw(Canvas canvas) {
138139
super.onDraw(canvas);
139140
paint.setAntiAlias(true);
140141
paint.setStyle(Paint.Style.FILL);
141-
if (isOpen){
142+
if (isOpen){
142143
paint.setColor(getResources().getColor(R.color.colorPrimary));
143-
}else {
144+
}else {
144145
paint.setColor(0xffcccccc);
145146
}
146147
canvas.drawPath(sPath, paint); // 画出田径场
@@ -159,29 +160,30 @@ public boolean onTouchEvent(MotionEvent event) {
159160
case MotionEvent.ACTION_DOWN:
160161
downX = event.getX();
161162
downTime = date.getTime();
163+
firstState = isOpen;
162164
break;
163165
case MotionEvent.ACTION_MOVE:
164166
moveX = event.getX();
165167
dx = moveX - downX;
166168
if (!isOpen) {
167169
if (dx < 0) {
168-
BTmoveX = bWidth / 2;
170+
BTmoveX = bWidth / 2;
169171
isOpen = false;
170172
} else if (dx > maxX) {
171-
BTmoveX = maxX+ (bWidth / 2);
173+
BTmoveX = maxX+ (bWidth / 2);
172174
isOpen = true;
173175
} else {
174-
BTmoveX = dx+(bWidth / 2);
176+
BTmoveX = dx + (bWidth / 2);
175177
}
176178
} else {
177179
if (dx > 0) {
178-
BTmoveX = maxX+ bWidth / 2;
180+
BTmoveX = maxX+ bWidth / 2;
179181
isOpen = true;
180182
} else if (Math.abs(dx) > maxX) {
181-
BTmoveX = bWidth / 2;
183+
BTmoveX = bWidth / 2;
182184
isOpen = false;
183185
} else {
184-
BTmoveX = maxX-Math.abs(dx)+(bWidth / 2);
186+
BTmoveX = maxX - Math.abs(dx) + (bWidth / 2);
185187
}
186188
}
187189
invalidate();
@@ -191,36 +193,63 @@ public boolean onTouchEvent(MotionEvent event) {
191193
if (Math.abs(dx) < 3 && upTime - downTime < 1000) {
192194
if (isOpen) {
193195
isOpen = false;
194-
BTmoveX = bWidth / 2;
196+
BTmoveX = bWidth / 2;
197+
firstState = false;
198+
switchListener.closebutton();
195199
} else {
196200
isOpen = true;
197-
BTmoveX = maxX+bWidth / 2;
201+
BTmoveX = maxX + bWidth / 2;
202+
firstState = true;
203+
switchListener.openbutton();
198204
}
199205
} else {
200206
if (!isOpen) {
201207
if (dx < maxX / 2) {
202-
BTmoveX = bWidth / 2;
208+
BTmoveX = bWidth / 2;
203209
isOpen = false;
204210
} else {
205-
BTmoveX = maxX+bWidth / 2;
211+
BTmoveX = maxX + bWidth / 2;
206212
isOpen = true;
213+
firstState = true;
214+
switchListener.openbutton();
207215
}
208-
}else {
209-
if (Math.abs(dx) < maxX / 2|| dx>maxX/2) {
210-
BTmoveX = maxX+bWidth / 2;
216+
}else {
217+
if (Math.abs(dx) < maxX / 2|| dx > maxX / 2) {
218+
BTmoveX = maxX + bWidth / 2;
211219
isOpen = true;
212220
} else {
213-
BTmoveX = bWidth / 2;
221+
BTmoveX = bWidth / 2;
214222
isOpen = false;
223+
firstState = false;
224+
switchListener.closebutton();
215225
}
216226
}
217227
}
228+
if (firstState){
229+
if (!isOpen){
230+
switchListener.closebutton();
231+
}
232+
}else {
233+
if (isOpen){
234+
switchListener.openbutton();
235+
}
236+
}
218237
invalidate();
219238
break;
220239
}
221240
return true;
222241
}
223242

224243

244+
public void setOnSwitchListener(OnSwitchListener onSwitchListener) {
245+
this.switchListener = onSwitchListener;
246+
}
247+
248+
public interface OnSwitchListener {
249+
void openbutton();
250+
251+
void closebutton();
252+
}
253+
225254

226255
}

‎app/src/main/res/layout/activity_main.xml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
>
88

99
<com.xie.brad.myswitchbutton.MyButton.SwitchButton
10-
android:layout_width="100dp"
10+
android:id="@+id/switchbutton"
11+
android:layout_width="150dp"
1112
android:layout_height="wrap_content"
1213
android:layout_centerInParent="true" />
1314
</RelativeLayout>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /