Example #1
0
  private void init(AttributeSet attrs) {

    TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.SmoothCheckBox);
    int tickColor = ta.getColor(R.styleable.SmoothCheckBox_color_tick, COLOR_TICK);
    mAnimDuration = ta.getInt(R.styleable.SmoothCheckBox_duration, DEF_ANIM_DURATION);
    mFloorColor =
        ta.getColor(R.styleable.SmoothCheckBox_color_unchecked_stroke, COLOR_FLOOR_UNCHECKED);
    mCheckedColor = ta.getColor(R.styleable.SmoothCheckBox_color_checked, COLOR_CHECKED);
    mUnCheckedColor = ta.getColor(R.styleable.SmoothCheckBox_color_unchecked, COLOR_UNCHECKED);
    mStrokeWidth =
        ta.getDimensionPixelSize(
            R.styleable.SmoothCheckBox_stroke_width, CompatUtils.dp2px(getContext(), 0));
    ta.recycle();

    mFloorUnCheckedColor = mFloorColor;
    mTickPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTickPaint.setStyle(Paint.Style.STROKE);
    mTickPaint.setStrokeCap(Paint.Cap.ROUND);
    mTickPaint.setColor(tickColor);

    mFloorPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mFloorPaint.setStyle(Paint.Style.FILL);
    mFloorPaint.setColor(mFloorColor);

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(mCheckedColor);

    mTickPath = new Path();
    mCenterPoint = new Point();
    mTickPoints = new Point[3];
    mTickPoints[0] = new Point();
    mTickPoints[1] = new Point();
    mTickPoints[2] = new Point();

    setOnClickListener(
        new OnClickListener() {
          @Override
          public void onClick(View v) {
            toggle();
            mTickDrawing = false;
            mDrewDistance = 0;
            if (isChecked()) {
              startCheckedAnimation();
            } else {
              startUnCheckedAnimation();
            }
          }
        });
  }
Example #2
0
  private int measureSize(int measureSpec) {
    int defSize = CompatUtils.dp2px(getContext(), DEF_DRAW_SIZE);
    int specSize = MeasureSpec.getSize(measureSpec);
    int specMode = MeasureSpec.getMode(measureSpec);

    int result = 0;
    switch (specMode) {
      case MeasureSpec.UNSPECIFIED:
      case MeasureSpec.AT_MOST:
        result = Math.min(defSize, specSize);
        break;
      case MeasureSpec.EXACTLY:
        result = specSize;
        break;
    }
    return result;
  }