private RadialGradient getShader(ColorChangeTask task) {
    if (mShader == null) {
      if (task.isOut) {
        int color_middle = ColorUtil.getColor(mCurColor, 0f);
        mShader =
            new RadialGradient(
                task.x,
                task.y,
                GRADIENT_RADIUS,
                new int[] {0, color_middle, mCurColor},
                GRADIENT_STOPS,
                Shader.TileMode.CLAMP);
      } else {
        int color_middle = ColorUtil.getColor(task.color, 0f);
        mShader =
            new RadialGradient(
                task.x,
                task.y,
                GRADIENT_RADIUS,
                new int[] {0, color_middle, task.color},
                GRADIENT_STOPS,
                Shader.TileMode.CLAMP);
      }
    }

    return mShader;
  }
Beispiel #2
0
  @Override
  public void draw(@NonNull Canvas canvas) {
    super.draw(canvas);

    float x =
        (mDrawRect.width() - mThumbRadius * 2) * mThumbPosition + mDrawRect.left + mThumbRadius;
    if (mIsRtl) x = 2 * mDrawRect.centerX() - x;
    float y = mDrawRect.centerY();

    getTrackPath(x, y, mThumbRadius);
    mPaint.setColor(
        ColorUtil.getMiddleColor(getTrackColor(false), getTrackColor(true), mThumbPosition));
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawPath(mTrackPath, mPaint);

    if (mShadowSize > 0) {
      int saveCount = canvas.save();
      canvas.translate(x, y + mShadowOffset);
      canvas.drawPath(mShadowPath, mShadowPaint);
      canvas.restoreToCount(saveCount);
    }

    mPaint.setColor(
        ColorUtil.getMiddleColor(getThumbColor(false), getThumbColor(true), mThumbPosition));
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(x, y, mThumbRadius, mPaint);
  }
  private void applyStyle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    TypedArray a =
        context.obtainStyledAttributes(attrs, R.styleable.TimePicker, defStyleAttr, defStyleRes);
    mBackgroundColor =
        a.getColor(
            R.styleable.TimePicker_tp_backgroundColor,
            ColorUtil.getColor(ThemeUtil.colorPrimary(context, 0xFF000000), 0.25f));
    mSelectionColor =
        a.getColor(
            R.styleable.TimePicker_tp_selectionColor, ThemeUtil.colorPrimary(context, 0xFF000000));
    mSelectionRadius =
        a.getDimensionPixelOffset(
            R.styleable.TimePicker_tp_selectionRadius, ThemeUtil.dpToPx(context, 8));
    mTickSize =
        a.getDimensionPixelSize(R.styleable.TimePicker_tp_tickSize, ThemeUtil.dpToPx(context, 1));
    mTextSize =
        a.getDimensionPixelSize(
            R.styleable.TimePicker_tp_textSize,
            context.getResources().getDimensionPixelOffset(R.dimen.abc_text_size_caption_material));
    mTextColor = a.getColor(R.styleable.TimePicker_tp_textColor, 0xFF000000);
    mTextHighlightColor = a.getColor(R.styleable.TimePicker_tp_textHighlightColor, 0xFFFFFFFF);
    mAnimDuration =
        a.getInteger(
            R.styleable.TimePicker_tp_animDuration,
            context.getResources().getInteger(android.R.integer.config_mediumAnimTime));
    int resId = a.getResourceId(R.styleable.TimePicker_tp_inInterpolator, 0);
    mInInterpolator =
        resId == 0 ? new DecelerateInterpolator() : AnimationUtils.loadInterpolator(context, resId);
    resId = a.getResourceId(R.styleable.TimePicker_tp_outInterpolator, 0);
    mOutInterpolator =
        resId == 0 ? new DecelerateInterpolator() : AnimationUtils.loadInterpolator(context, resId);
    setMode(a.getInteger(R.styleable.TimePicker_tp_mode, mMode), false);
    set24Hour(a.getBoolean(R.styleable.TimePicker_tp_24Hour, m24Hour));
    setHour(a.getInteger(R.styleable.TimePicker_tp_hour, mHour));
    setMinute(a.getInteger(R.styleable.TimePicker_tp_minute, mMinute));

    String familyName = a.getString(R.styleable.TimePicker_tp_fontFamily);
    int style = a.getInteger(R.styleable.TimePicker_tp_textStyle, Typeface.NORMAL);

    mTypeface = TypefaceUtil.load(context, familyName, style);

    a.recycle();
  }
