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();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章