词穷, 懒得写铺垫, 直接上效果
SwitchView.gifxml引用
<cn.startrails.webblintestdemo.widget.SmilingFaceView
android:id="@+id/sfv_test"
android:layout_width="100dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
在control里添加监听
mBD.sfvTest.setStateListener(state -> {
if (state == SmilingFaceView.State.ON) {
// do something
} else {
// do something
}
});
java代码
package cn.startrails.webblintestdemo.widget;
import android.annotation.SuppressLint;
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.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import cn.startrails.webblintestdemo.R;
/**
* @author WebbLin(林恩)
* @date 2018/4/18 15:56
* @email Webb@starcc.cc
*/
public class SmilingFaceView extends View {
/**
* 画笔
*/
private Paint mPaint;
/**
* 圆角矩形
*/
private RectF mRectF;
/**
* 圆角半径 rx ry
*/
private float radius;
/**
* 位图
*/
private Bitmap mBitmap;
/**
* bitmap 的间距
*/
private int bitSpc;
/**
* 移动的距离
*/
private int translateX;
/**
* 默认为关
*/
private State mState = State.OFF;
private StateListener mStateListener;
public SmilingFaceView(Context context) {
super(context);
init();
}
public SmilingFaceView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public SmilingFaceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public enum State {
ON,
OFF
}
private void init() {
setLayerType(LAYER_TYPE_SOFTWARE, null);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.WHITE);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
/*
* 圆角矩形大小和位置
*/
float top;
float left = top = 10;
float right = w - 10;
float bottom = h - 10;
// View高
float height = bottom - top;
// 矩形
mRectF = new RectF(left, top, right, bottom);
// 半径
radius = bottom - top;
// 位图
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.synthetic1_100);
mBitmap = Bitmap.createScaledBitmap(bitmap,
(int) height - 8, (int) height - 8, true);
bitSpc = (h - mBitmap.getHeight()) / 2;
translateX = w - (mBitmap.getWidth() + 28);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setStyle(Paint.Style.FILL);
// 绘制背景圆角矩形
canvas.drawRoundRect(mRectF, radius, radius, mPaint);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(8);
mPaint.setColor(getResources().getColor(R.color.bluegrey300));
// 绘制阴影圆角矩形
canvas.drawRoundRect(mRectF, radius, radius, mPaint);
canvas.save();
if (mState != State.OFF) {
canvas.translate(translateX, 0);
mPaint.setColor(Color.WHITE);
} else {
mPaint.setColor(getResources().getColor(R.color.greena700));
}
// 绘制位图
canvas.drawBitmap(mBitmap, bitSpc, bitSpc, mPaint);
canvas.restore();
}
@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);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
// 关闭状态
if (mState == State.OFF) {
mState = State.ON;
if (mStateListener != null)
mStateListener.onState(mState);
postInvalidate();
// 打开状态
} else {
mState = State.OFF;
if (mStateListener != null)
mStateListener.onState(mState);
postInvalidate();
}
break;
}
return true;
}
/**
* 状态监听
*/
public interface StateListener {
void onState(State state);
}
public StateListener getStateListener() {
return mStateListener;
}
public void setStateListener(StateListener stateListener) {
mStateListener = stateListener;
}
}
网友评论