Beispiel #4
0
  protected void applyStyle(
      Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    getRippleManager().onCreate(this, context, attrs, defStyleAttr, defStyleRes);

    TypedArray a =
        context.obtainStyledAttributes(attrs, R.styleable.Switch, defStyleAttr, defStyleRes);

    for (int i = 0, count = a.getIndexCount(); i < count; i++) {
      int attr = a.getIndex(i);
      if (attr == R.styleable.Switch_sw_trackSize) mTrackSize = a.getDimensionPixelSize(attr, 0);
      else if (attr == R.styleable.Switch_sw_trackColor) mTrackColors = a.getColorStateList(attr);
      else if (attr == R.styleable.Switch_sw_trackCap) {
        int cap = a.getInteger(attr, 0);
        if (cap == 0) mTrackCap = Paint.Cap.BUTT;
        else if (cap == 1) mTrackCap = Paint.Cap.ROUND;
        else mTrackCap = Paint.Cap.SQUARE;
      } else if (attr == R.styleable.Switch_sw_thumbColor) mThumbColors = a.getColorStateList(attr);
      else if (attr == R.styleable.Switch_sw_thumbRadius)
        mThumbRadius = a.getDimensionPixelSize(attr, 0);
      else if (attr == R.styleable.Switch_sw_thumbElevation) {
        mShadowSize = a.getDimensionPixelSize(attr, 0);
        mShadowOffset = mShadowSize / 2;
      } else if (attr == R.styleable.Switch_sw_animDuration) mMaxAnimDuration = a.getInt(attr, 0);
      else if (attr == R.styleable.Switch_android_gravity) mGravity = a.getInt(attr, 0);
      else if (attr == R.styleable.Switch_android_checked)
        setCheckedImmediately(a.getBoolean(attr, mChecked));
      else if (attr == R.styleable.Switch_sw_interpolator) {
        int resId = a.getResourceId(R.styleable.Switch_sw_interpolator, 0);
        if (resId != 0) mInterpolator = AnimationUtils.loadInterpolator(context, resId);
      }
    }

    a.recycle();

    if (mTrackSize < 0) mTrackSize = ThemeUtil.dpToPx(context, 2);

    if (mThumbRadius < 0) mThumbRadius = ThemeUtil.dpToPx(context, 8);

    if (mShadowSize < 0) {
      mShadowSize = ThemeUtil.dpToPx(context, 2);
      mShadowOffset = mShadowSize / 2;
    }

    if (mMaxAnimDuration < 0)
      mMaxAnimDuration = context.getResources().getInteger(android.R.integer.config_mediumAnimTime);

    if (mInterpolator == null) mInterpolator = new DecelerateInterpolator();

    if (mTrackColors == null) {
      int[][] states =
          new int[][] {
            new int[] {-android.R.attr.state_checked}, new int[] {android.R.attr.state_checked},
          };
      int[] colors =
          new int[] {
            ColorUtil.getColor(ThemeUtil.colorControlNormal(context, 0xFF000000), 0.5f),
            ColorUtil.getColor(ThemeUtil.colorControlActivated(context, 0xFF000000), 0.5f),
          };

      mTrackColors = new ColorStateList(states, colors);
    }

    if (mThumbColors == null) {
      int[][] states =
          new int[][] {
            new int[] {-android.R.attr.state_checked}, new int[] {android.R.attr.state_checked},
          };
      int[] colors =
          new int[] {
            0xFAFAFA, ThemeUtil.colorControlActivated(context, 0xFF000000),
          };

      mThumbColors = new ColorStateList(states, colors);
    }

    mPaint.setStrokeCap(mTrackCap);
    buildShadow();
    invalidate();
  }
  @Override
  public void draw(Canvas canvas) {
    super.draw(canvas);

    mPaint.setColor(mBackgroundColor);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mOuterRadius, mPaint);

    if (!mRunning) {
      float angle;
      int selectedTick;
      int start;
      int length;
      float radius;

      if (mMode == MODE_HOUR) {
        angle = getAngle(mHour, MODE_HOUR);
        selectedTick = getSelectedTick(mHour, MODE_HOUR);
        start = 0;
        length = m24Hour ? 24 : 12;
        radius = m24Hour && selectedTick < 12 ? mSecondInnerRadius : mInnerRadius;
      } else {
        angle = getAngle(mMinute, MODE_MINUTE);
        selectedTick = getSelectedTick(mMinute, MODE_MINUTE);
        start = 24;
        length = 12;
        radius = mInnerRadius;
      }

      mPaint.setColor(mSelectionColor);
      float x = mCenterPoint.x + (float) Math.cos(angle) * radius;
      float y = mCenterPoint.y + (float) Math.sin(angle) * radius;
      canvas.drawCircle(x, y, mSelectionRadius, mPaint);

      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setStrokeWidth(mTickSize);
      x -= (float) Math.cos(angle) * mSelectionRadius;
      y -= (float) Math.sin(angle) * mSelectionRadius;
      canvas.drawLine(mCenterPoint.x, mCenterPoint.y, x, y, mPaint);

      mPaint.setStyle(Paint.Style.FILL);
      mPaint.setColor(mTextColor);
      canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mTickSize * 2, mPaint);

      mPaint.setTextSize(mTextSize);
      mPaint.setTypeface(mTypeface);
      mPaint.setTextAlign(Paint.Align.CENTER);

      int index;
      for (int i = 0; i < length; i++) {
        index = start + i;
        mPaint.setColor(index == selectedTick ? mTextHighlightColor : mTextColor);
        canvas.drawText(TICKS[index], mLocations[index * 2], mLocations[index * 2 + 1], mPaint);
      }
    } else {
      float maxOffset = mOuterRadius - mInnerRadius + mTextSize / 2;
      int textOutColor = ColorUtil.getColor(mTextColor, 1f - mAnimProgress);
      int textHighlightOutColor = ColorUtil.getColor(mTextHighlightColor, 1f - mAnimProgress);
      int textInColor = ColorUtil.getColor(mTextColor, mAnimProgress);
      int textHighlightInColor = ColorUtil.getColor(mTextHighlightColor, mAnimProgress);
      float outOffset;
      float inOffset;
      float outAngle;
      float inAngle;
      int outStart;
      int inStart;
      int outLength;
      int inLength;
      int outSelectedTick;
      int inSelectedTick;
      float outRadius;
      float inRadius;

      if (mMode == MODE_MINUTE) {
        outAngle = getAngle(mHour, MODE_HOUR);
        inAngle = getAngle(mMinute, MODE_MINUTE);
        outOffset = mOutInterpolator.getInterpolation(mAnimProgress) * maxOffset;
        inOffset = (1f - mInInterpolator.getInterpolation(mAnimProgress)) * -maxOffset;
        outSelectedTick = getSelectedTick(mHour, MODE_HOUR);
        inSelectedTick = getSelectedTick(mMinute, MODE_MINUTE);
        outStart = 0;
        outLength = m24Hour ? 24 : 12;
        outRadius = m24Hour && outSelectedTick < 12 ? mSecondInnerRadius : mInnerRadius;
        inStart = 24;
        inLength = 12;
        inRadius = mInnerRadius;
      } else {
        outAngle = getAngle(mMinute, MODE_MINUTE);
        inAngle = getAngle(mHour, MODE_HOUR);
        outOffset = mOutInterpolator.getInterpolation(mAnimProgress) * -maxOffset;
        inOffset = (1f - mInInterpolator.getInterpolation(mAnimProgress)) * maxOffset;
        outSelectedTick = getSelectedTick(mMinute, MODE_MINUTE);
        inSelectedTick = getSelectedTick(mHour, MODE_HOUR);
        outStart = 24;
        outLength = 12;
        outRadius = mInnerRadius;
        inStart = 0;
        inLength = m24Hour ? 24 : 12;
        inRadius = m24Hour && inSelectedTick < 12 ? mSecondInnerRadius : mInnerRadius;
      }

      mPaint.setColor(ColorUtil.getColor(mSelectionColor, 1f - mAnimProgress));
      float x = mCenterPoint.x + (float) Math.cos(outAngle) * (outRadius + outOffset);
      float y = mCenterPoint.y + (float) Math.sin(outAngle) * (outRadius + outOffset);
      canvas.drawCircle(x, y, mSelectionRadius, mPaint);

      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setStrokeWidth(mTickSize);
      x -= (float) Math.cos(outAngle) * mSelectionRadius;
      y -= (float) Math.sin(outAngle) * mSelectionRadius;
      canvas.drawLine(mCenterPoint.x, mCenterPoint.y, x, y, mPaint);

      mPaint.setStyle(Paint.Style.FILL);
      mPaint.setColor(ColorUtil.getColor(mSelectionColor, mAnimProgress));
      x = mCenterPoint.x + (float) Math.cos(inAngle) * (inRadius + inOffset);
      y = mCenterPoint.y + (float) Math.sin(inAngle) * (inRadius + inOffset);
      canvas.drawCircle(x, y, mSelectionRadius, mPaint);

      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setStrokeWidth(mTickSize);
      x -= (float) Math.cos(inAngle) * mSelectionRadius;
      y -= (float) Math.sin(inAngle) * mSelectionRadius;
      canvas.drawLine(mCenterPoint.x, mCenterPoint.y, x, y, mPaint);

      mPaint.setStyle(Paint.Style.FILL);
      mPaint.setColor(mTextColor);
      canvas.drawCircle(mCenterPoint.x, mCenterPoint.y, mTickSize * 2, mPaint);

      mPaint.setTextSize(mTextSize);
      mPaint.setTypeface(mTypeface);
      mPaint.setTextAlign(Paint.Align.CENTER);

      double step = Math.PI / 6;
      double angle = -Math.PI / 3;
      int index;

      for (int i = 0; i < outLength; i++) {
        index = i + outStart;
        x = mLocations[index * 2] + (float) Math.cos(angle) * outOffset;
        y = mLocations[index * 2 + 1] + (float) Math.sin(angle) * outOffset;
        mPaint.setColor(index == outSelectedTick ? textHighlightOutColor : textOutColor);
        canvas.drawText(TICKS[index], x, y, mPaint);
        angle += step;
      }

      for (int i = 0; i < inLength; i++) {
        index = i + inStart;
        x = mLocations[index * 2] + (float) Math.cos(angle) * inOffset;
        y = mLocations[index * 2 + 1] + (float) Math.sin(angle) * inOffset;
        mPaint.setColor(index == inSelectedTick ? textHighlightInColor : textInColor);
        canvas.drawText(TICKS[index], x, y, mPaint);
        angle += step;
      }
    }
  }