public void init(AttributeSet attrs) {
    TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.SlantedTextView);

    mTextSize = array.getDimension(R.styleable.SlantedTextView_slantedTextSize, mTextSize);
    mTextColor = array.getColor(R.styleable.SlantedTextView_slantedTextColor, mTextColor);
    mSlantedLength = array.getDimension(R.styleable.SlantedTextView_slantedLength, mSlantedLength);
    mSlantedBackgroundColor =
        array.getColor(R.styleable.SlantedTextView_slantedBackgroundColor, mSlantedBackgroundColor);

    if (array.hasValue(R.styleable.SlantedTextView_slantedText)) {
      mSlantedText = array.getString(R.styleable.SlantedTextView_slantedText);
    }

    if (array.hasValue(R.styleable.SlantedTextView_slantedMode)) {
      mMode = array.getInt(R.styleable.SlantedTextView_slantedMode, 0);
    }
    array.recycle();

    mPaint = new Paint();
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
    mPaint.setAntiAlias(true);
    mPaint.setColor(mSlantedBackgroundColor);

    mTextPaint = new TextPaint();
    mTextPaint.setAntiAlias(true);
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.ADD));
    mTextPaint.setColor(mTextColor);
    mTextPaint.setTextAlign(Paint.Align.CENTER);
  }
示例#2
0
  @Override
  public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    freeze();
    Drawable restoreBackground = this.getBackground();
    Drawable[] restoreDrawables = this.getCompoundDrawables();
    int restoreColor = this.getCurrentTextColor();

    this.setCompoundDrawables(null, null, null, null);

    for (Shadow shadow : outerShadows) {
      this.setShadowLayer(shadow.r, shadow.dx, shadow.dy, shadow.color);
      super.onDraw(canvas);
    }
    this.setShadowLayer(0, 0, 0, 0);
    this.setTextColor(restoreColor);

    if (this.foregroundDrawable != null && this.foregroundDrawable instanceof BitmapDrawable) {
      generateTempCanvas();
      super.onDraw(tempCanvas);
      Paint paint = ((BitmapDrawable) this.foregroundDrawable).getPaint();
      paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
      this.foregroundDrawable.setBounds(canvas.getClipBounds());
      this.foregroundDrawable.draw(tempCanvas);
      canvas.drawBitmap(tempBitmap, 0, 0, null);
      tempCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
    }

    if (strokeColor != null) {
      TextPaint paint = this.getPaint();
      paint.setStyle(Style.STROKE);
      paint.setStrokeJoin(strokeJoin);
      paint.setStrokeMiter(strokeMiter);
      this.setTextColor(strokeColor);
      paint.setStrokeWidth(strokeWidth);
      super.onDraw(canvas);
      paint.setStyle(Style.FILL);
      this.setTextColor(restoreColor);
    }
    if (innerShadows.size() > 0) {
      generateTempCanvas();
      TextPaint paint = this.getPaint();
      for (Shadow shadow : innerShadows) {
        this.setTextColor(shadow.color);
        super.onDraw(tempCanvas);
        this.setTextColor(0xFF000000);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        paint.setMaskFilter(new BlurMaskFilter(shadow.r, BlurMaskFilter.Blur.NORMAL));

        tempCanvas.save();
        tempCanvas.translate(shadow.dx, shadow.dy);
        super.onDraw(tempCanvas);
        tempCanvas.restore();
        canvas.drawBitmap(tempBitmap, 0, 0, null);
        tempCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        paint.setXfermode(null);
        paint.setMaskFilter(null);
        this.setTextColor(restoreColor);
        this.setShadowLayer(0, 0, 0, 0);
      }
    }

    if (restoreDrawables != null) {
      this.setCompoundDrawablesWithIntrinsicBounds(
          restoreDrawables[0], restoreDrawables[1], restoreDrawables[2], restoreDrawables[3]);
    }
    this.setBackgroundDrawable(restoreBackground);
    this.setTextColor(restoreColor);

    unfreeze();
  }