Java Code Examples for android.graphics.Canvas#setMatrix()

The following examples show how to use android.graphics.Canvas#setMatrix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BitmapLayer.java    From MapView with MIT License 6 votes vote down vote up
@Override
public void draw(Canvas canvas, Matrix currentMatrix, float currentZoom, float
        currentRotateDegrees) {
    if (isVisible && bitmap != null) {
        canvas.save();
        float[] goal = {location.x, location.y};
        if (!autoScale) {
            currentMatrix.mapPoints(goal);
        } else {
            canvas.setMatrix(currentMatrix);
        }
        canvas.drawBitmap(bitmap, goal[0] - bitmap.getWidth() / 2,
                goal[1] - bitmap.getHeight() / 2, paint);
        canvas.restore();
    }
}
 
Example 2
Source File: DrawFaceView.java    From RairDemo with Apache License 2.0 6 votes vote down vote up
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.setMatrix(matrix);
        for (Camera.Face face : faces) {
            if (face == null) {
                break;
            }
            canvas.drawRect(face.rect, paint);
            if (face.leftEye != null) {
                canvas.drawPoint(face.leftEye.x, face.leftEye.y, paint);
            }
            if (face.rightEye != null) {
                canvas.drawPoint(face.rightEye.x, face.rightEye.y, paint);
            }
            if (face.mouth != null) {
                canvas.drawPoint(face.mouth.x, face.mouth.y, paint);
            }
            // 因为旋转了画布矩阵,所以字体也跟着旋转
//            canvas.drawText(String.valueOf("id:" + face.id + "\n置信度:" + face.score), face.rect.left, face.rect.bottom + 10, paint);
        }
        if (isClear) {
            canvas.drawColor(Color.WHITE, PorterDuff.Mode.CLEAR);
            isClear = false;
        }
    }
 
Example 3
Source File: DynamicBitmapUtils.java    From dynamic-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Resize bitmap to the new width and height.
 *
 * @param bitmap The bitmap to resize.
 * @param newWidth The new width for the bitmap.
 * @param newHeight The new height for the bitmap.
 *
 * @return The resized bitmap with new width and height.
 */
public static @NonNull Bitmap resizeBitmap(@NonNull Bitmap bitmap,
        int newWidth, int newHeight) {
    Bitmap resizedBitmap = Bitmap.createBitmap(
            newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setFilterBitmap(true);

    Canvas canvas = new Canvas(resizedBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, paint);

    return resizedBitmap;
}
 
Example 4
Source File: CropView.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result = super.drawChild(canvas, child, drawingTime);
    if (child == imageView && paintingOverlay != null) {
        canvas.save();
        canvas.setMatrix(overlayMatrix);
        paintingOverlay.draw(canvas);
        canvas.restore();
    }
    return result;
}
 
Example 5
Source File: CropView.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result = super.drawChild(canvas, child, drawingTime);
    if (child == imageView && paintingOverlay != null) {
        canvas.save();
        canvas.setMatrix(overlayMatrix);
        paintingOverlay.draw(canvas);
        canvas.restore();
    }
    return result;
}
 
Example 6
Source File: ScreencastDispatcher.java    From stetho with MIT License 5 votes vote down vote up
private void updateScreenBitmap() {
  if (!mIsRunning) {
    return;
  }
  Activity activity = mActivityTracker.tryGetTopActivity();
  if (activity == null) {
    return;
  }
  // This stuff needs to happen in the UI thread
  View rootView = activity.getWindow().getDecorView();
  try {
    if (mBitmap == null) {
      int viewWidth = rootView.getWidth();
      int viewHeight = rootView.getHeight();
      float scale = Math.min((float) mRequest.maxWidth / (float) viewWidth,
          (float) mRequest.maxHeight / (float) viewHeight);
      int destWidth = (int) (viewWidth * scale);
      int destHeight = (int) (viewHeight * scale);
      mBitmap = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
      mCanvas = new Canvas(mBitmap);
      Matrix matrix = new Matrix();
      mTempSrc.set(0, 0, viewWidth, viewHeight);
      mTempDst.set(0, 0, destWidth, destHeight);
      matrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.CENTER);
      mCanvas.setMatrix(matrix);
    }
    rootView.draw(mCanvas);
  } catch (OutOfMemoryError e) {
    LogUtil.w("Out of memory trying to allocate screencast Bitmap.");
  }
}
 
