仿苹果按钮

<com.haoqikj.app.zsxc.Customer.SlideSwitch
android:layout_marginTop="30dp"
android:layout_marginLeft="16dp"
android:id="@+id/swiBtn"
android:layout_width="60dip"
android:layout_height="30dip"
slideswitch:isOpen="true"
slideswitch:shape="circle"
slideswitch:themeColor="#FE0134">
</com.haoqikj.app.zsxc.Customer.SlideSwitch>
=================================================================================================

public classSlideSwitchextendsView {
public static final int SHAPE_RECT=1;
public static final intSHAPE_CIRCLE=2;
private static final intRIM_SIZE=6;
private static final intDEFAULT_COLOR_THEME= Color.parseColor("#FE0134");
private intcolor_theme;
public booleanisOpen;
private intshape;
privatePaintpaint;
privateRectbackRect;
privateRectfrontRect;
privateRectFfrontCircleRect;
privateRectFbackCircleRect;
private intalpha;
private intmax_left;
private intmin_left;
private intfrontRect_left;
private intfrontRect_left_begin=RIM_SIZE;
private inteventStartX;
private inteventLastX;
private intdiffX=0;
private booleanslideable=true;
privateSlideListenerlistener;

public interfaceSlideListener {
public void open();

public voidclose();
}

publicSlideSwitch(Context context,AttributeSet attrs, int defStyleAttr) {
super(context,attrs,defStyleAttr);
listener=null;
paint=newPaint();
paint.setAntiAlias(true);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.slideswitch);
color_theme= a.getColor(R.styleable.slideswitch_themeColor,
DEFAULT_COLOR_THEME);
isOpen= a.getBoolean(R.styleable.slideswitch_isOpen, false);
shape= a.getInt(R.styleable.slideswitch_shape,SHAPE_RECT);
a.recycle();
}

publicSlideSwitch(Context context,AttributeSet attrs) {
this(context,attrs,0);
}

publicSlideSwitch(Context context) {
this(context, null);
}

@Override
protected void onMeasure(intwidthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
intwidth = measureDimension(10,widthMeasureSpec);
intheight = measureDimension(30,heightMeasureSpec);
if(shape==SHAPE_CIRCLE) {
if(width < height)
width = height *2;
}
setMeasuredDimension(width,height);
initDrawingVal();
}

public void initDrawingVal() {
intwidth = getMeasuredWidth();
intheight = getMeasuredHeight();

backCircleRect=newRectF();
frontCircleRect=newRectF();
frontRect=newRect();
backRect=newRect(0,0,width,height);
min_left=RIM_SIZE;
if(shape==SHAPE_RECT)
max_left= width /2;
else
max_left= width - (height -2*RIM_SIZE) - RIM_SIZE;
if(isOpen) {
frontRect_left=max_left;
alpha=255;
}else{
frontRect_left=RIM_SIZE;
alpha=0;
}
frontRect_left_begin=frontRect_left;
}

public int measureDimension(intdefaultSize, intmeasureSpec) {
intresult;
intspecMode = MeasureSpec.getMode(measureSpec);
intspecSize = MeasureSpec.getSize(measureSpec);
if(specMode == MeasureSpec.EXACTLY) {
result = specSize;
}else{
result = defaultSize;// UNSPECIFIED
if(specMode == MeasureSpec.AT_MOST) {
result = Math.min(result,specSize);
}
}
returnresult;
}

