当用户滑动开关时候可以关闭或者开启按钮
ToggleButton .java
package com.qf.sxy.customview04.widget;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by sxy on 2016/9/29.
*/
public class ToggleButton extends View {
private Bitmap backGroundBitmap ;//背景图
private Bitmap slidBitmap;//滑动的图片
private Paint mPaint;//画笔对象
private Matrix matrix;//矩阵
//设置开关的矩阵
private Rect onRect,offRect;
private int currentX =0;//滑动时 当前的位置
private boolean isSlid =false;//是否是滑动
private boolean isToggle = false;//是开还是关的状态
//创建一个回调接口
public interface onToggleButtonListner{
public void onClick(boolean state);
}
onToggleButtonListner linstner;
//设置监听
public void setToggleButtnLinstner(onToggleButtonListner linstner){
this.linstner = linstner;
}
public ToggleButton(Context context) {
super(context);
}
public ToggleButton(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setAntiAlias(true);
//获取矩阵对象
matrix = new Matrix();
}
//通过逻辑代码 设置背景图和滑动图片
public void setToggleButtonBitmap(int backGroundBitmapId,int slidBitmapId ){
//获取背景图
backGroundBitmap = BitmapFactory.decodeResource(getResources(),backGroundBitmapId);
//滑动的图片
slidBitmap = BitmapFactory.decodeResource(getResources(),slidBitmapId);
//获取关闭的矩阵
offRect = new Rect(0,0,slidBitmap.getWidth(),backGroundBitmap.getHeight());
//开的矩阵
onRect = new Rect(backGroundBitmap.getWidth()-slidBitmap.getWidth(),0,
backGroundBitmap.getWidth(),backGroundBitmap.getHeight());
}
/**
* 绘制
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//背景图绘制到画布
canvas.drawBitmap(backGroundBitmap,matrix,mPaint);
//画滑动的图片
int left = (currentX-slidBitmap.getWidth()/2);
if(isSlid){//判断是否滑动的
//不让小滑块移动出去
if(left<offRect.left){
left = offRect.left;
}else if(left>onRect.left){
left= onRect.left;
}
//正在移动绘制小滑块
canvas.drawBitmap(slidBitmap,left,onRect.top,mPaint);
}else{//最终的结果获取
if(isToggle){//true
canvas.drawBitmap(slidBitmap,onRect.left,onRect.top,mPaint);
}else{
canvas.drawBitmap(slidBitmap,offRect.left,offRect.top,mPaint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
isSlid = true;
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_MOVE:
currentX = (int) event.getX();
break;
case MotionEvent.ACTION_UP:
isSlid = false;
currentX = (int) event.getX();
break;
}
Log.e("AAA","=currentX=>"+currentX);
boolean state = currentX>backGroundBitmap.getWidth()/2;
//是开还是关状态
//通过回调接口返回数据 当前状态是开还是关
if(linstner!=null&&state!=isToggle){
linstner.onClick(isToggle);
}
isToggle = state;
invalidate();//刷新
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//设置最终的区域
setMeasuredDimension(backGroundBitmap.getWidth(),backGroundBitmap.getHeight());
}
}
MainActivity .java
package com.qf.sxy.customview04;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.qf.sxy.customview04.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private ToggleButton tgBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tgBtn = ((ToggleButton) findViewById(R.id.tgBtn));
//给图片设置进去
tgBtn.setToggleButtonBitmap(R.mipmap.switch_background,R.mipmap.slide_button_background);
//设置监听
tgBtn.setToggleButtnLinstner(new ToggleButton.onToggleButtonListner() {
@Override
public void onClick(boolean state) {
if(state){
Toast.makeText(MainActivity.this,"开",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,"关",Toast.LENGTH_SHORT).show();
}
}
});
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.qf.sxy.customview04.MainActivity">
<com.qf.sxy.customview04.widget.ToggleButton
android:id="@+id/tgBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>