天天看点

android自定义开关SwitchView

先上传两张图片

android自定义开关SwitchView
android自定义开关SwitchView

代码实现:

package com.example.animation.view;

import com.example.animation.R;

import com.example.animation.util.CommonUtil;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.RectF;

import android.util.AttributeSet;

import android.view.MotionEvent;

import android.view.View;

//自定义开关

public class SwitchView extends View {

// 开关圆的图片

private Bitmap circleBitmap, onBitmap, offBitmap;

private float downX;

private float distanceX;

private float mBitmapWidth;

// 默认开关是开着的

public State mState = State.ON;

private int mWidth, mHeihgt, radius, mStrokeWidth;

public enum State {

ON, OFF

}

public SwitchView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

initView(context);

}

public SwitchView(Context context, AttributeSet attrs) {

super(context, attrs);

initView(context);

}

public SwitchView(Context context) {

super(context);

initView(context);

}

private void initView(Context context) {

radius = CommonUtil.dpToPx(context.getResources(), 20);

mStrokeWidth = CommonUtil.dpToPx(context.getResources(), 1);

circleBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.yuan);

onBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.on_btn);

offBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.off_btn);

}

@Override

public boolean onTouchEvent(MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

downX = event.getX();

distanceX = 0;

break;

case MotionEvent.ACTION_MOVE:

float moveX = event.getX();

distanceX = moveX - downX;

invalidate();

break;

case MotionEvent.ACTION_UP:

case MotionEvent.ACTION_CANCEL:

downX = 0;

// need to reset the state

// 滑动的距离大于1/2个距离,滑动的距离+1/2个圆的距离》1/2width

float middle = distanceX + mBitmapWidth / 2;

if (middle >= (mWidth / 2)) {

// trun on

changeStateOn();

} else {

// turn off

changeStateOff();

}

break;

default:

break;

}

return true;

}

private void changeStateOff() {

distanceX = 0;

if (mState != State.OFF && mListener != null) {

mListener.onStateChanged(State.OFF);

}

mState = State.OFF;

invalidate();

}

private void changeStateOn() {

distanceX = mWidth - mBitmapWidth;

if (mState != State.ON && mListener != null) {

mListener.onStateChanged(State.ON);

}

mState = State.ON;

invalidate();

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// mWidth = this.getMeasuredWidth();

mWidth = onBitmap.getWidth();

mHeihgt = onBitmap.getHeight();

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

@Override

protected void onDraw(Canvas canvas) {

mBitmapWidth = circleBitmap.getWidth();

float right = mWidth - mBitmapWidth;

// if right == turn on

if (mState == State.ON) {

distanceX = right + distanceX;

}

if (distanceX < 0) {

distanceX = 0;

}

if (distanceX > right) {

distanceX = right;

}

Paint paint = new Paint();

canvas.drawBitmap(onBitmap, 0, 0, paint);

// canvas.drawBitmap(offBitmap, distanceX, 0, paint);

canvas.drawBitmap(circleBitmap, distanceX, 0, paint);

// 画圆角矩形

paint.setStyle(Paint.Style.FILL);// 充满

paint.setColor(Color.WHITE);

paint.setAntiAlias(true);// 设置画笔的锯齿效果

RectF oval = new RectF(distanceX, 0, mWidth, mHeihgt);// 设置个新的长方形

canvas.drawRoundRect(oval, radius, radius, paint);// 第二个参数是x半径,第三个参数是y半径

paint.setStyle(Paint.Style.STROKE);

paint.setStrokeWidth(mStrokeWidth);

paint.setColor(Color.GRAY);

paint.setAntiAlias(true);

RectF oval1 = new RectF(distanceX, 0, mWidth, mHeihgt);// 设置个新的长方形

canvas.drawRoundRect(oval1, radius, radius, paint);// 第二个参数是x半径,第三个参数是y半径

super.onDraw(canvas);

}

public void setState(State state) {

this.mState = state;

invalidate();

}

public State getState() {

return mState;

}

public interface StateChangedListener {

void onStateChanged(State nowState);

}

private StateChangedListener mListener;

public void setStateChangedListener(StateChangedListener listener) {

this.mListener = listener;

}

}

源码下载