@Override
protected void onDraw(Canvas canvas) {
if(shape==SHAPE_RECT) {
paint.setColor(Color.GRAY);
canvas.drawRect(backRect,paint);
paint.setColor(color_theme);
paint.setAlpha(alpha);
canvas.drawRect(backRect,paint);
frontRect.set(frontRect_left,RIM_SIZE,frontRect_left
+ getMeasuredWidth() / 2-RIM_SIZE,getMeasuredHeight()
-RIM_SIZE);
paint.setColor(Color.WHITE);
canvas.drawRect(frontRect,paint);
}else{
// draw circle
intradius;
radius = backRect.height() / 2-RIM_SIZE;
paint.setColor(Color.GRAY);
backCircleRect.set(backRect);
canvas.drawRoundRect(backCircleRect,radius,radius,paint);
paint.setColor(color_theme);
paint.setAlpha(alpha);
canvas.drawRoundRect(backCircleRect,radius,radius,paint);
frontRect.set(frontRect_left,RIM_SIZE,frontRect_left
+backRect.height() - 2*RIM_SIZE,backRect.height()
-RIM_SIZE);
frontCircleRect.set(frontRect);
paint.setColor(Color.WHITE);
canvas.drawRoundRect(frontCircleRect,radius,radius,paint);
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if(slideable==false)
return super.onTouchEvent(event);
intaction = MotionEventCompat.getActionMasked(event);
switch(action) {
caseMotionEvent.ACTION_DOWN:
eventStartX= (int) event.getRawX();
break;
caseMotionEvent.ACTION_MOVE:
eventLastX= (int) event.getRawX();
diffX=eventLastX-eventStartX;
inttempX =diffX+frontRect_left_begin;
tempX = (tempX > max_left?max_left: tempX);
tempX = (tempX < min_left?min_left: tempX);
if(tempX >=min_left&& tempX <=max_left) {
frontRect_left= tempX;
alpha= (int) (255* (float) tempX / (float)max_left);
invalidateView();
}
break;
caseMotionEvent.ACTION_UP:
intwholeX = (int) (event.getRawX() - eventStartX);
frontRect_left_begin=frontRect_left;
booleantoRight;
toRight = (frontRect_left_begin>max_left/2?true:false);
if(Math.abs(wholeX) < 3) {
toRight = !toRight;
}
moveToDest(toRight);
break;
default:
break;
}
return true;
}

/**
* draw again
*/
private void invalidateView() {
if(Looper.getMainLooper() == Looper.myLooper()) {
invalidate();
}else{
postInvalidate();
}
}

public void setSlideListener(SlideListener listener) {
this.listener= listener;
}

public void moveToDest(final boolean toRight) {
ValueAnimator toDestAnim = ValueAnimator.ofInt(frontRect_left,
toRight ? max_left:min_left);
toDestAnim.setDuration(500);
toDestAnim.setInterpolator(newAccelerateDecelerateInterpolator());
toDestAnim.start();
toDestAnim.addUpdateListener(newAnimatorUpdateListener() {

@Override
public void onAnimationUpdate(ValueAnimator animation) {
frontRect_left= (Integer) animation.getAnimatedValue();
alpha= (int) (255* (float)frontRect_left/ (float)max_left);
invalidateView();
}
});
toDestAnim.addListener(newAnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if(toRight) {
isOpen=true;
if(listener!=null)
listener.open();
frontRect_left_begin=max_left;
}else{
isOpen=false;
if(listener!=null)
listener.close();
frontRect_left_begin=min_left;
}
}
});
}

public void setState(booleanisOpen) {
this.isOpen= isOpen;
initDrawingVal();
invalidateView();
if(listener!=null)
if(isOpen ==true) {
listener.open();
}else{
listener.close();
}
}

public void setShapeType(intshapeType) {
this.shape= shapeType;
}

public void setSlideable(booleanslideable) {
this.slideable= slideable;
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
if(stateinstanceofBundle) {
Bundle bundle = (Bundle) state;
this.isOpen= bundle.getBoolean("isOpen");
state = bundle.getParcelable("instanceState");
}
super.onRestoreInstanceState(state);
}

@Override
protectedParcelableonSaveInstanceState() {
Bundle bundle =newBundle();
bundle.putParcelable("instanceState", super.onSaveInstanceState());
bundle.putBoolean("isOpen", this.isOpen);
returnbundle;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值