最近用到Switch,但是系统的风格不尽如人意,所以决定自己自定义一个,方案有3,一个继承 CompoundButton,类似系统4.0中Switch写法完全自定义,其二,继承系统的switch组件,不过兼容性只能适配4.0以上系统,其三,就是继承View自定义,网上有很多是用系统源码改写的,有人嫌自定义view不支持点击,所以我将自己的代码贴出来,以飨读者!
/**------------------
*MySwitchjava
*------------------
*/
package com.example.witch;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.CompoundButton;
import android.widget.Switch;
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 jelly
* @time 2014/1/22
*/
public class MySwitch extends View implements OnTouchListener {
private float startX,endX,nowX;
private Bitmap state_on_drawable, state_off_drawable, slipper_drawable;
private boolean nowStatus = false;
private boolean onSlip = false;
private Matrix matrix;
private Paint paint;
private OnSwitchChangedListener listener;
public MySwitch(Context context) {
super(context);
initResource();
}
public MySwitch(Context context, AttributeSet attrs) {
super(context, attrs);
initResource();
}
public void initResource() {
matrix = new Matrix();
paint = new Paint();
state_on_drawable = BitmapFactory.decodeResource(getResources(),
R.drawable.state_on);
state_off_drawable = BitmapFactory.decodeResource(getResources(),
R.drawable.state_off);
slipper_drawable = BitmapFactory.decodeResource(getResources(),
R.drawable.slider);
setOnTouchListener(this);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float x = 0;
if (nowStatus) {
canvas.drawBitmap(state_on_drawable, matrix, paint);
} else {
canvas.drawBitmap(state_off_drawable, matrix, paint);
}
if (onSlip) {
if (nowX >= state_on_drawable.getWidth())
x = state_on_drawable.getWidth() - slipper_drawable.getWidth()
/ 2;
else
x = nowX - slipper_drawable.getWidth() / 2;
} else {
if (nowStatus) {
x = state_on_drawable.getWidth() - slipper_drawable.getWidth();
} else {
x = 0;
}
}
if (x < 0) {
x = 0;
} else if (x > state_on_drawable.getWidth()
- slipper_drawable.getWidth()) {
x = state_on_drawable.getWidth() - slipper_drawable.getWidth();
}
canvas.drawBitmap(slipper_drawable, x, 0, paint);
}
public boolean isClickEvent(float startX,float endX) {//is click or slip
if(endX-startX<slipper_drawable.getWidth()/4)
return true;
else return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
if (event.getX() > state_off_drawable.getWidth()
|| event.getY() > state_off_drawable.getHeight()) {//ingnore other area
return false;
} else {
onSlip=true;
startX = event.getX();
nowX = startX;
}
break;
}
case MotionEvent.ACTION_MOVE: {
nowX = event.getX();
break;
}
case MotionEvent.ACTION_UP: {
endX=nowX;
onSlip=false;
if(isClickEvent(startX, endX))//is user just do click event
{
if(nowStatus)
{
nowStatus = false;
nowX = 0;
}
else {
nowStatus = true;
nowX = state_on_drawable.getWidth()
- slipper_drawable.getWidth();
}
}
else {//is slip event
if (event.getX() >= (state_on_drawable.getWidth() / 2)) {
nowStatus = true;
nowX = state_on_drawable.getWidth()
- slipper_drawable.getWidth();
} else {
nowStatus = false;
nowX = 0;
}
}
if (listener != null) {
listener.OnChanged(MySwitch.this, nowStatus);
}
break;
}
}
invalidate();
return true;
}
public void setOnSwitchChangedListener(OnSwitchChangedListener listener) {
this.listener = listener;
}
public void setChecked(boolean checked) {
if (checked) {
nowX = state_off_drawable.getWidth();
} else {
nowX = 0;
}
nowStatus = checked;
}
public interface OnSwitchChangedListener {
public void OnChanged(MySwitch mySwitch, boolean checkState);
}
}
/**------------------
*MainActivity.java
*------------------
*/
package com.example.witch;import com.example.witch.MySwitch.OnSwitchChangedListener;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity implements OnSwitchChangedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySwitch mwitch = (MySwitch) findViewById(R.id.mwitch);
// 设置初始状态为false
mwitch.setChecked(false);
// 设置监听
mwitch.setOnSwitchChangedListener(this);
}
@Override
public void OnChanged(MySwitch mySwitch, boolean checkState) {
Log.i("now state", checkState + "");
}
}
/**------------------
*activity_main.xml
*------------------
*/
<LinearLayout 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:orientation="vertical" >
<com.example.witch.MySwitch
android:id="@+id/mwitch"
android:layout_width="100dip"
android:layout_height="50dip"
/>
</LinearLayout>