Example usage for android.graphics Shader setLocalMatrix

List of usage examples for android.graphics Shader setLocalMatrix

Introduction

In this page you can find the example usage for android.graphics Shader setLocalMatrix.

Prototype

public void setLocalMatrix(@Nullable Matrix localM) 

Source Link

Document

Set the shader's local matrix.

Usage

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;//from   w  w  w. ja  v  a2  s. co m
    }
    MasterImage master = MasterImage.getImage();
    Matrix m = master.computeImageToScreen(image, 0, false);
    if (m == null) {
        return;
    }

    canvas.save();

    RectF d = new RectF(0, 0, image.getWidth(), image.getHeight());
    m.mapRect(d);
    d.roundOut(mImageBounds);

    boolean showAnimatedImage = master.onGoingNewLookAnimation();
    if (!showAnimatedImage && mDidStartAnimation) {
        // animation ended, but do we have the correct image to show?
        if (master.getPreset().equals(master.getCurrentPreset())) {
            // we do, let's stop showing the animated image
            mDidStartAnimation = false;
            MasterImage.getImage().resetAnimBitmap();
        } else {
            showAnimatedImage = true;
        }
    } else if (showAnimatedImage) {
        mDidStartAnimation = true;
    }

    if (showAnimatedImage) {
        canvas.save();

        // Animation uses the image before the change
        Bitmap previousImage = master.getPreviousImage();
        Matrix mp = master.computeImageToScreen(previousImage, 0, false);
        RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight());
        mp.mapRect(dp);
        Rect previousBounds = new Rect();
        dp.roundOut(previousBounds);
        float centerX = dp.centerX();
        float centerY = dp.centerY();
        boolean needsToDrawImage = true;

        if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) {
            float maskScale = MasterImage.getImage().getMaskScale();
            if (maskScale >= 0.0f) {
                float maskW = sMask.getWidth() / 2.0f;
                float maskH = sMask.getHeight() / 2.0f;
                Point point = mActivity.hintTouchPoint(this);
                float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH);
                maskScale = maskScale * maxMaskScale;
                float x = point.x - maskW * maskScale;
                float y = point.y - maskH * maskScale;

                // Prepare the shader
                mShaderMatrix.reset();
                mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale);
                mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top);
                float scaleImageX = mImageBounds.width() / (float) image.getWidth();
                float scaleImageY = mImageBounds.height() / (float) image.getHeight();
                mShaderMatrix.preScale(scaleImageX, scaleImageY);
                mMaskPaint.reset();
                Shader maskShader = createShader(image);
                maskShader.setLocalMatrix(mShaderMatrix);
                mMaskPaint.setShader(maskShader);

                drawShadow(canvas, mImageBounds); // as needed
                canvas.drawBitmap(previousImage, m, mPaint);
                canvas.clipRect(mImageBounds);
                canvas.translate(x, y);
                canvas.scale(maskScale, maskScale);
                canvas.drawBitmap(sMask, 0, 0, mMaskPaint);
                needsToDrawImage = false;
            }
        } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) {
            Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(),
                    master.getPreviousImage().getWidth());
            Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(),
                    master.getPreviousImage().getHeight());
            float finalScale = d1.width() / (float) d2.height();
            finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction());
            canvas.rotate(master.getAnimRotationValue(), centerX, centerY);
            canvas.scale(finalScale, finalScale, centerX, centerY);
        } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) {
            if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) {
                FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master
                        .getCurrentFilterRepresentation();

                ImagePreset preset = master.getPreset();
                ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset
                        .getGeometryFilters();
                GeometryMathUtils.GeometryHolder holder = null;
                holder = GeometryMathUtils.unpackGeometry(geometry);

                if (holder.rotation.value() == 90 || holder.rotation.value() == 270) {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    }
                } else {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    }
                }
            }
        }

        if (needsToDrawImage) {
            drawShadow(canvas, previousBounds); // as needed
            canvas.drawBitmap(previousImage, mp, mPaint);
        }

        canvas.restore();
    } else {
        drawShadow(canvas, mImageBounds); // as needed
        canvas.drawBitmap(image, m, mPaint);
    }

    canvas.restore();
}