简单的自定义带有动画的checkbox
来源:互联网 发布:天小猫淘宝信誉查询 编辑:程序博客网 时间:2024/06/04 17:44
android自带的checkbox交互体验不好点击一下干巴巴的难看,很早就写了一个demo现在贴上来。
没必要解释很简单了
public class RightView extends View implements View.OnClickListener,ValueAnimator.AnimatorUpdateListener{ private Paint borderPaint; private float paintWidth=5; private int animDuration=200; private int selectColor=Color.RED; private int unselectColor=Color.GRAY; private boolean pressed=false; Path pathLine,dst1; PathMeasure pathMeasure; ValueAnimator animator; private RightViewSelectListener listener; float v1; public RightView(Context context) { this(context,null); } public RightView(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public RightView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); setOnClickListener(this); } private void init() { setBackgroundColor(Color.WHITE); borderPaint=new Paint(); borderPaint.setStyle(Paint.Style.STROKE); borderPaint.setAntiAlias(true); pathLine=new Path(); dst1=new Path(); pathMeasure=new PathMeasure(); animator=ValueAnimator.ofFloat(0,1); animator.setDuration(animDuration); animator.setInterpolator(new DecelerateInterpolator()); animator.addUpdateListener(this); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(measureDimension(widthMeasureSpec),measureDimension(heightMeasureSpec)); } private int measureDimension(int ms){ int result; int measureMode=MeasureSpec.getMode(ms); int measureSize=MeasureSpec.getSize(ms); if(measureMode==MeasureSpec.EXACTLY){ result=measureSize; }else { result=20; if(measureMode==MeasureSpec.AT_MOST){ result=Math.max(result,measureSize); } } return result; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Rect rect=new Rect(0,0,getWidth(),getHeight()); if(!pressed){ borderPaint.setColor(unselectColor); }else{ //点击之后 borderPaint.setStrokeWidth(paintWidth/2); borderPaint.setColor(selectColor); pathLine.moveTo(20, (float) (getHeight()*2.0/3)); pathLine.lineTo((float) (getWidth()*1.0/3),getHeight()-20); pathLine.lineTo(getWidth()-20, (float) (getHeight()*1.0/3)); pathMeasure.setPath(pathLine,false); pathMeasure.getSegment(0,v1*pathMeasure.getLength(),dst1,true); canvas.drawPath(dst1,borderPaint); dst1.reset(); } borderPaint.setStrokeWidth(paintWidth); canvas.drawRect(rect,borderPaint);//边框 } @Override public void onClick(View v) { if(pressed){ pressed=false; invalidate(); }else{ pressed=true; animator.start(); } if(listener!=null){ listener.onSelect(pressed); } } @Override public void onAnimationUpdate(ValueAnimator animation) { if(animation.equals(animation)){ v1= ((float) animation.getAnimatedValue()); invalidate(); } } public boolean getState(){ return pressed; } public void setRightViewSelectListener(RightViewSelectListener l){ listener=l; } interface RightViewSelectListener{ void onSelect(boolean select); }}
这里是xml
<com.sofang.lxm.viewevent.RightView android:layout_width="50dp" android:layout_height="50dp" />
阅读全文
0 0
- 简单的自定义带有动画的checkbox
- Android-带有切换动画的CheckBox
- Android 带有切换动画的CheckBox。
- Android自定义底部带有动画的Dialog
- 带有CheckBox的TreeList
- 写了一个简单的带有CheckBox的Tree[Swing]
- 自定义控件--带动画的CheckBox
- 自定义控件--带动画的CheckBox
- Android 自定义带动画的 CheckBox
- 带有checkbox的权限树
- 带有checkbox的jstree封装
- Android自定义View示例(四)—带有动画的Dialog
- 安卓自定义控件之带有折叠动画的ExpandableListView
- 自定义简单的转场动画
- android简单的自定义动画
- 带有checkbox的树的操作(Extjs)
- 带酷炫动画的checkbox
- 带有动画效果的tabbar
- 字符串
- Sublime Text 3修改插件安装位置
- ASP.NET 连接数据库 增删改查简单实例
- Tensorflow 基础(二)
- webstorm配置svn
- 简单的自定义带有动画的checkbox
- 八大排序总结
- 数组的常用方法总结一
- C编译环境搭建
- java常见异常
- 关于hibernate操作
- POJ1064 -- Cable master(二分)
- 【HDU1862】 EXCEL排序 (sort)
- Spring框架的设计理念与设计模式分析