SwitchView

简介: 今天写了一个很简单的开关控件,仿华为手机的,先看效果图...这是华为的001.png这是我的003.gif代码 很简单的package org.

今天写了一个很简单的开关控件,仿华为手机的,先看效果图

...

这是华为的

001.png

这是我的

003.gif

代码 很简单的

<pre>

package org.alex.switchview;
/**
* 作者:Alex
* 时间:2016/10/21 12:59
* 简述:
*/
public class SwitchView extends View implements View.OnClickListener {
private final int defaultLightColor = 0xFF4BD763;
private final int defaultDarkColor = 0xFFE3E3E3;
private final int defaultCircleSlideColor = 0xFFFFFFFF;
private final float defaultCircleScale = 0.90F;
private final int defaultOpenDuration = 300;
private final int defaultCloseDuration = 300;
private Paint paint;
/**
* 左右两端是半圆,中间是矩形的跑道
*/
private Path runwayPath;
/**
* 中间的 小滑块
*/
private Path circleSlidePath;
/**
* 中间的 小滑块
*/
private RectF circleSlideRectF;
private int lightColor;
private int darkColor;
private int circleSlideColor;
private boolean isOpened;
private int width, height;
private int viewBottom, viewTop, viewLeft, viewRight;
private float runwayHeight;
/**
* 中间小滑块 占 滑动开关的 高度比
*/
private float circleSlideScale;
private float circleSlideHeight;
private float circleSlideLeft, circleSlideTop, circleSlideRight, circleSlideBottom;

private int openDuration, closeDuration;
private boolean isCanVisibleDrawing;
/\*\*
 \* 小滑块 和 外层 跑道之间的 间距
 \*/
private float circleSlideOutGap;
/\*\*
 \* 打开开关 动画
 \*/
private ValueAnimator openAnimator;
/\*\*
 \* 关闭开关 动画
 \*/
private ValueAnimator closeAnimator;
/\*\*
 \* 0 表示在最左端
 \* 1 表示在最右端
 \*/
private float progress;
/\*\*
 \* 小滑块 滑动的 距离
 \*/
private float circleSlideRunLength;

private int swStatus;
private OnChangeListener onChangeListener;

public SwitchView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context, attrs);
}

private void initView(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchView);
    lightColor = typedArray.getColor(R.styleable.SwitchView_sv_lightColor, defaultLightColor);
    darkColor = typedArray.getColor(R.styleable.SwitchView_sv_darkColor, defaultDarkColor);
    circleSlideColor = typedArray.getColor(R.styleable.SwitchView_sv_circleSlideColor, defaultCircleSlideColor);
    circleSlideScale = typedArray.getFloat(R.styleable.SwitchView_sv_circleSlideScale, defaultCircleScale);
    openDuration = typedArray.getInt(R.styleable.SwitchView_sv_openDuration, defaultOpenDuration);
    closeDuration = typedArray.getInt(R.styleable.SwitchView_sv_closeDuration, defaultCloseDuration);
    isOpened = typedArray.getBoolean(R.styleable.SwitchView_sv_isOpened, false);
    typedArray.recycle();
    progress = isOpened ? 1F : 0F;
    swStatus = SwStatus.isNone;
    isCanVisibleDrawing = false;
    setLayerType(LAYER_TYPE_SOFTWARE, null);
    paint = new Paint();
    runwayPath = new Path();
    circleSlidePath = new Path();
    circleSlideRectF = new RectF();
    /\*关联动画\*/
    openAnimator = ValueAnimator.ofFloat(0f, 1.0f);
    closeAnimator = ValueAnimator.ofFloat(1.0f, 0.0f);

    closeAnimator.setDuration(closeDuration);
    closeAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    closeAnimator.addUpdateListener(new MyAnimatorUpdateListener("closeAnimator"));

    openAnimator.setDuration(openDuration);
    openAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
    openAnimator.addUpdateListener(new MyAnimatorUpdateListener("openAnimator"));
    setOnClickListener(this);
}

private final class MyAnimatorUpdateListener implements ValueAnimator.AnimatorUpdateListener {
    private String tag;

    public MyAnimatorUpdateListener(String tag) {
        this.tag = tag;
    }

    /\*\*
     \* <p>Notifies the occurrence of another frame of the animation.</p>
     \*
     \* @param animation The animation which was repeated.
     \*/
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        progress = (float) animation.getAnimatedValue();
        if ("openAnimator".equals(tag)) {
            swStatus = progress > 0.99F ? SwStatus.isNone : SwStatus.is2Open;
        } else if ("closeAnimator".equals(tag)) {
            swStatus = progress < 0.01F ? SwStatus.isNone : SwStatus.is2Close;
        }
        invalidate();
    }
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (!isCanVisibleDrawing) {
        return;
    }
    paint.setAntiAlias(true);
    final boolean isOn = isOpened;
    /\*画背景\*/
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(isOn ? lightColor : darkColor);
    canvas.drawPath(runwayPath, paint);
    //canvas.save();

    /\*画 中间的 圆形的 滑块\*/
    //canvas.scale(circleSlideScale, circleSlideScale, circleSlideHeight / 2, circleSlideHeight / 2);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(circleSlideColor);
    calcCircleSlidePath();
    canvas.drawPath(circleSlidePath, paint);

}

private void calcCircleSlidePath() {
    circleSlidePath.reset();
    if (swStatus == SwStatus.is2Open) {
        slowly4Open();
    } else if (swStatus == SwStatus.is2Close) {
        slowly4Close();
    } else {
        circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + progress \* circleSlideRunLength;
        circleSlideRectF.right = circleSlideRight - circleSlideOutGap + progress \* circleSlideRunLength;
        circleSlidePath.arcTo(circleSlideRectF, 90, 180);
        circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + progress \* circleSlideRunLength;
        circleSlideRectF.right = circleSlideRight - circleSlideOutGap + progress \* circleSlideRunLength;
        circleSlidePath.arcTo(circleSlideRectF, 270, 180);
    }
    circleSlidePath.close();
}

private void slowly4Open() {
    circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + progress \* circleSlideRunLength;
    circleSlideRectF.right = circleSlideRight - circleSlideOutGap + progress \* circleSlideRunLength;
    circleSlidePath.arcTo(circleSlideRectF, 90, 180);
    if (progress < 0.8) {
        circleSlideRectF.right = circleSlideRight - circleSlideOutGap + (progress + 0.2F) \* circleSlideRunLength;
    } else if ((progress >= 0.8) && (progress < 0.99)) {
        circleSlideRectF.right = circleSlideRight - circleSlideOutGap + (progress + (1 - progress)) \* circleSlideRunLength;
    } else {
        circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + progress \* circleSlideRunLength;
        circleSlideRectF.right = circleSlideRight - circleSlideOutGap + progress \* circleSlideRunLength;
    }
    circleSlidePath.arcTo(circleSlideRectF, -90, 180);
}

