我是靠谱客的博主 超级黑夜,这篇文章主要介绍Android studio实现滑动开关,现在分享给大家,希望可以做个参考。

大家好,今天刚学会使用Android Studio实现滑动开关的效果,自己感觉还可以,和大家分享一下,如果觉得可以的可以拿去,然后再给我点个赞,谢谢。本人也是学Android studiok开发不久,是个菜鸟,各位大佬觉得有不好的地方,可以讨论一下,共同学习,一起进步。

实现效果

下面是代码,代码写的比较粗糙,注释没有写的很好,需要用的可以自己移植一下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package com.example.biansheng2; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import androidx.annotation.Nullable; /** * 用于选择手动模式还是路线规划模式 */ public class ModelSelectView extends View { Paint backPaint = new Paint();//背景画笔 Paint facePaint = new Paint();//表面视图画笔 Paint colorPaint1 = new Paint();//绘制红色和蓝色 Paint colorPaint2 = new Paint();//绘制橙色 Paint colorPaint3 = new Paint();//绘制灰色 Paint handFont = new Paint();//手动控制模式字体 Paint routeFont = new Paint();//路线规划模式字体 boolean modelFlag = false;//false表示为手动控制模式,true表示为路线规划模式 RectF mRectF1 = new RectF(150, 840, 580, 960); int x = 150, y = 900;//得到手指再控件上移动的坐标 public ModelSelectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); paintInit(); canvas.drawRect(mRectF1, backPaint); canvas.drawCircle(150, 900, 60, backPaint); canvas.drawCircle(580, 900, 60, backPaint); if (!modelFlag){ canvas.drawCircle(150, 900, 60, facePaint); canvas.drawRect(150,840,x,960,facePaint); //设置手动控制字体可见 handFont.setAlpha(255); //设置路线规划字体不可见 routeFont.setAlpha(0); }else { canvas.drawCircle(580, 900, 60, facePaint); canvas.drawRect(x,840,580,960,facePaint); //设置手动控制字体不可见 handFont.setAlpha(0); //设置路线规划字体可见 routeFont.setAlpha(255); } canvas.drawText("手动控制",250,915,handFont); canvas.drawText("路线规划",250,915,routeFont); canvas.drawCircle(x, y, 44, backPaint); canvas.drawCircle(x, y, 40, colorPaint2); canvas.drawCircle(x, y, 47, colorPaint3); } private void paintInit() { if (!modelFlag) { backPaint.setAntiAlias(true); backPaint.setColor(Color.parseColor("#ff00b5eb")); facePaint.setColor(Color.parseColor("#D81B60")); facePaint.setAntiAlias(true); }else { backPaint.setAntiAlias(true); backPaint.setColor(Color.parseColor("#D81B60")); facePaint.setColor(Color.parseColor("#ff00b5eb")); facePaint.setAntiAlias(true); } colorPaint2.setAntiAlias(true); colorPaint2.setColor(Color.parseColor("#FF6407")); colorPaint1.setAntiAlias(true); colorPaint1.setColor(Color.BLUE); colorPaint3.setColor(Color.WHITE); colorPaint3.setAntiAlias(true); colorPaint3.setStyle(Paint.Style.STROKE); colorPaint3.setStrokeWidth(5); Typeface font = Typeface.create(Typeface.DEFAULT_BOLD,Typeface.BOLD_ITALIC); handFont.setTypeface(font); handFont.setColor(Color.WHITE); handFont.setAntiAlias(true); handFont.setTextSize(50); routeFont.setTypeface(font); routeFont.setColor(Color.WHITE); routeFont.setAntiAlias(true); routeFont.setTextSize(50); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: int getX = (int) event.getX(); int getY = (int) event.getY(); //判断得到的坐标是否在视图范围之内 if (getX >= 150 && getX <= 580 && getY >= 840 && getY <= 960) { x = getX; y = 900; } break; case MotionEvent.ACTION_UP: int getX1 = (int) event.getX(); int getY1 = (int) event.getY(); //判断是否在控件内 if (getY1 >= 810 && getY1 <= 990) { if (getX1 <= 280) { x = 150; modelFlag = false; } else if (getX1 >= 443) { x = 580; modelFlag = true; } else { if (!modelFlag) { x = 150; modelFlag = false; } else { x = 580; modelFlag = true; } } } break; } invalidate();//刷新界面 return true; } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是超级黑夜最近收集整理的关于Android studio实现滑动开关的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(150)

评论列表共有 0 条评论

立即
投稿
返回
顶部