android 防IOS开关SwitchButton

android 防IOS开关SwitchButton,第1张

概述项目重用到了IOS风格的一个开关SwitchButton,网上也找了蛮多,但是文件都比较大,所以自己写了一个。先看效果图github:https://github.com/xiebinJava/SwitchButton 下面直接上代码: packagecom.xie.brad.myswitchbutton.MyButton;importandroid.content.Context;impor

项目重用到了IOS风格的一个开关Switchbutton,网上也找了蛮多,但是文件都比较大,所以自己写了一个。

先看效果图

github:https://github.com/xiebinJava/SwitchButton

 

下面直接上代码:

 

package com.xIE.brad.myswitchbutton.Mybutton;import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.Path;import androID.graphics.RectF;import androID.util.AttributeSet;import androID.vIEw.MotionEvent;import androID.vIEw.VIEw;import com.xIE.brad.myswitchbutton.R;import java.util.Date;/** * Created by dell on 2017/10/20. */public class Switchbutton extends VIEw {    private Paint paint;    private float downX;    private float moveX;    private float BTmoveX;    private float dx;    private float maxX;    private boolean isOpen;    private Date date;    private long downTime;    private long upTime;    private int mWIDth;    private int mHeight;    private int sRight;    private float sBottom;    private float s@R_419_6823@;    private int stop;    private float sWIDth;    private float sHeight;    private float sCenterX;    private float sCenterY;    private Path sPath = new Path();    private int b@R_419_6823@;    private int btop;    private float bBottom;    private float bRight;    private float bWIDth;    private float bRadius;    private float bStrokWIDth;    private float sScale;    private float sScaleCenterX;    private OnSwitchListener switchListener;    private boolean firstState;    public Switchbutton(Context context) {        super(context, null);    }    public Switchbutton(Context context, AttributeSet attrs) {        super(context, attrs, 0);        paint = new Paint();        date = new Date();        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Switchbutton);        firstState = typedArray.getBoolean(R.styleable.Switchbutton_switch_state, false);        isOpen = firstState;    }    public Switchbutton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    // height = wIDth * 0.65 左右    @OverrIDe    protected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {        super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);        int wIDthSize = MeasureSpec.getSize(wIDthMeasureSpec);        int heightSize = (int) (wIDthSize * 0.65f);        setMeasuredDimension(wIDthSize, heightSize);    }    @OverrIDe    protected voID onLayout(boolean changed, int @R_419_6823@, int top, int right, int bottom) {        super.onLayout(changed, @R_419_6823@, top, right, bottom);    }    @OverrIDe    protected voID onSizeChanged(int w, int h, int olDW, int oldh) {        super.onSizeChanged(w, h, olDW, oldh);        mWIDth = w; // 视图自身宽度        mHeight = h; // 视图自身高度        s@R_419_6823@ = stop = 0; // 田径场 左和上的坐标        sRight = mWIDth; // 田径场 右占自身的全部        sBottom = mHeight * 0.8f; // 田径场底部 占全身的百分之八十, 下面预留百分之二十的空间画按钮阴影。        sWIDth = sRight - s@R_419_6823@; // 田径场的宽度        sHeight = sBottom - stop; // 田径场的高度        sCenterX = (sRight + s@R_419_6823@) / 2; // 田径场的X轴中心坐标        sCenterY = (sBottom + stop) / 2; // 田径场的Y轴中心坐标        RectF sRectF = new RectF(s@R_419_6823@, stop, sBottom, sBottom);        sPath.arcTo(sRectF, 90, 180);        sRectF.@R_419_6823@ = sRight - sBottom;        sRectF.right = sRight;        sPath.arcTo(sRectF, 270, 180);        sPath.close();    // path准备田径场的路径        b@R_419_6823@ = btop = 0;        bRight = bBottom = sBottom; // 和田径场同高,同宽的节奏, 没错包裹圆形的肯定是个正方形是小孩子都知道的。        bWIDth = bRight - b@R_419_6823@;        final float halfheightOfS = (sBottom - stop) / 2;        bRadius = halfheightOfS * 0.9f; // 按钮的半径        bStrokWIDth = 2 * (halfheightOfS - bRadius); // 按钮的边框        sScale = 1 - bStrokWIDth / sHeight; //替换之前的0.98<        sScaleCenterX = sWIDth - halfheightOfS;        if (isOpen){            BTmoveX = sWIDth - bWIDth / 2;        }else {            BTmoveX = bWIDth / 2;        }        maxX = sWIDth - bWIDth;    }    @OverrIDe    protected voID onDraw(Canvas canvas) {        super.onDraw(canvas);        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.FILL);        if (isOpen) {            paint.setcolor(getResources().getcolor(R.color.colorPrimary));        } else {            paint.setcolor(0xffcccccc);        }        canvas.drawPath(sPath, paint); // 画出田径场        canvas.save();        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.FILL);        paint.setcolor(getResources().getcolor(R.color.colorAccent));        canvas.drawCircle(BTmoveX, bWIDth / 2, bRadius, paint); // 按钮白底        canvas.restore();        paint.reset();    }    @OverrIDe    public boolean ontouchEvent(MotionEvent event) {        switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                downX = event.getX();                downTime = date.getTime();                firstState = isOpen;                break;            case MotionEvent.ACTION_MOVE:                moveX = event.getX();                dx = moveX - downX;                if (!isOpen) {                    if (dx < 0) {                        BTmoveX = bWIDth / 2;                        isOpen = false;                    } else if (dx > maxX) {                        BTmoveX = maxX + (bWIDth / 2);                        isOpen = true;                    } else {                        BTmoveX = dx + (bWIDth / 2);                    }                } else {                    if (dx > 0) {                        BTmoveX = maxX + bWIDth / 2;                        isOpen = true;                    } else if (Math.abs(dx) > maxX) {                        BTmoveX = bWIDth / 2;                        isOpen = false;                    } else {                        BTmoveX = maxX - Math.abs(dx) + (bWIDth / 2);                    }                }                invalIDate();                break;            case MotionEvent.ACTION_UP:                upTime = date.getTime();                if (Math.abs(dx) < 3 && upTime - downTime < 1000) {                    if (isOpen) {                        isOpen = false;                        BTmoveX = bWIDth / 2;                        firstState = false;                        switchListener.closebutton();                    } else {                        isOpen = true;                        BTmoveX = maxX + bWIDth / 2;                        firstState = true;                        switchListener.openbutton();                    }                } else {                    if (!isOpen) {                        if (dx < maxX / 2) {                            BTmoveX = bWIDth / 2;                            isOpen = false;                        } else {                            BTmoveX = maxX + bWIDth / 2;                            isOpen = true;                            firstState = true;                            switchListener.openbutton();                        }                    } else {                        if (Math.abs(dx) < maxX / 2 || dx > maxX / 2) {                            BTmoveX = maxX + bWIDth / 2;                            isOpen = true;                        } else {                            BTmoveX = bWIDth / 2;                            isOpen = false;                            firstState = false;                            switchListener.closebutton();                        }                    }                }                if (firstState){                    if (!isOpen){                        switchListener.closebutton();                    }                }else {                    if (isOpen){                        switchListener.openbutton();                    }                }                invalIDate();                break;        }        return true;    }    public voID setonSwitchListener(OnSwitchListener onSwitchListener) {        this.switchListener = onSwitchListener;    }    public interface OnSwitchListener {        voID openbutton();        voID closebutton();    }}

 

用法:

  <com.xIE.brad.myswitchbutton.Mybutton.Switchbutton                androID:ID="@+ID/switchbutton"                androID:layout_wIDth="100dp"                androID:layout_height="wrap_content"                androID:layout_centerInParent="true"                app:switch_state="true"/>

 

package com.xIE.brad.myswitchbutton;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.Widget.Toast;import com.xIE.brad.myswitchbutton.Mybutton.Switchbutton;public class MainActivity extends AppCompatActivity implements Switchbutton.OnSwitchListener {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        Switchbutton switchbutton = (Switchbutton) findVIEwByID(R.ID.switchbutton);        switchbutton.setonSwitchListener(this);    }    @OverrIDe    public voID openbutton() {        Toast.makeText(this,"开",Toast.LENGTH_SHORT).show();    }    @OverrIDe    public voID closebutton() {        Toast.makeText(this,"关",Toast.LENGTH_SHORT).show();    }}

代码写的蛮久了,最近闲暇,拿出来分享,代码比较乱,希望大家提出意见

总结

以上是内存溢出为你收集整理的android 防IOS开关SwitchButton全部内容,希望文章能够帮你解决android 防IOS开关SwitchButton所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1058801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存