importandroid.content.Context;importandroid.util.AttributeSet;importandroid.view.Gravity;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewGroup;importandroid.view.animation.AlphaAnimation;importandroid.view.animation.Animation;importandroid.view.animation.AnimationUtils;importandroid.widget.LinearLayout;importandroid.widget.Scroller;importcom.fans.framework.utils.ViewUtils;importcom.fans.momhelpers.R;/***选项*<p/>*声明R.dimen.wXX这种类型的是一个dp数字。这种是一种实时的适配方式**/publicclassSwitchViewextendsViewGroupimplementsView.OnTouchListener{privateLinearLayoutscrollView;//滑块privateintscrollWidth=0;privateScrollerscroller;privatebooleanchecked=false;privateViewswitchView;privateintmaskWidth;privateAnimationgone=AnimationUtils.loadAnimation(getContext(),R.anim.alpha_gone);privateAnimationshow=AnimationUtils.loadAnimation(getContext(),R.anim.alpha_show);privateOnCheckedChangedListeneronCheckedChangedListener;//监听选择变化privateViewbgView;publicSwitchView(Contextcontext,AttributeSetattrs){super(context,attrs);init();switchView.setBackgroundResource(R.drawable.switch_mask);maskWidth=(int)getResources().getDimension(R.dimen.w65);intcheckedBg=R.drawable.switch_open;intnormalBg=R.drawable.switch_close;setBackgroundResource(normalBg);Animationanim=newAlphaAnimation(0,0);anim.setDuration(0);anim.setFillAfter(true);bgView.startAnimation(anim);bgView.setBackgroundResource(checkedBg);}/***初始化*/privatevoidinit(){addView();scroller=newScroller(getContext());scrollView.setOnTouchListener(this);show.setDuration(250);show.setFillAfter(true);gone.setDuration(250);gone.setFillAfter(true);}privateintlastX=0;privateintmarginLeft=0;//按压点距离左边的距离privatelonglastTime=0;@OverridepublicbooleanonTouch(Viewview,MotionEventmotionEvent){intx=(int)motionEvent.getX();switch(motionEvent.getAction()){caseMotionEvent.ACTION_DOWN:if(lastX==0)lastX=x;if(marginLeft==0)marginLeft=x+scrollView.getScrollX();lastTime=System.currentTimeMillis();break;caseMotionEvent.ACTION_MOVE:intmoveX=marginLeft-x;if(moveX>0)scrollView.scrollTo(0,0);elseif(moveX<-scrollWidth)scrollView.scrollTo(-scrollWidth,0);elsescrollView.scrollTo(moveX,0);lastX=x;break;caseMotionEvent.ACTION_UP:caseMotionEvent.ACTION_CANCEL:marginLeft=0;if(System.currentTimeMillis()-lastTime<=100){if(checked)close();elseopen();onCheckedChanged();returntrue;}intcenter=scrollWidth/2-scrollView.getScrollX();//滑块中间的X坐标if(center>scrollWidth){//超过中线open();}else{close();}onCheckedChanged();break;}returntrue;}/***关闭动画*/privatevoidclose(intduring){intstartX=scrollView.getScrollX();intdx=-startX;scroller.startScroll(startX,0,dx,0,during);if(checked&&during>0){bgView.setVisibility(View.INVISIBLE);bgView.startAnimation(gone);}else{bgView.setVisibility(View.INVISIBLE);}invalidate();checked=false;//onCheckedChanged();}privatevoidclose(){close(250);}privatevoidopen(){open(250);}/***开启动画*/privatevoidopen(intduring){intstartX=scrollView.getScrollX();intdx=scrollWidth+startX;scroller.startScroll(startX,0,-dx,0,during);bgView.setVisibility(View.VISIBLE);if(!checked&&during>0){bgView.startAnimation(show);}else{bgView.clearAnimation();}invalidate();checked=true;//onCheckedChanged();}publicvoidsetChecked(finalbooleanisCheck){if(isChecked()==isCheck)return;if(scrollWidth>0){switchCheck(isCheck);}else{post(newRunnable(){@Overridepublicvoidrun(){switchCheck(isCheck);}});}}privatevoidswitchCheck(finalbooleanisCheck){if(isCheck){open(0);}else{close(0);}}/***打开或者关闭时候调用*/privatevoidonCheckedChanged(){if(onCheckedChangedListener!=null)onCheckedChangedListener.onCheckedChanged(checked,this);}@OverridepublicvoidcomputeScroll(){if(scroller.computeScrollOffset()){scrollView.scrollTo(scroller.getCurrX(),scroller.getCurrY());postInvalidate();}}/***是否打开**@returnbool*/publicbooleanisChecked(){returnchecked;}/***添加滑块*/privatevoidaddView(){LayoutParamsparams=newLayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);addView(bgView=newView(getContext()));scrollView=newLinearLayout(getContext());LinearLayout.LayoutParamsmaskParams=newLinearLayout.LayoutParams((int)getResources().getDimension(R.dimen.w65),(int)getResources().getDimension(R.dimen.w65));maskParams.gravity=Gravity.CENTER_VERTICAL;maskParams.topMargin=(int)getResources().getDimension(R.dimen.h3);scrollView.addView(switchView=newView(getContext()),maskParams);scrollView.addView(newView(getContext()),newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT,1));addView(scrollView,params);}@OverrideprotectedvoidonLayout(booleanchanged,intl,intt,intr,intb){Viewchild=getChildAt(0);intheight=child.getMeasuredHeight();intwidth=child.getMeasuredWidth();child.layout(0,0,width,height);getChildAt(1).layout(0,0,width,height);scrollWidth=width-maskWidth;}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){super.onMeasure(widthMeasureSpec,heightMeasureSpec);//@dimen/w95//@dimen/w58//intwidth=getintwidth=ViewUtils.getDimenPx(R.dimen.w95);intheight=ViewUtils.getDimenPx(R.dimen.w58);//intwidth=MeasureSpec.getSize(widthMeasureSpec);//intheight=MeasureSpec.getSize(heightMeasureSpec);widthMeasureSpec=MeasureSpec.makeMeasureSpec(width,MeasureSpec.EXACTLY);heightMeasureSpec=MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY);setMeasuredDimension(width,height);getChildAt(0).measure(widthMeasureSpec,heightMeasureSpec);getChildAt(1).measure(widthMeasureSpec,heightMeasureSpec);}/***设置监听**@paramlistener*listener*/publicvoidsetOnCheckedChangedListener(OnCheckedChangedListenerlistener){onCheckedChangedListener=listener;}publicinterfaceOnCheckedChangedListener{publicvoidonCheckedChanged(booleanisChecked,SwitchViewv);}}

在res 中新建一个文件夹anim 然后添加两个动画

这个动画名字是 alpha_gone

<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:fromAlpha="1.0"android:toAlpha="0.0"android:duration="250"android:fillAfter="true"/></set>

这个动画叫 alpha_show

<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android="http://schemas.android.com/apk/res/android"><alphaandroid:fromAlpha="0.0"android:toAlpha="1.0"android:duration="250"/></set>

现在已经完成了 你可以想实现CheackBox一样使用这个了

<com.widget.SwitchViewandroid:id="@+id/switch_view"android:layout_width="wrap_content"android:layout_height="wrap_content"/>


更多相关文章

  1. android亮屏、暗屏、解锁、关闭系统对话的监听事件
  2. Android加载Gif动画实现代码
  3. android中动画的学习
  4. Android(安卓)监听网络变化然后刷新页面
  5. Android(安卓)简单的网络变化监听器
  6. Android(安卓)Q自定义开关机动画
  7. Android菜鸟日记15-传感器与摄像头
  8. android ListView监听滚动条滚动到底部时继续加载
  9. Android抖动动画

随机推荐

  1. android的PowerManager和PowerManager.Wa
  2. TextView属性详解
  3. android:imeOptions属性
  4. 从NDK在非Root手机上的调试原理探讨Andro
  5. Android系统服务-WindowManager
  6. Android音频子系统源码分析之AudioTrack
  7. andriod EditText的属性[转]
  8. Android(安卓)侧滑菜单的实现(NavigationV
  9. Android(安卓)Activity生命周期管理
  10. Android网络游戏之神农诀项目开发--视频