Search in sources :

Example 1 with StickerDrawable

use of com.customview.drawable.StickerDrawable in project StickerCamera by Skykai521.

the class EffectUtil method addStickerImage.

//添加贴纸
public static MyHighlightView addStickerImage(final ImageViewTouch processImage, Context context, final Addon sticker, final StickerCallback callback) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), sticker.getId());
    if (bitmap == null) {
        return null;
    }
    StickerDrawable drawable = new StickerDrawable(context.getResources(), bitmap);
    drawable.setAntiAlias(true);
    drawable.setMinSize(30, 30);
    final MyHighlightView hv = new MyHighlightView(processImage, R.style.AppTheme, drawable);
    //设置贴纸padding
    hv.setPadding(10);
    hv.setOnDeleteClickListener(new MyHighlightView.OnDeleteClickListener() {

        @Override
        public void onDeleteClick() {
            ((MyImageViewDrawableOverlay) processImage).removeHightlightView(hv);
            hightlistViews.remove(hv);
            ((MyImageViewDrawableOverlay) processImage).invalidate();
            callback.onRemoveSticker(sticker);
        }
    });
    Matrix mImageMatrix = processImage.getImageViewMatrix();
    int cropWidth, cropHeight;
    int x, y;
    final int width = processImage.getWidth();
    final int height = processImage.getHeight();
    // width/height of the sticker
    cropWidth = (int) drawable.getCurrentWidth();
    cropHeight = (int) drawable.getCurrentHeight();
    final int cropSize = Math.max(cropWidth, cropHeight);
    final int screenSize = Math.min(processImage.getWidth(), processImage.getHeight());
    RectF positionRect = null;
    if (cropSize > screenSize) {
        float ratio;
        float widthRatio = (float) processImage.getWidth() / cropWidth;
        float heightRatio = (float) processImage.getHeight() / cropHeight;
        if (widthRatio < heightRatio) {
            ratio = widthRatio;
        } else {
            ratio = heightRatio;
        }
        cropWidth = (int) ((float) cropWidth * (ratio / 2));
        cropHeight = (int) ((float) cropHeight * (ratio / 2));
        int w = processImage.getWidth();
        int h = processImage.getHeight();
        positionRect = new RectF(w / 2 - cropWidth / 2, h / 2 - cropHeight / 2, w / 2 + cropWidth / 2, h / 2 + cropHeight / 2);
        positionRect.inset((positionRect.width() - cropWidth) / 2, (positionRect.height() - cropHeight) / 2);
    }
    if (positionRect != null) {
        x = (int) positionRect.left;
        y = (int) positionRect.top;
    } else {
        x = (width - cropWidth) / 2;
        y = (height - cropHeight) / 2;
    }
    Matrix matrix = new Matrix(mImageMatrix);
    matrix.invert(matrix);
    float[] pts = new float[] { x, y, x + cropWidth, y + cropHeight };
    MatrixUtils.mapPoints(matrix, pts);
    RectF cropRect = new RectF(pts[0], pts[1], pts[2], pts[3]);
    Rect imageRect = new Rect(0, 0, width, height);
    hv.setup(context, mImageMatrix, imageRect, cropRect, false);
    ((MyImageViewDrawableOverlay) processImage).addHighlightView(hv);
    ((MyImageViewDrawableOverlay) processImage).setSelectedHighlightView(hv);
    hightlistViews.add(hv);
    return hv;
}
Also used : RectF(android.graphics.RectF) Bitmap(android.graphics.Bitmap) Rect(android.graphics.Rect) Matrix(android.graphics.Matrix) MyImageViewDrawableOverlay(com.customview.MyImageViewDrawableOverlay) MyHighlightView(com.customview.MyHighlightView) StickerDrawable(com.customview.drawable.StickerDrawable)

Example 2 with StickerDrawable

use of com.customview.drawable.StickerDrawable in project StickerCamera by Skykai521.

the class EffectUtil method applyOnSave.

private static void applyOnSave(Canvas mCanvas, ImageViewTouch processImage, MyHighlightView view) {
    if (view != null && view.getContent() instanceof StickerDrawable) {
        final StickerDrawable stickerDrawable = ((StickerDrawable) view.getContent());
        RectF cropRect = view.getCropRectF();
        Rect rect = new Rect((int) cropRect.left, (int) cropRect.top, (int) cropRect.right, (int) cropRect.bottom);
        Matrix rotateMatrix = view.getCropRotationMatrix();
        Matrix matrix = new Matrix(processImage.getImageMatrix());
        if (!matrix.invert(matrix)) {
        }
        int saveCount = mCanvas.save(Canvas.MATRIX_SAVE_FLAG);
        mCanvas.concat(rotateMatrix);
        stickerDrawable.setDropShadow(false);
        view.getContent().setBounds(rect);
        view.getContent().draw(mCanvas);
        mCanvas.restoreToCount(saveCount);
    }
}
Also used : RectF(android.graphics.RectF) Rect(android.graphics.Rect) Matrix(android.graphics.Matrix) StickerDrawable(com.customview.drawable.StickerDrawable)

Aggregations

Matrix (android.graphics.Matrix)2 Rect (android.graphics.Rect)2 RectF (android.graphics.RectF)2 StickerDrawable (com.customview.drawable.StickerDrawable)2 Bitmap (android.graphics.Bitmap)1 MyHighlightView (com.customview.MyHighlightView)1 MyImageViewDrawableOverlay (com.customview.MyImageViewDrawableOverlay)1