设为首页 加入收藏

TOP

自定义控件--带动画的CheckBox(二)
2015-11-21 01:02:11 来源: 作者: 【 】 浏览:11
Tags:定义 控件 动画 CheckBox
m w * @param h * @param oldw * @param oldh */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); height = width = Math.min(w - getPaddingLeft() - getPaddingRight(),h - getPaddingBottom() - getPaddingTop()); cx = w / 2; cy = h / 2; float r = height / 2f; points[0] = r / 2f + getPaddingLeft(); points[1] = r + getPaddingTop(); points[2] = r * 5f / 6f + getPaddingLeft(); points[3] = r + r / 3f + getPaddingTop(); points[4] = r * 1.5f +getPaddingLeft(); points[5] = r - r / 3f + getPaddingTop(); radius = (int) (height * 0.125f); } @Override protected void onDraw(Canvas canvas) { float f = (radius -height * 0.125f) / (height * 0.5f); //当前进度 mCirclePaint.setColor(eva luate(f,unCheckColor,circleColor)); canvas.drawCircle(cx, cy, radius, mCirclePaint); //画圆 //画对号 if(correctProgress>0) { if(correctProgress<1/3f) { float x = points[0] + (points[2] - points[0]) * correctProgress; float y = points[1] + (points[3] - points[1]) * correctProgress; canvas.drawLine(points[0], points[1], x, y, mCorrectPaint); }else { float x = points[2] + (points[4] - points[2]) * correctProgress; float y = points[3] + (points[5] - points[3]) * correctProgress; canvas.drawLine(points[0], points[1], points[2], points[3], mCorrectPaint); canvas.drawLine(points[2], points[3], x,y, mCorrectPaint); } } } /** * 设置圆的颜色 * @param color */ public void setCircleColor(int color){ circleColor = color; } /** * 设置对号的颜色 * @param color */ public void setCorrectColor(int color){ mCorrectPaint.setColor(color); } /** * 设置未选中时的颜色 * @param color */ public void setUnCheckColor(int color){ unCheckColor = color; } private int eva luate(float fraction, int startValue, int endValue) { int startInt = startValue; int startA = (startInt >> 24) & 0xff; int startR = (startInt >> 16) & 0xff; int startG = (startInt >> 8) & 0xff; int startB = startInt & 0xff; int endInt = endValue; int endA = (endInt >> 24) & 0xff; int endR = (endInt >> 16) & 0xff; int endG = (endInt >> 8) & 0xff; int endB = endInt & 0xff; return ((startA + (int) (fraction * (endA - startA))) << 24) | ((startR + (int) (fraction * (endR - startR))) << 16) | ((startG + (int) (fraction * (endG - startG))) << 8) | ((startB + (int) (fraction * (endB - startB)))); } /** * 处理触摸事件触发动画 */ /*private class OnChangeStatusListener implements OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { Log.i(Touch,Touch); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downY = event.getRawY(); break; case MotionEvent.ACTION_MOVE: float dy = event.getRawY() - downY; if (Math.abs(dy) >= 0) { //滑过一半触发 toggle = true; } else { toggle = false; } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (toggle) { if (isChecked) { hideCorrect(); } else { showCheck(); } } break; } return true; } }*/ private void showUnChecked() { if (isAnim) { return; } isAnim = true; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(animDurtion); va.setInterpolator(new LinearInterpolator()); va.start(); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) animation.getAnimatedValue(); // 0f ~ 1f radius = (int) ((1 - value) * height * 0.375f + height * 0.125f); if (value >= 1) { isChecked = false; isAnim = false; if(listener!=null){ listener.onCheckedChanged(TouchCheckBox.this,false); } } invalidate(); } }); } private void showCheck() { if (isAnim) { return; } isAnim = true; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(animDurtion); va.setInterpolator(new LinearInterpolator()); va.start(); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) animation.getAnimatedValue(); // 0f ~ 1f radius = (int) (value * height * 0.37f + height * 0.125f); if (value >= 1) { isChecked = true; isAnim = false; if(listener!=null){ listener.onCheckedChanged(TouchCheckBox.this,true); } showCorrect(); } invalidate(); } }); } private void showCorrect() { if (isAnim) { return; } isAnim = true; ValueAnimator va = ValueAnimator.ofFloat(0, 1).setDuration(animDurtion); va.setInterpolator(new LinearInterpolator()); va.start(); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float value = (float) animation.getAnimatedValue(); // 0f ~ 1f correctProgress = value; invalidate(); if(value>=1){ isAnim = false; } } }); } private void hideCorrect() { if (isAnim) {
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇拼图代码-两张图片拼接 下一篇C++中虚基类

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: