mainactivity--------------------------
package com.liulailin.day03_myswitchbutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyButton myButton = (MyButton) findViewById(R.id.myButton);
myButton.setState(false);
myButton.setBackgroudColor(R.mipmap.switch_background);
myButton.setSlidingColor(R.mipmap.slide_button_background);
myButton.setOnButtonStateChangedListener(new OnButtonStateChangedListener() {
@Override
public void onButtonState(boolean state) {
if (state){
Toast.makeText(MainActivity.this, "开关是开的", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "开关是关的状态", Toast.LENGTH_SHORT).show();
}
}
});
}
}
mybutton-----------------------------------
package com.liulailin.day03_myswitchbutton;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import static android.R.attr.left;
/**
* Created by Administrator on 2018/3/9.
*/
public class MyButton extends View {
boolean state = false;
Bitmap buttonBackGround;
Bitmap buttonSlidingBack;
private boolean isSliding;
private int currentx;
OnButtonStateChangedListener MyListener;
public MyButton(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//设置背景图片的宽和高
setMeasuredDimension(buttonBackGround.getWidth(),buttonBackGround.getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//1:绘制背景图片
canvas.drawBitmap(buttonBackGround,0,0,null);
//2:绘制滑动块的图片,并指定其位置
//判断是否正在滑动中,如果在滑动中
//需要根据手指按下的x轴的值,来去绘制滑动left位置
int onLeft = buttonBackGround.getWidth()-buttonSlidingBack.getWidth();
if (isSliding){
//左右边界超出 就是按下不在中心的位置
int left = currentx-buttonSlidingBack.getWidth()/2;
if (left<0){
left=0;
}else if(left>onLeft){
left=onLeft;
}
//当前手指按下的不是滑动块的中心点
//正在滑动中我们应该走这
canvas.drawBitmap(buttonSlidingBack,left,0,null);
}else {
//不是正在滑动中我们是不是要有一个状态
if (state){
//开的状态
canvas.drawBitmap(buttonSlidingBack,onLeft,0,null);
}else {
//关的状态
canvas.drawBitmap(buttonSlidingBack,0,0,null);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
isSliding = true;
currentx = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
currentx = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
isSliding = false;
currentx = (int) event.getX();
boolean flag = currentx>buttonBackGround.getWidth()/2;
if(flag!=state && MyListener!=null){
MyListener.onButtonState(flag);
}
if (currentx<buttonBackGround.getWidth()/2){
//当前x 轴的值,<背景图片的一半,进入关的状态
state=false;
}else {
//如果当前x轴的值,>背景图片的一半,进入开的状态
state=true;
}
//调用用户的回调事件
break;
}
//让其重新绘制的方法
invalidate();
return true;
}
//设置按钮的开关的状态
public void setState(boolean b) {
state = b;
}
//设置按钮的背景颜色
public void setBackgroudColor(int backGround) {
buttonBackGround = BitmapFactory.decodeResource(getResources(), backGround);
}
//设置滑动块的
public void setSlidingColor(int slidingBack) {
buttonSlidingBack = BitmapFactory.decodeResource(getResources(), slidingBack);
}
public void setOnButtonStateChangedListener(OnButtonStateChangedListener listener){
MyListener =listener;
}
}
onbuttonstatechangelistener
package com.liulailin.day03_myswitchbutton;
/**
* Created by Administrator on 2018/3/10.
* 开关状态的监听事件
*/
public interface OnButtonStateChangedListener {
public void onButtonState(boolean state);
}