十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Android开发仿IOS滑动开关实现代码

Android与iOS相比,ios好多控件都是自带的,而android需要使用自定义来实现。今天说的是ios的滑动开关,我层看到好多博客都是通过自定义ToggleButton实现的。这里我通过自定义view来实现他的效果。
首先在onsizechange里把2个半圆和一个矩形绘制出来。
width = w;
height = h;
left = top = 0;
right = width;
bottom = height * 0.8f;
cx = (right + left) / 2;
cy = (bottom + top) / 2;
RectF rectF = new RectF(left, top, bottom, bottom);
path.arcTo(rectF, 90, 180);
rectF.left = right - bottom;
rectF.right = right;
path.arcTo(rectF, 270, 180);
path.close();
circle_left = 0;
circle_right = bottom;
circle_width = circle_right - circle_left;
float circle_height = (bottom - top) / 2;
radius = circle_height * 0.9f;
borderwidth = (int) (2 * (circle_height - radius));
circle_cx = width - circle_height;