Example 7
Source File: ScreencastDispatcher.java    From weex with Apache License 2.0 5 votes vote down vote up
private void updateScreenBitmap() {
  if (!mIsRunning) {
    return;
  }
  Activity activity = mActivityTracker.tryGetTopActivity();
  if (activity == null) {
    return;
  }
  // This stuff needs to happen in the UI thread
  View rootView = activity.getWindow().getDecorView();
  try {
    if (mBitmap == null) {
      int viewWidth = rootView.getWidth();
      int viewHeight = rootView.getHeight();
      float scale = Math.min((float) mRequest.maxWidth / (float) viewWidth,
          (float) mRequest.maxHeight / (float) viewHeight);
      int destWidth = (int) (viewWidth * scale);
      int destHeight = (int) (viewHeight * scale);
      mBitmap = Bitmap.createBitmap(destWidth, destHeight, Bitmap.Config.RGB_565);
      mCanvas = new Canvas(mBitmap);
      Matrix matrix = new Matrix();
      mTempSrc.set(0, 0, viewWidth, viewHeight);
      mTempDst.set(0, 0, destWidth, destHeight);
      matrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.CENTER);
      mCanvas.setMatrix(matrix);
    }
    rootView.draw(mCanvas);
  } catch (OutOfMemoryError e) {
    LogUtil.w("Out of memory trying to allocate screencast Bitmap.");
  }
}
 
Example 8
Source File: AndroidGraphics.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
public Pixmap newPixmap(Bitmap bitmap, String fileName) {
	PixmapFormat format;
	float newWidth = bitmap.getWidth();
	float newHeight = bitmap.getHeight();
	int textureWidth = determineTextureSize((int) newWidth);
	int textureHeight = determineTextureSize((int) newHeight);
	float tx2 = (float) newWidth / (float) textureWidth;
	float ty2 = (float) newHeight / (float) textureHeight;
	Bitmap scaledBitmap = Bitmap.createBitmap(textureWidth, textureHeight, bitmap.getConfig());
	
	float ratioX = newWidth / (float) bitmap.getWidth();
	float ratioY = newHeight / (float) bitmap.getHeight();
	float middleX = newWidth / 2.0f;
	float middleY = newHeight / 2.0f;

	Matrix scaleMatrix = new Matrix();
	scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

	Canvas canvas = new Canvas(scaledBitmap);
	canvas.setMatrix(scaleMatrix);
	canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
	MemUtil.freeBitmap(bitmap);
	bitmap = scaledBitmap;
	
	if (bitmap.getConfig() == Config.RGB_565) {
		format = PixmapFormat.RGB565;
	} else if (bitmap.getConfig() == Config.ARGB_4444) {
		format = PixmapFormat.ARGB4444;
	} else {
		format = PixmapFormat.ARGB8888;
	}
	AndroidPixmap pm = new AndroidPixmap(bitmap, format, fileName, textureManager, (int) newWidth, (int) newHeight, tx2, ty2);
	return pm;
}
 
Example 9
Source File: BlurKit.java    From blurkit-android with MIT License 5 votes vote down vote up
private Bitmap getBitmapForView(View src, float downscaleFactor) {
    Bitmap bitmap = Bitmap.createBitmap(
            (int) (src.getWidth() * downscaleFactor),
            (int) (src.getHeight() * downscaleFactor),
            Bitmap.Config.ARGB_8888
    );

    Canvas canvas = new Canvas(bitmap);
    Matrix matrix = new Matrix();
    matrix.preScale(downscaleFactor, downscaleFactor);
    canvas.setMatrix(matrix);
    src.draw(canvas);

    return bitmap;
}
 
Example 10
Source File: GUIDrawable.java    From geoar-app with Apache License 2.0 5 votes vote down vote up
protected void setScaleAndFocusToDrawingArea(Canvas canvas) {
	Matrix m = new Matrix();
	m.setScale(drawableArea.bottom - drawableArea.top, drawableArea.right
			- drawableArea.right);
	m.setTranslate(drawableArea.left, drawableArea.top);
	canvas.setMatrix(m);
}
 
