Android 自定义Switch开关按钮的样式 仿ios开关按钮 《二》(从入门到巅峰) - Go语言中文社区

Android 自定义Switch开关按钮的样式 仿ios开关按钮 《二》(从入门到巅峰)


uploading.4e448015.gif转存失败重新上传取消

1.通过2个图片切换实现

2.通过shape,touch事件实现

https://blog.csdn.net/languobeibei/article/details/70256154

3.自定义

第一种方式:

<ImageView
    android:id="@+id/aiqa_voice_reply_setting_iv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|right"
    android:layout_marginRight="14dp"
    android:src="@mipmap/ic_aiqa_setting_open" />
assistantSetInfo.setVoice_wake(1);
ivWakeUpSetting.setImageResource(R.mipmap.ic_aiqa_setting_open);

 

第三种方式:

public SwitchButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SwitchView, defStyleAttr, R.style.def_switch_view);
    int indexCount = typedArray.getIndexCount();
    for (int i = 0; i < indexCount; i++) {
        int attr = typedArray.getIndex(i);
        if (attr == R.styleable.SwitchView_switch_bg_color) {
            switchViewBgColor = typedArray.getColor(attr, Color.BLACK);
        } else if (attr == R.styleable.SwitchView_switch_ball_color) {
            switchViewBallColor = typedArray.getColor(attr, Color.BLACK);
        }
    }
    typedArray.recycle();
    initData();
}

 

private void initData() {

    greyColor = switchViewBgColor;
    greenColor = Color.parseColor("#11d59c");

    mBallPaint = createPaint(switchViewBallColor, 0, Paint.Style.FILL, 0);
    mBgPaint = createPaint(switchViewBgColor, 0, Paint.Style.FILL, 0);
    mCurrentState = State.CLOSE;
    setOnClickListener(this);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {

    mViewHeight = h;
    mViewWidth = w;

    // 默认描边宽度是控件宽度的1/30, 比如控件宽度是120dp, 描边宽度就是4dp
    switchViewStrockWidth = w * 1.0f / 30;

    mStrokeRadius = mViewHeight / 2;
    mSolidRadius = (mViewHeight - 2 * switchViewStrockWidth) / 2;
    BALL_X_RIGHT = mViewWidth - mStrokeRadius;

    mSwitchBallx = mStrokeRadius;
    mBgStrokeRectF = new RectF(0, 0, mViewWidth, mViewHeight);

    if (first) {
        if (mCurrentState == State.OPEN) {
            animate(mStrokeRadius, BALL_X_RIGHT, greyColor, greenColor,100);
        }
        first = false;
    }

}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);

    int measureWidth;
    int measureHeight;

    switch (widthMode) {
        case MeasureSpec.UNSPECIFIED:
        case MeasureSpec.AT_MOST://wrap_content
            measureWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEF_W, getResources().getDisplayMetrics());
            widthMeasureSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.EXACTLY);
            break;
        case MeasureSpec.EXACTLY:
            break;
    }

    switch (heightMode) {
        case MeasureSpec.UNSPECIFIED:
        case MeasureSpec.AT_MOST://wrap_content
            measureHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEF_H, getResources().getDisplayMetrics());
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(measureHeight, MeasureSpec.EXACTLY);
            break;
        case MeasureSpec.EXACTLY:
            break;

    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
    drawSwitchBg(canvas);
    drawSwitchBall(canvas);
}
@Override
public void onClick(View v) {
    mCurrentState = (mCurrentState == State.CLOSE ? State.OPEN : State.CLOSE);
    //绿色   #1AAC19
    //灰色   #999999
    if (mCurrentState == State.CLOSE) {
        animate(BALL_X_RIGHT, mStrokeRadius, greenColor, greyColor,200);
    } else {
        animate(mStrokeRadius, BALL_X_RIGHT, greyColor, greenColor,200);
    }
    if (mOnCheckedChangeListener != null) {
        if (mCurrentState == State.OPEN) {
            mOnCheckedChangeListener.onCheckedChanged(this, true);
        } else {
            mOnCheckedChangeListener.onCheckedChanged(this, false);
        }
    }
}
private void animate(int from, int to, int startColor, int endColor,int time) {
    ValueAnimator translate = ValueAnimator.ofFloat(from, to);
    translate.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            mSwitchBallx = ((float) animation.getAnimatedValue());
            postInvalidate();
        }
    });

    ValueAnimator color = ValueAnimator.ofObject(new ArgbEvaluator(), startColor, endColor);
    color.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            switchViewBgColor = ((int) animation.getAnimatedValue());
            mBgPaint.setColor(switchViewBgColor);
            postInvalidate();
        }
    });

    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(translate, color);
    animatorSet.setDuration(time);
    animatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            setClickable(false);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            setClickable(true);
        }
    });
    animatorSet.start();
}
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/WHB20081815/article/details/105127564
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-06 10:48:07
  • 阅读 ( 3748 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