WiperSwitch.java
package com.confidant.customwiperswitch;
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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
/**
*
* @author xuhaixin
*
*/
public class WiperSwitch extends View implements OnTouchListener {
private Bitmap bg_on, bg_off, slipper_btn;// 允许、拒绝、滑块
private float downX, nowX;// 按下时的x和当前的x
private boolean onSlip = false; // 记录用户是否在滑动
private boolean nowStatus = true;// 当前的状态
private OnChangedListener listener; // 监听接口
public WiperSwitch(Context context) {
super(context);
init();
}
public WiperSwitch(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
// 载入图片资源
bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.on_btn);
bg_off = BitmapFactory.decodeResource(getResources(),
R.drawable.off_btn);
slipper_btn = BitmapFactory.decodeResource(getResources(),
R.drawable.white_btn);
setOnTouchListener(this);
}
/**
* 绘制
*/
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Matrix matrix = new Matrix();
Paint paint = new Paint();
float x = 0;
// 根据nowX设置背景,开或者关状态
if (nowX < (bg_on.getWidth() / 2)) {
canvas.drawBitmap(bg_on, matrix, paint);// 画出打开时的背景
} else {
canvas.drawBitmap(bg_off, matrix, paint);// 画出关闭时的背景
}
if (onSlip) {// 是否是在滑动状态,
if (nowX >= bg_on.getWidth())// 是否划出指定范围,不能让滑块跑到外头,必须做这个判断
x = bg_on.getWidth() - slipper_btn.getWidth() / 2;// 减去滑块1/2的长度
else
x = nowX - slipper_btn.getWidth() / 2;
} else {
if (nowStatus) {// 根据当前的状态设置滑块的x值
x = 0;
} else {
x = bg_on.getWidth() - slipper_btn.getWidth();
}
}
// 对滑块滑动进行异常处理,不能让滑块出界
if (x < 0) {
x = 0;
} else if (x > bg_on.getWidth() - slipper_btn.getWidth()) {
x = bg_on.getWidth() - slipper_btn.getWidth();
}
canvas.drawBitmap(slipper_btn, x, 0, paint); // 画出滑块
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if (event.getX() > bg_off.getWidth()
|| event.getY() > bg_off.getHeight()) {
return false;
} else {
onSlip = true;
downX = event.getX();
nowX = downX;
}
break;
}
case MotionEvent.ACTION_MOVE: {
nowX = event.getX();
break;
}
case MotionEvent.ACTION_UP: {
onSlip = false;
if (event.getX() >= (bg_on.getWidth() / 2)) {
nowStatus = false;
nowX = bg_on.getWidth() - slipper_btn.getWidth();
} else {
nowStatus = true;
nowX = 0;
}
if (listener != null) {
listener.OnChanged(WiperSwitch.this, nowStatus);
}
break;
}
}
// 刷新界面
invalidate();
return true;
}
/**
* 为WiperSwitch设置一个监听,供外部调用的方法
*
* @param listener
*
*/
public void setOnChangedListener(OnChangedListener listener) {
this.listener = listener;
}
/**
* 设置滑动开关的初始状态,供外部调用
*
* @param checked
*
*/
public void setChecked(boolean checked) {
if (checked) {
nowX = 0;
} else {
nowX = bg_off.getWidth();
}
nowStatus = checked;
}
/**
* 回调接口
*/
public interface OnChangedListener {
public void OnChanged(WiperSwitch wiperSwitch, boolean checkState);
}
}
activity_main.xml
<?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" >
<com.confidant.customwiperswitch.WiperSwitch
android:id="@+id/wiperSwitch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java
package com.confidant.customwiperswitch;
import com.confidant.customwiperswitch.WiperSwitch.OnChangedListener;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity implements OnChangedListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//实例化WiperSwitch
WiperSwitch wiperSwitch = (WiperSwitch)findViewById(R.id.wiperSwitch1);
//设置初始状态为false
wiperSwitch.setChecked(false);
//设置监听
wiperSwitch.setOnChangedListener(this);
}
@Override
public void OnChanged(WiperSwitch wiperSwitch, boolean checkState) {
Log.e("log", "" + checkState);
}
}
styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowNoTitle">true</item>
</style>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.confidant.customwiperswitch"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.confidant.customwiperswitch.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>