package com.dash.project_1510a.view;
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 com.dash.project_1510a.R;
/**
* Created by Dash on 2017/12/1.
*/
public class SwitchButtom extends View {
private Bitmap backBitmap;
private Bitmap foreBitmap;
private boolean state;
private boolean flag;
private float x;
public SwitchButtom(Context context) {
super(context);
}
public SwitchButtom(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SwitchButtom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//直接使用代码指定我当前控件的宽度和高度...实际上是背景图片的宽高
//如果在xml里面任意制定了宽度高度的话,
setMeasuredDimension(backBitmap.getWidth(),backBitmap.getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//背景画上去
canvas.drawBitmap(backBitmap,0,0,null);
//如果是触摸状态的话
if (flag){
//让滑块的中心跟着手指动,而不是左边缘
float left = x - foreBitmap.getWidth()/2;
if (left<0){
left = 0;
}else if (left>backBitmap.getWidth()-foreBitmap.getWidth()){
left = backBitmap.getWidth()-foreBitmap.getWidth();
}
canvas.drawBitmap(foreBitmap,left,0,null);
}else {
//根据开关的状态去画滑块
if (state){
//把滑块画在右边显示
canvas.drawBitmap(foreBitmap,backBitmap.getWidth()-foreBitmap.getWidth(),0,null);
}else {
//画在左边显示
canvas.drawBitmap(foreBitmap,0,0,null);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
flag = true;
x = event.getX();
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
break;
case MotionEvent.ACTION_UP:
flag = false;
x = event.getX();
//根据位置计算是否是开或者关状态
state = x>backBitmap.getWidth()/2;
break;
}
//重新绘制
postInvalidate();
return true;
}
/**
* 对外提供设置背景图片的方法
*/
public void setBackgroudBitmap(int drawable){
//解码资源目录下的文件
backBitmap = BitmapFactory.decodeResource(getResources(), drawable);
}
/**
* 设置滑块的方法
* @param drawable
*/
public void setForegroudBitmap(int drawable){
//解码资源目录下的文件
foreBitmap = BitmapFactory.decodeResource(getResources(), drawable);
}
/**
* 对外提供设置开关状态的方法
*/
public void setState(boolean state){
this.state = state;
}
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 com.dash.project_1510a.R;
/**
* Created by Dash on 2017/12/1.
*/
public class SwitchButtom extends View {
private Bitmap backBitmap;
private Bitmap foreBitmap;
private boolean state;
private boolean flag;
private float x;
public SwitchButtom(Context context) {
super(context);
}
public SwitchButtom(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SwitchButtom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//直接使用代码指定我当前控件的宽度和高度...实际上是背景图片的宽高
//如果在xml里面任意制定了宽度高度的话,
setMeasuredDimension(backBitmap.getWidth(),backBitmap.getHeight());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//背景画上去
canvas.drawBitmap(backBitmap,0,0,null);
//如果是触摸状态的话
if (flag){
//让滑块的中心跟着手指动,而不是左边缘
float left = x - foreBitmap.getWidth()/2;
if (left<0){
left = 0;
}else if (left>backBitmap.getWidth()-foreBitmap.getWidth()){
left = backBitmap.getWidth()-foreBitmap.getWidth();
}
canvas.drawBitmap(foreBitmap,left,0,null);
}else {
//根据开关的状态去画滑块
if (state){
//把滑块画在右边显示
canvas.drawBitmap(foreBitmap,backBitmap.getWidth()-foreBitmap.getWidth(),0,null);
}else {
//画在左边显示
canvas.drawBitmap(foreBitmap,0,0,null);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
flag = true;
x = event.getX();
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
break;
case MotionEvent.ACTION_UP:
flag = false;
x = event.getX();
//根据位置计算是否是开或者关状态
state = x>backBitmap.getWidth()/2;
break;
}
//重新绘制
postInvalidate();
return true;
}
/**
* 对外提供设置背景图片的方法
*/
public void setBackgroudBitmap(int drawable){
//解码资源目录下的文件
backBitmap = BitmapFactory.decodeResource(getResources(), drawable);
}
/**
* 设置滑块的方法
* @param drawable
*/
public void setForegroudBitmap(int drawable){
//解码资源目录下的文件
foreBitmap = BitmapFactory.decodeResource(getResources(), drawable);
}
/**
* 对外提供设置开关状态的方法
*/
public void setState(boolean state){
this.state = state;
}
}