Example 11
Source File: SVGAndroidRenderer.java    From starcor.xul with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void duplicateCanvas()
{
   try {
      Bitmap  newBM = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
      bitmapStack.push(newBM);
      Canvas  newCanvas = new Canvas(newBM);
      newCanvas.setMatrix(canvas.getMatrix());
      canvas = newCanvas;
   } catch (OutOfMemoryError e) {
      error("Not enough memory to create temporary bitmaps for mask processing");
      throw e;
   }
}
 
Example 12
Source File: SVGAndroidRenderer.java    From XDroidAnimation with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void duplicateCanvas()
{
   try {
      Bitmap  newBM = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
      bitmapStack.push(newBM);
      Canvas  newCanvas = new Canvas(newBM);
      newCanvas.setMatrix(canvas.getMatrix());
      canvas = newCanvas;
   } catch (OutOfMemoryError e) {
      error("Not enough memory to create temporary bitmaps for mask processing");
      throw e;
   }
}
 
Example 13
Source File: StackView.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
void drawOutline(Canvas dest, Bitmap src) {
    final int[] xy = mTmpXY;
    Bitmap mask = src.extractAlpha(mBlurPaint, xy);
    mMaskCanvas.setBitmap(mask);
    mMaskCanvas.drawBitmap(src, -xy[0], -xy[1], mErasePaint);
    dest.drawColor(0, PorterDuff.Mode.CLEAR);
    dest.setMatrix(mIdentityMatrix);
    dest.drawBitmap(mask, xy[0], xy[1], mHolographicPaint);
    mMaskCanvas.setBitmap(null);
    mask.recycle();
}
 
Example 14
Source File: AppBarLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}
 
Example 15
Source File: ImageView.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}
 
Example 16
Source File: TextLayer.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override
void drawLayer(Canvas canvas, Matrix parentMatrix, int parentAlpha) {
  canvas.save();
  if (!lottieDrawable.useTextGlyphs()) {
    canvas.setMatrix(parentMatrix);
  }
  DocumentData documentData = textAnimation.getValue();
  Font font = composition.getFonts().get(documentData.fontName);
  if (font == null) {
    // Something is wrong.
    canvas.restore();
    return;
  }

  if (colorCallbackAnimation != null) {
    fillPaint.setColor(colorCallbackAnimation.getValue());
  } else if (colorAnimation != null) {
    fillPaint.setColor(colorAnimation.getValue());
  } else {
    fillPaint.setColor(documentData.color);
  }

  if (strokeColorCallbackAnimation != null) {
    strokePaint.setColor(strokeColorCallbackAnimation.getValue());
  } else if (strokeColorAnimation != null) {
    strokePaint.setColor(strokeColorAnimation.getValue());
  } else {
    strokePaint.setColor(documentData.strokeColor);
  }
  int opacity = transform.getOpacity() == null ? 100 : transform.getOpacity().getValue();
  int alpha = opacity * 255 / 100;
  fillPaint.setAlpha(alpha);
  strokePaint.setAlpha(alpha);

  if (strokeWidthCallbackAnimation != null) {
    strokePaint.setStrokeWidth(strokeWidthCallbackAnimation.getValue());
  } else if (strokeWidthAnimation != null) {
    strokePaint.setStrokeWidth(strokeWidthAnimation.getValue());
  } else {
    float parentScale = Utils.getScale(parentMatrix);
    strokePaint.setStrokeWidth(documentData.strokeWidth * Utils.dpScale() * parentScale);
  }

  if (lottieDrawable.useTextGlyphs()) {
    drawTextGlyphs(documentData, parentMatrix, font, canvas);
  } else {
    drawTextWithFont(documentData, font, parentMatrix, canvas);
  }

  canvas.restore();
}
 
Example 17
Source File: FrameLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}
 
