纪录一下滑动按钮功能。。
首先效果图:
然后是分别建立三个文件,第一个是main.class,第二个是SlipButton.class,第三个是 onchangeListener.class
main.class
import Android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
publicclassMain extends Activity implements OnChangedListener {
/** Called when the activity is first created. */
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn);//获得指定控件
myBtn.SetOnChangedListener(this);//为控件设置监听器
}
@Override
publicvoidOnChanged(boolean CheckState) {//当按钮状态被改变时
// TODO Auto-generated method stub
if(CheckState)
Toast.makeText(this,"打开了...", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"关闭了...", Toast.LENGTH_SHORT).show();
}
}
SlipButton.class
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.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
publicclassSlipButton extends View implements OnTouchListener{
privateboolean NowChoose =false;//记录当前按钮是否打开,true为打开,flase为关闭
privateboolean OnSlip =false;//记录用户是否在滑动的变量
privatefloatDownX,NowX;//按下时的x,当前的x,
privateRect Btn_On,Btn_Off;//打开和关闭状态下,游标的Rect
privateboolean isChgLsnOn =false;
privateOnChangedListener ChgLsn;
privateBitmap bg_on,bg_off,slip_btn;
publicSlipButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
publicSlipButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
privatevoidinit(){//初始化
//载入图片资源
bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_on);
bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_off);
slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.slip_btn);
//获得需要的Rect数据
Btn_On =newRect(0,0,slip_btn.getWidth(),slip_btn.getHeight());
Btn_Off =newRect(
bg_off.getWidth()-slip_btn.getWidth(),
0,
bg_off.getWidth(),
slip_btn.getHeight());
setOnTouchListener(this);//设置监听器,也可以直接复写OnTouchEvent
}
@Override
protectedvoidonDraw(Canvas canvas) {//绘图函数
// TODO Auto-generated method stub
super.onDraw(canvas);
Matrix matrix =newMatrix();
Paint paint =newPaint();
floatx;
{
if(NowX
canvas.drawBitmap(bg_off,matrix, paint);//画出关闭时的背景
else
canvas.drawBitmap(bg_on,matrix, paint);//画出打开时的背景
if(OnSlip)//是否是在滑动状态,
{
if(NowX >= bg_on.getWidth())//是否划出指定范围,不能让游标跑到外头,必须做这个判断
x = bg_on.getWidth()-slip_btn.getWidth()/2;//减去游标1/2的长度...
else
x = NowX - slip_btn.getWidth()/2;
}else{//非滑动状态
if(NowChoose)//根据现在的开关状态设置画游标的位置
x = Btn_Off.left;
else
x = Btn_On.left;
}
if(x<0)//对游标位置进行异常判断...
x = 0;
elseif(x>bg_on.getWidth()-slip_btn.getWidth())
x = bg_on.getWidth()-slip_btn.getWidth();
canvas.drawBitmap(slip_btn,x, 0, paint);//画出游标.
}
}
@Override
publicboolean onTouch(View v, MotionEventevent) {
// TODO Auto-generated method stub
switch(event.getAction())//根据动作来执行代码
{
caseMotionEvent.ACTION_MOVE://滑动
NowX =event.getX();
break;
caseMotionEvent.ACTION_DOWN://按下
if(event.getX()>bg_on.getWidth()||event.getY()>bg_on.getHeight())
returnfalse;
OnSlip =true;
DownX =event.getX();
NowX = DownX;
break;
caseMotionEvent.ACTION_UP://松开
OnSlip =false;
boolean LastChoose = NowChoose;
if(event.getX()>=(bg_on.getWidth()/2))
NowChoose =true;
else
NowChoose =false;
if(isChgLsnOn&&(LastChoose!=NowChoose))//如果设置了监听器,就调用其方法..
ChgLsn.OnChanged(NowChoose);
break;
default:
}
invalidate();//重画控件
returntrue;
}
publicvoidSetOnChangedListener(OnChangedListener l){//设置监听器,当状态修改的时候
isChgLsnOn =true;
ChgLsn = l;
}
}
onchangeListener.class
package CMD100.demo.slipButton;
publicinterfaceOnChangedListener {
abstractvoidOnChanged(boolean CheckState);
}
main.xml代码
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
android:orientation ="horizontal"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:background ="#ff0000"
>
android:text ="测试:"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
/>
android:id ="@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft ="10sp"
/>
注意:在xml里头要放置的位置
android:id = "@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
然后可以像其他控件一样使用了...
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn);
myBtn.SetOnChangedListener(...);
代码到这里就全部完成了。