public void draw(@NonNull Canvas canvas) {
   if (!hasValue()) {
     return;
   }
   if (offset.x == 0 && offset.y == 0) {
     calculatePosition();
   }
   if (highContrast) {
     paint.setColor(contrastColor);
     paint.setAlpha(255);
   }
   final int width = view.getWidth();
   final int height = view.getHeight();
   switch (direction) {
     case up:
       canvas.drawText(value, width + offset.x, offset.y, paint);
       break;
     case down:
       canvas.drawText(value, width + offset.x, height + offset.y, paint);
       break;
     case left:
       canvas.drawText(value, offset.x, height / 2 + offset.y, paint);
       break;
     case right:
       canvas.drawText(value, width + offset.x, height / 2 + offset.y, paint);
       break;
   }
   if (highContrast) {
     paint.setColor(color);
     paint.setAlpha(intAlpha());
   }
 }
 protected void updateTextPaint(TextPaint ds) {
   if (isVisible()) {
     ds.setTextSize(ds.getTextSize() * 1.0f);
     ds.setAlpha(ds.getAlpha());
   } else {
     ds.setTextSize(ds.getTextSize() * mInvisibleSize);
     ds.setAlpha(0); // ensure the text is not visible
   }
 }
Ejemplo n.º 3
0
  private void drawPlaceHolder(Canvas canvas) {
    placeholderPaint.setColor(layout.placeHolderColor);

    placeholderTextPaint.setAlpha(255);
    placeholderPaint.setAlpha(255);
    avatarPaint.setAlpha(255);

    rect.set(
        layout.layoutAvatarLeft,
        layout.layoutAvatarTop,
        layout.layoutAvatarLeft + layout.layoutAvatarWidth,
        layout.layoutAvatarTop + layout.layoutAvatarWidth);

    canvas.drawRect(rect, placeholderPaint);
    if (layout.usePlaceholder) {
      canvas.drawText(
          layout.placeHolderName,
          layout.placeholderLeft,
          layout.placeholderTop,
          placeholderTextPaint);
    } else {
      rect2.set(0, 0, placeholder.getWidth(), placeholder.getHeight());
      canvas.drawBitmap(placeholder, rect2, rect, avatarPaint);
    }
  }
  @SuppressWarnings("NullableProblems")
  @Override
  protected void onDraw(Canvas canvas) {

    // Draw Title Text
    if (mAttributes.isShowTitle()
        && mTitlePaint != null
        && mCurTitleProperty != null
        && mCurTitleProperty.mAlpha != 0) {
      CharSequence buf = getHint();
      if (buf != null) {
        mTitlePaint.setTextSize(mCurTitleProperty.mTextSize);

        int color = getCurrentTitleTextColor();
        int alpha = GeniusUi.modulateAlpha(Color.alpha(color), mCurTitleProperty.mAlpha);

        mTitlePaint.setColor(color);
        mTitlePaint.setAlpha(alpha);

        canvas.drawText(
            buf,
            0,
            buf.length(),
            mCurTitleProperty.mPaddingLeft,
            mCurTitleProperty.mPaddingTop + mCurTitleProperty.mTextSize,
            mTitlePaint);
      }
    }

    super.onDraw(canvas);
  }
 public void setColor(int color, float alpha) {
   if (this.color == color && this.alpha == alpha) {
     return;
   }
   this.color = color;
   this.contrastColor = makeContrastColor(color);
   this.alpha = alpha;
   paint.setColor(color);
   paint.setAlpha(intAlpha());
   initPaintShadow();
   invalidate(false);
 }
    public void initPaint(@NonNull TextPaint base) {
      paint.set(base);
      paint.setColor(color);
      paint.setAlpha(intAlpha());
      final Typeface typeface = base.getTypeface();
      if (typeface != null && typeface.getStyle() != Typeface.NORMAL) {
        paint.setTypeface(Typeface.create(typeface, Typeface.NORMAL));
      }

      // pre-calculate fixed height
      paint.setTextSize(Math.max(base.getTextSize() * DEF_SCALE, minTextSize));
      paint.getTextBounds("|", 0, 1, bounds);
      fixedTextHeight = bounds.height();

      // set real text size value
      paint.setTextSize(Math.max(base.getTextSize() * scale, minTextSize));

      initPaintShadow();
      invalidate(true);
    }
  @Override
  protected void onDraw(@NonNull Canvas canvas) {
    int startX = getScrollX() + (iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding));
    int endX =
        getScrollX()
            + (iconRightBitmaps == null ? getWidth() : getWidth() - iconOuterWidth - iconPadding);
    int lineStartY = getScrollY() + getHeight() - getPaddingBottom();

    // draw the icon(s)
    paint.setAlpha(255);
    if (iconLeftBitmaps != null) {
      Bitmap icon = iconLeftBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0];
      int iconLeft = startX - iconPadding - iconOuterWidth + (iconOuterWidth - icon.getWidth()) / 2;
      int iconTop =
          lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2;
      canvas.drawBitmap(icon, iconLeft, iconTop, paint);
    }
    if (iconRightBitmaps != null) {
      Bitmap icon =
          iconRightBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0];
      int iconRight = endX + iconPadding + (iconOuterWidth - icon.getWidth()) / 2;
      int iconTop =
          lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2;
      canvas.drawBitmap(icon, iconRight, iconTop, paint);
    }

    // draw the clear button
    if (hasFocus() && showClearButton && !TextUtils.isEmpty(getText()) && isEnabled()) {
      paint.setAlpha(255);
      int buttonLeft;
      if (isRTL()) {
        buttonLeft = startX;
      } else {
        buttonLeft = endX - iconOuterWidth;
      }
      Bitmap clearButtonBitmap = clearButtonBitmaps[0];
      buttonLeft += (iconOuterWidth - clearButtonBitmap.getWidth()) / 2;
      int iconTop =
          lineStartY
              + bottomSpacing
              - iconOuterHeight
              + (iconOuterHeight - clearButtonBitmap.getHeight()) / 2;
      canvas.drawBitmap(clearButtonBitmap, buttonLeft, iconTop, paint);
    }

    // draw the underline
    if (!hideUnderline) {
      lineStartY += bottomSpacing;
      if (!isInternalValid()) { // not valid
        paint.setColor(errorColor);
        canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
      } else if (!isEnabled()) { // disabled
        paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x44000000);
        float interval = getPixel(1);
        for (float xOffset = 0; xOffset < getWidth(); xOffset += interval * 3) {
          canvas.drawRect(
              startX + xOffset,
              lineStartY,
              startX + xOffset + interval,
              lineStartY + getPixel(1),
              paint);
        }
      } else if (hasFocus()) { // focused
        paint.setColor(primaryColor);
        canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
      } else { // normal
        paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x1E000000);
        canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint);
      }
    }

    textPaint.setTextSize(bottomTextSize);
    Paint.FontMetrics textMetrics = textPaint.getFontMetrics();
    float relativeHeight = -textMetrics.ascent - textMetrics.descent;
    float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent;

    // draw the characters counter
    if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) {
      textPaint.setColor(
          isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor);
      String charactersCounterText = getCharactersCounterText();
      canvas.drawText(
          charactersCounterText,
          isRTL() ? startX : endX - textPaint.measureText(charactersCounterText),
          lineStartY + bottomSpacing + relativeHeight,
          textPaint);
    }

    // draw the bottom text
    if (textLayout != null) {
      if (tempErrorText != null
          || ((helperTextAlwaysShown || hasFocus())
              && !TextUtils.isEmpty(helperText))) { // error text or helper text
        textPaint.setColor(
            tempErrorText != null
                ? errorColor
                : helperTextColor != -1 ? helperTextColor : (baseColor & 0x00ffffff | 0x44000000));
        canvas.save();
        if (isRTL()) {
          canvas.translate(
              endX - textLayout.getWidth(), lineStartY + bottomSpacing - bottomTextPadding);
        } else {
          canvas.translate(
              startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding);
        }
        textLayout.draw(canvas);
        canvas.restore();
      }
    }

    // draw the floating label
    if (floatingLabelEnabled && !TextUtils.isEmpty(floatingLabelText)) {
      textPaint.setTextSize(floatingLabelTextSize);
      // calculate the text color
      textPaint.setColor(
          (Integer)
              focusEvaluator.evaluate(
                  focusFraction * (isEnabled() ? 1 : 0),
                  floatingLabelTextColor != -1
                      ? floatingLabelTextColor
                      : (baseColor & 0x00ffffff | 0x44000000),
                  primaryColor));

      // calculate the horizontal position
      float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
      int floatingLabelStartX;
      if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL()) {
        floatingLabelStartX = (int) (endX - floatingLabelWidth);
      } else if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
        floatingLabelStartX = startX;
      } else {
        floatingLabelStartX =
            startX
                + (int)
                    (getInnerPaddingLeft()
                        + (getWidth()
                                - getInnerPaddingLeft()
                                - getInnerPaddingRight()
                                - floatingLabelWidth)
                            / 2);
      }

      // calculate the vertical position
      int distance = floatingLabelPadding;
      int floatingLabelStartY =
          (int)
              (innerPaddingTop
                  + floatingLabelTextSize
                  + floatingLabelPadding
                  - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction)
                  + getScrollY());

      // calculate the alpha
      int alpha =
          ((int)
              ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction)
                  * 0xff
                  * (0.74f * focusFraction * (isEnabled() ? 1 : 0) + 0.26f)
                  * (floatingLabelTextColor != -1
                      ? 1
                      : Color.alpha(floatingLabelTextColor) / 256f)));
      textPaint.setAlpha(alpha);

      // draw the floating label
      canvas.drawText(
          floatingLabelText.toString(), floatingLabelStartX, floatingLabelStartY, textPaint);
    }

    // draw the bottom ellipsis
    if (hasFocus() && singleLineEllipsis && getScrollX() != 0) {
      paint.setColor(isInternalValid() ? primaryColor : errorColor);
      float startY = lineStartY + bottomSpacing;
      int ellipsisStartX;
      if (isRTL()) {
        ellipsisStartX = endX;
      } else {
        ellipsisStartX = startX;
      }
      int signum = isRTL() ? -1 : 1;
      canvas.drawCircle(
          ellipsisStartX + signum * bottomEllipsisSize / 2,
          startY + bottomEllipsisSize / 2,
          bottomEllipsisSize / 2,
          paint);
      canvas.drawCircle(
          ellipsisStartX + signum * bottomEllipsisSize * 5 / 2,
          startY + bottomEllipsisSize / 2,
          bottomEllipsisSize / 2,
          paint);
      canvas.drawCircle(
          ellipsisStartX + signum * bottomEllipsisSize * 9 / 2,
          startY + bottomEllipsisSize / 2,
          bottomEllipsisSize / 2,
          paint);
    }

    // draw the original things
    super.onDraw(canvas);
  }