Example 18
Source File: RadialMenuView.java    From talkback with Apache License 2.0 4 votes vote down vote up
private void drawWedge(Canvas canvas, float center, int i, RadialMenu menu, float degrees) {
  final float offset = subMenu != null ? subMenuOffset : rootMenuOffset;

  final RadialMenuItem wedge = menu.getItem(i);
  final String title = wedge.getTitle().toString();
  final float rotation = ((degrees * i) + offset);
  final boolean selected = wedge.equals(focusedItem);

  // Apply the appropriate color filters.
  if (wedge.hasSubMenu()) {
    paint.setColorFilter(subMenuFilter);
  } else {
    paint.setColorFilter(null);
  }

  wedge.offset = rotation;

  tempMatrix.reset();
  tempMatrix.setRotate(rotation, center, center);
  tempMatrix.postTranslate((this.center.x - center), (this.center.y - center));
  canvas.setMatrix(tempMatrix);

  paint.setStyle(Style.FILL);
  paint.setColor(selected ? selectionColor : outerFillColor);
  paint.setShadowLayer(shadowRadius, 0, 0, (selected ? selectionShadowColor : textShadowColor));
  canvas.drawPath(cachedOuterPath, paint);
  paint.setShadowLayer(0, 0, 0, 0);

  paint.setStyle(Style.FILL);
  paint.setColor(selected ? selectionTextFillColor : textFillColor);
  paint.setTextAlign(Align.CENTER);
  paint.setTextSize(textSize);
  paint.setShadowLayer(textShadowRadius, 0, 0, textShadowColor);

  final String renderText = getEllipsizedText(paint, title, cachedOuterPathWidth);

  // Orient text differently depending on the angle.
  if ((rotation < 90) || (rotation > 270)) {
    canvas.drawTextOnPath(renderText, cachedOuterPathReverse, 0, (2 * textSize), paint);
  } else {
    canvas.drawTextOnPath(renderText, cachedOuterPath, 0, -textSize, paint);
  }

  paint.setShadowLayer(0, 0, 0, 0);
  paint.setColorFilter(null);
}
 
Example 19
Source File: FlowLayout.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}
 
Example 20
Source File: Button.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public void drawShadow(@NotNull Canvas canvas) {
    float alpha = getAlpha() * Carbon.getBackgroundTintAlpha(this) / 255.0f;
    if (alpha == 0 || !hasShadow())
        return;

    float z = getElevation() + getTranslationZ();

    int saveCount;
    boolean maskShadow = getBackground() != null && alpha != 1;
    boolean r = revealAnimator != null && revealAnimator.isRunning();

    if (alpha != 255) {
        paint.setAlpha((int) (127 * alpha));
        saveCount = canvas.saveLayer(0, 0, canvas.getWidth(), canvas.getHeight(), paint, Canvas.ALL_SAVE_FLAG);
    } else {
        saveCount = canvas.save();
    }
    Matrix matrix = getMatrix();
    canvas.setMatrix(matrix);

    if (r) {
        canvas.clipRect(
                getLeft() + revealAnimator.x - revealAnimator.radius, getTop() + revealAnimator.y - revealAnimator.radius,
                getLeft() + revealAnimator.x + revealAnimator.radius, getTop() + revealAnimator.y + revealAnimator.radius);
    }

    shadowDrawable.setFillColor(spotShadowColor);
    shadowDrawable.setShadowColor(spotShadowColor != null ? spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()) : 0xff000000);
    shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_ALWAYS);
    shadowDrawable.setAlpha(0x44);
    shadowDrawable.setElevation(z);
    shadowDrawable.setShadowVerticalOffset(0);
    shadowDrawable.setBounds(getLeft(), (int) (getTop() + z / 4), getRight(), (int) (getBottom() + z / 4));
    shadowDrawable.draw(canvas);

    canvas.translate(this.getLeft(), this.getTop());
    canvas.concat(matrix);
    paint.setXfermode(Carbon.CLEAR_MODE);
    if (maskShadow) {
        cornersMask.setFillType(Path.FillType.WINDING);
        canvas.drawPath(cornersMask, paint);
    }
    if (r)
        canvas.drawPath(revealAnimator.mask, paint);

    canvas.restoreToCount(saveCount);
    paint.setXfermode(null);
    paint.setAlpha(255);
}