private void slowly4Close() {
    circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + progress \* circleSlideRunLength;
    circleSlideRectF.right = circleSlideRight - circleSlideOutGap + progress \* circleSlideRunLength;
    circleSlidePath.arcTo(circleSlideRectF, -90, 180);

    if (progress > 0.2) {
        circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + (progress - 0.2F) \* circleSlideRunLength;
    } else if ((progress <= 0.2) && (progress > 0.01)) {
        circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + (progress \* 0.2F) \* circleSlideRunLength;
    } else {
        circleSlideRectF.left = circleSlideLeft + circleSlideOutGap + progress \* circleSlideRunLength;
        circleSlideRectF.right = circleSlideRight - circleSlideOutGap + progress \* circleSlideRunLength;
    }
    circleSlidePath.arcTo(circleSlideRectF, 90, 180);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    width = w;
    height = h;
    isCanVisibleDrawing = width > getPaddingLeft() + getPaddingRight() && height > getPaddingTop() + getPaddingBottom();
    if (!isCanVisibleDrawing) {
        return;
    }
    viewLeft = getPaddingLeft();
    viewRight = getWidth() - getPaddingRight();
    viewTop = getPaddingTop();
    viewBottom = getHeight() - getPaddingBottom();
    runwayHeight = viewBottom - viewTop;

    circleSlideLeft = viewLeft;
    circleSlideTop = viewTop;
    circleSlideBottom = viewBottom;
    circleSlideHeight = viewBottom - viewTop;
    circleSlideRight = viewLeft + circleSlideHeight;

    runwayPath.reset();
    /\*画跑道需要的矩形\*/
    RectF runwayRectF = new RectF();
    runwayRectF.top = viewTop;
    runwayRectF.bottom = viewBottom;
    runwayRectF.left = viewLeft;
    runwayRectF.right = viewLeft + runwayHeight;
    /\*画 跑道的左半 半圆\*/
    runwayPath.arcTo(runwayRectF, 90, 180);
    runwayRectF.left = viewRight - runwayHeight;
    runwayRectF.right = viewRight;
    /\*画 跑道的右半 半圆\*/
    runwayPath.arcTo(runwayRectF, -90, 180);
    runwayPath.close();

    circleSlideOutGap = height \* (1 - circleSlideScale) \* 0.5F;
    circleSlideRectF.left = circleSlideLeft + circleSlideOutGap;
    circleSlideRectF.right = circleSlideRight - circleSlideOutGap;
    circleSlideRectF.top = circleSlideTop + circleSlideOutGap;
    circleSlideRectF.bottom = circleSlideBottom - circleSlideOutGap;

    circleSlideRunLength = width - circleSlideOutGap \* 2 - (circleSlideRectF.right - circleSlideRectF.left);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int resultWidth;
    if (widthMode == MeasureSpec.EXACTLY) {
        resultWidth = widthSize;
    } else {
        resultWidth = (int) (56 \* getResources().getDisplayMetrics().density + 0.5f) + getPaddingLeft() + getPaddingRight();
        if (widthMode == MeasureSpec.AT_MOST) {
            resultWidth = Math.min(resultWidth, widthSize);
        }
    }

    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int resultHeight;
    if (heightMode == MeasureSpec.EXACTLY) {
        resultHeight = heightSize;
    } else {
        int selfExpectedResultHeight = (int) (resultWidth \* 0.68F) + getPaddingTop() + getPaddingBottom();
        resultHeight = selfExpectedResultHeight;
        if (heightMode == MeasureSpec.AT_MOST) {
            resultHeight = Math.min(resultHeight, heightSize);
        }
    }
    setMeasuredDimension(resultWidth, resultHeight);
}


/\*\*
 \* Called when a view has been clicked.
 \*
 \* @param v The view that was clicked.
 \*/
@Override
public void onClick(View v) {
    if (closeAnimator.isRunning() || openAnimator.isRunning()) {
        return;
    }
    if (isOpened) {
        swStatus = SwStatus.is2Close;
        closeAnimator.start();
        if (onChangeListener != null) {
            onChangeListener.onChange(this, false);
        }
    } else {
        swStatus = SwStatus.is2Open;
        openAnimator.start();
        if (onChangeListener != null) {
            onChangeListener.onChange(this, true);
        }
    }
    isOpened = !isOpened;
}


public SwitchView onChangeListener(OnChangeListener onChangeListener) {
    this.onChangeListener = onChangeListener;
    return this;
}

public interface OnChangeListener {
    void onChange(SwitchView switchView, boolean isOpen);
}


private final class SwStatus {
    /\*\*
     \* 初始状态
     \*/
    private final static int isNone = 0;
    /\*\*
     \* 初始状态
     \*/
    private final static int is2Open = 1;
    /\*\*
     \* 初始状态
     \*/
    private final static int is2Close = 2;
}

}

</pre>

SwitchView

目录
相关文章
云计算公网质量白皮书
随着互联网的发展,网络已经融入了整个社会发展的进程,如同血液 贯通人类文明的五脏六腑。一旦网络出现故障,将给社会的政治、经 济、文化造成重大损失。 2021年10月4日,Facebook在地球上”消失”了6小时,市值跌 掉3000亿人民币,给客户造成的损失更无法估量。 2021年7月22日,Akamai的网络故障长达1小时,导致包括银行和 航空公司在内的大量美国本土企业官网无法访问。 当前,国内很多行业企业在一带一路政策的指引下,都在积极布局国 际化和出海战略,向海外用户提供优质的产品和服务。公网作为企业 出海最关键的承载基础设施之一,其质量的好坏会直接影响应用(尤 其是互联网应用)的用户体验
云计算公网质量白皮书
|
SQL 数据采集 数据挖掘
数据湖构建DLF数据探索快速入门-淘宝用户行为分析
本教程通过使⽤数据湖构建(DLF)产品对于淘宝⽤户⾏为样例数据的分析,介绍DLF产品的数据发现和数据探索功能。教程内容包括:1. 服务开通:开通阿⾥云账号及DLF/OSS相关服务2. 样例数据集下载和导⼊:下载样例数据(csv⽂件),并上传⾄OSS3. DLF数据发现:使⽤DLF⾃动识别⽂件Schema并创建元数据表4. DLF数据探索:使⽤DLF数据探索,对⽤户⾏为进⾏分析,包括⽤户活跃度、漏⽃模型等
数据湖构建DLF数据探索快速入门-淘宝用户行为分析
|
消息中间件 存储 调度
抖音短视频系统开发,消息机制的原理细节处理
对于Android抖音短视频系统开发来说,Binder和Handler是两大利剑,分别实现了进程间和线程间的通讯。Android的消息机制
1523 0
抖音短视频系统开发,消息机制的原理细节处理
|
关系型数据库 MySQL Shell
MYSQL常用语句
1 常用命令 $后为Shell环境命令,>后为MySQL命令。 第一步,连接数据库。
289 0
|
弹性计算 安全 NoSQL
为什么要从经典网络迁移到专有网络VPC?
经典网络是阿里云上早期的一个简便易用但是也存在不少问题的网络产品,阿里云正在努力推动使用新型的功能强大、用户自主可控的专有网络VPC来替代经典网络产品。本文简单列举了一些经典网络的问题和专有网络的优势,希望对推动经典网络迁移专有网络有一定的帮助。
3693 0
|
监控 Python Windows
使用PyInstaller打包Python程序
使用PyInstaller打包Python程序 PyInstaller介绍: PyInstaller是一个能将Python程序转换成单个可执行文件的程序, 操作系统支持Windows, Linux, Mac OS X, Solaris和AIX。
4087 0
|
图形学
Unity3D深入浅出 - 脚本开发基础(Scripts)
常用脚本事件: Update:每帧调用一次 Start:在第一次Update执行前调用 Awake:脚本实例在创建时调用 FixedUpdate:每个固定物理时间间隔调用一次 LateUpdate:每帧调用一次,在Update之后 MonoBehaviour类: MonoBehaviour类是uniyt中非常重要的类,定义了基本的脚本行为,所有脚本类均需要从它直接或间接继承。
5952 0
|
Linux
SecureCRT 设置自动超时断开连接与中文乱码解决方案
SecureCRT 设置自动超时断开连接与中文乱码解决方案
3062 0
|
SQL 数据库
SQL Server 多表数据增量获取和发布 3
程序结构 新建一个窗体应用程序,新增三个类库,实现各个层次责任分离 BLL 业务逻辑层 Common 公共帮助类层 Models 模型层 SqlMonitoring SQL监视器程序 image.png 程序设计思路 大概的设计思路是这样的: 1、因为是多表数据增量获取,首先通过配置CDC已经完成多表的捕获配置。
1197 0