Example usage for android.graphics Canvas scale

List of usage examples for android.graphics Canvas scale

Introduction

In this page you can find the example usage for android.graphics Canvas scale.

Prototype

public void scale(float sx, float sy) 

Source Link

Document

Preconcat the current matrix with the specified scale.

Usage

From source file:android.support.graphics.drawable.VectorDrawableCompat.java

@Override
public void draw(Canvas canvas) {
    if (mDelegateDrawable != null) {
        mDelegateDrawable.draw(canvas);/*from ww  w .  j a v a2  s  .c o m*/
        return;
    }
    // We will offset the bounds for drawBitmap, so copyBounds() here instead
    // of getBounds().
    copyBounds(mTmpBounds);
    if (mTmpBounds.width() <= 0 || mTmpBounds.height() <= 0) {
        // Nothing to draw
        return;
    }

    // Color filters always override tint filters.
    final ColorFilter colorFilter = (mColorFilter == null ? mTintFilter : mColorFilter);

    // The imageView can scale the canvas in different ways, in order to
    // avoid blurry scaling, we have to draw into a bitmap with exact pixel
    // size first. This bitmap size is determined by the bounds and the
    // canvas scale.
    canvas.getMatrix(mTmpMatrix);
    mTmpMatrix.getValues(mTmpFloats);
    float canvasScaleX = Math.abs(mTmpFloats[Matrix.MSCALE_X]);
    float canvasScaleY = Math.abs(mTmpFloats[Matrix.MSCALE_Y]);

    float canvasSkewX = Math.abs(mTmpFloats[Matrix.MSKEW_X]);
    float canvasSkewY = Math.abs(mTmpFloats[Matrix.MSKEW_Y]);

    // When there is any rotation / skew, then the scale value is not valid.
    if (canvasSkewX != 0 || canvasSkewY != 0) {
        canvasScaleX = 1.0f;
        canvasScaleY = 1.0f;
    }

    int scaledWidth = (int) (mTmpBounds.width() * canvasScaleX);
    int scaledHeight = (int) (mTmpBounds.height() * canvasScaleY);
    scaledWidth = Math.min(MAX_CACHED_BITMAP_SIZE, scaledWidth);
    scaledHeight = Math.min(MAX_CACHED_BITMAP_SIZE, scaledHeight);

    if (scaledWidth <= 0 || scaledHeight <= 0) {
        return;
    }

    final int saveCount = canvas.save();
    canvas.translate(mTmpBounds.left, mTmpBounds.top);

    // Handle RTL mirroring.
    final boolean needMirroring = needMirroring();
    if (needMirroring) {
        canvas.translate(mTmpBounds.width(), 0);
        canvas.scale(-1.0f, 1.0f);
    }

    // At this point, canvas has been translated to the right position.
    // And we use this bound for the destination rect for the drawBitmap, so
    // we offset to (0, 0);
    mTmpBounds.offsetTo(0, 0);

    mVectorState.createCachedBitmapIfNeeded(scaledWidth, scaledHeight);
    if (!mAllowCaching) {
        mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
    } else {
        if (!mVectorState.canReuseCache()) {
            mVectorState.updateCachedBitmap(scaledWidth, scaledHeight);
            mVectorState.updateCacheStates();
        }
    }
    mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter, mTmpBounds);
    canvas.restoreToCount(saveCount);
}

From source file:com.raibow.yamahaspk.filtershow.imageshow.ImageShow.java

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;// w  w w  . ja v a2s. 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();
                mMaskPaint.setShader(createShader(image));
                mMaskPaint.getShader().setLocalMatrix(mShaderMatrix);

                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();
}

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

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;//from ww w.j  a  v a  2 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();
}

From source file:android.car.ui.provider.CarDrawerLayout.java

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final int height = getHeight();
    final boolean drawingContent = isContentView(child);
    int clipLeft = findContentView().getLeft();
    int clipRight = findContentView().getRight();
    final int baseAlpha = (mScrimColor & 0xff000000) >>> 24;

    final int restoreCount = canvas.save();
    if (drawingContent) {
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View v = getChildAt(i);
            if (v == child || v.getVisibility() != VISIBLE || !hasOpaqueBackground(v) || !isDrawerView(v)
                    || v.getHeight() < height) {
                continue;
            }/*from w w w  .j  av  a 2s  .  c o  m*/

            if (checkDrawerViewAbsoluteGravity(v, Gravity.LEFT)) {
                final int vright = v.getRight();
                if (vright > clipLeft) {
                    clipLeft = vright;
                }
            } else {
                final int vleft = v.getLeft();
                if (vleft < clipRight) {
                    clipRight = vleft;
                }
            }
        }
        canvas.clipRect(clipLeft, 0, clipRight, getHeight());
    }
    final boolean result = super.drawChild(canvas, child, drawingTime);
    canvas.restoreToCount(restoreCount);

    if (drawingContent) {
        int scrimAlpha = SCRIM_ENABLED
                ? (int) (baseAlpha * Math.max(0, Math.min(1, onScreen())) * MAX_SCRIM_ALPHA)
                : 0;

        if (scrimAlpha > 0) {
            int color = scrimAlpha << 24 | (mScrimColor & 0xffffff);
            mScrimPaint.setColor(color);

            canvas.drawRect(clipLeft, 0, clipRight, getHeight(), mScrimPaint);

            canvas.drawRect(clipLeft - 1, 0, clipLeft, getHeight(), mEdgeHighlightPaint);
        }

        LayoutParams drawerLp = (LayoutParams) findDrawerView().getLayoutParams();
        if (mShadow != null && checkDrawerViewAbsoluteGravity(findDrawerView(), Gravity.LEFT)) {
            final int offScreen = (int) ((1 - drawerLp.onScreen) * findDrawerView().getWidth());
            final int drawerRight = getWidth() - drawerLp.getMarginEnd() - offScreen;
            final int shadowWidth = mShadow.getIntrinsicWidth();
            final float alpha = Math.max(0, Math.min((float) drawerRight / mDragger.getEdgeSize(), 1.f));
            mShadow.setBounds(drawerRight, child.getTop(), drawerRight + shadowWidth, child.getBottom());
            mShadow.setAlpha((int) (255 * alpha * alpha * alpha));
            mShadow.draw(canvas);
        } else if (mShadow != null && checkDrawerViewAbsoluteGravity(findDrawerView(), Gravity.RIGHT)) {
            final int onScreen = (int) (findDrawerView().getWidth() * drawerLp.onScreen);
            final int drawerLeft = drawerLp.getMarginStart() + getWidth() - onScreen;
            final int shadowWidth = mShadow.getIntrinsicWidth();
            final float alpha = Math.max(0, Math.min((float) onScreen / mDragger.getEdgeSize(), 1.f));
            canvas.save();
            canvas.translate(2 * drawerLeft - shadowWidth, 0);
            canvas.scale(-1.0f, 1.0f);
            mShadow.setBounds(drawerLeft - shadowWidth, child.getTop(), drawerLeft, child.getBottom());
            mShadow.setAlpha((int) (255 * alpha * alpha * alpha * alpha));
            mShadow.draw(canvas);
            canvas.restore();
        }
    }
    return result;
}

From source file:com.aliyun.homeshell.Folder.java

void drawFolderIcon(Canvas c, int w, int h, float padding, float contentPadding, boolean dropMode,
        boolean hotSeat) {
    CellLayout mContent = mContentList.get(0);
    boolean supportCard = isSupportCardIcon();
    int count = mContent.getShortcutAndWidgetContainer().getChildCount();
    int desireWidth = mContent.getDesiredWidth();
    if (mSupportCardIcon != supportCard) {
        updateFolderLayout(supportCard);
    }//  w  w  w.java 2  s  .  co m
    updateOrientation();
    int countX = mContent.getCountX();
    int cellWidth = mContent.getCellWidth();
    int cellHeight = mContent.getCellHeight();
    int widthGap = mContent.getWidthGap();
    int heightGap = mContent.getHeightGap();
    int topPadding = mContent.getPaddingTop();
    int startPadding = mContent.getPaddingStart();
    mHotSeat = hotSeat;
    mSupportCardIcon = supportCard;
    mIconChild = count;
    mLastDesireWidth = desireWidth;
    int contentWidth = mContent.getDesiredWidth();
    float iconScale;
    if (supportCard) {
        iconScale = (float) w / contentWidth;
        padding = (int) (startPadding * iconScale);
        mIconPaddingX = contentPadding;
        if (!hotSeat)
            mIconPaddingY = contentPadding - iconScale * getFolderNamePadding();
    } else {
        iconScale = (float) (w - padding * 2) / (contentWidth - startPadding * 2);
        float div = (cellHeight * countX + heightGap * (countX - 1) - cellWidth * countX
                + widthGap * (countX - 1)) / 2 * iconScale * 0.65f;
        c.translate(0, -div);
        mIconPaddingX = contentPadding - iconScale * startPadding;
        if (!hotSeat)
            mIconPaddingY = contentPadding - div - iconScale * (getFolderNamePadding() + topPadding);
    }
    if (hotSeat)
        mIconPaddingY = BubbleTextView.sTopPaddingHotseat - iconScale * getFolderNamePadding();
    mIconScale = iconScale;

    if (supportCard) {
        c.clipRect(0, 0, w, h);
    } else
        c.clipRect(0, 0, w - padding * 2, h - padding);
    c.scale(iconScale, iconScale);
    int iconH = (int) (h / iconScale);

    int countY = (iconH - topPadding + heightGap) / (cellHeight + heightGap);
    ArrayList<View> icons = getItemsInReadingOrder(false);
    if (supportCard)
        c.translate(startPadding, topPadding);
    //YUNOS BEGIN PB
    //##modules(Homeshell): ##author:wutao.wt@alibaba-inc-com
    //##BugID:(5635800) ##date:20141213
    //##decrpition: Homeshell crash
    if (mFolderIcon == null || mFolderIcon.mHideIcon)
        return;
    //YUNOS END PB
    boolean rtl = getResources().getConfiguration().getLayoutDirection() == LAYOUT_DIRECTION_RTL;
    mPageCount = countX * (supportCard ? countX : countX + 1);
    for (int i = 0, N = mDrawingLimit = Math.min(icons.size(), countX * countY); i < N; i++) {
        int x = (rtl ? (countX - (i % countX) - 1) : (i % countX)) * (cellWidth + widthGap);
        int y = (i / countX) * (cellHeight + heightGap);
        c.translate(x, y);
        ((BubbleTextView) icons.get(i)).drawWithRect(0, 0, cellWidth, cellHeight, c);
        c.translate(-x, -y);
    }
}

From source file:com.cognizant.trumobi.PersonaLauncher.java

public void showPreviews(final View anchor, int start, int end) {
    if (mWorkspace != null && mWorkspace.getChildCount() > 0) {
        if (newPreviews) {
            showingPreviews = true;//from   ww  w . j a  v  a2  s .  c  om
            hideDesktop(true);
            mWorkspace.lock();
            mWorkspace.openSense(true);
        } else {
            // check first if it's already open
            final PopupWindow window = (PopupWindow) anchor.getTag(R.id.TAG_PREVIEW);
            if (window != null)
                return;
            Resources resources = getResources();

            PersonaWorkspace personaWorkspace = mWorkspace;
            PersonaCellLayout cell = ((PersonaCellLayout) personaWorkspace.getChildAt(start));
            float max;
            ViewGroup preview;
            max = personaWorkspace.getChildCount();
            preview = new LinearLayout(this);

            Rect r = new Rect();
            // ADW: seems sometimes this throws an out of memory error....
            // so...
            try {
                resources.getDrawable(R.drawable.pr_preview_background).getPadding(r);
            } catch (OutOfMemoryError e) {
            }
            int extraW = (int) ((r.left + r.right) * max);
            int extraH = r.top + r.bottom;

            int aW = cell.getWidth() - extraW;
            float w = aW / max;

            int width = cell.getWidth();
            int height = cell.getHeight();
            // width -= (x + cell.getRightPadding());
            // height -= (y + cell.getBottomPadding());
            if (width != 0 && height != 0) {
                showingPreviews = true;
                float scale = w / width;

                int count = end - start;

                final float sWidth = width * scale;
                float sHeight = height * scale;

                PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
                ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);

                for (int i = start; i < end; i++) {
                    ImageView image = new ImageView(this);
                    cell = (PersonaCellLayout) personaWorkspace.getChildAt(i);
                    Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight, Bitmap.Config.ARGB_8888);
                    cell.setDrawingCacheEnabled(false);
                    Canvas c = new Canvas(bitmap);
                    c.scale(scale, scale);
                    c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
                    cell.dispatchDraw(c);

                    image.setBackgroundDrawable(resources.getDrawable(R.drawable.pr_preview_background));
                    image.setImageBitmap(bitmap);
                    image.setTag(i);
                    image.setOnClickListener(handler);
                    image.setOnFocusChangeListener(handler);
                    image.setFocusable(true);
                    if (i == mWorkspace.getCurrentScreen())
                        image.requestFocus();

                    preview.addView(image, LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);

                    bitmaps.add(bitmap);
                }

                PopupWindow p = new PopupWindow(this);
                p.setContentView(preview);
                p.setWidth((int) (sWidth * count + extraW));
                p.setHeight((int) (sHeight + extraH));
                p.setAnimationStyle(R.style.AnimationPreview);
                p.setOutsideTouchable(true);
                p.setFocusable(true);
                p.setBackgroundDrawable(new ColorDrawable(0));
                p.showAsDropDown(anchor, 0, 0);
                p.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    public void onDismiss() {
                        dismissPreview(anchor);
                    }
                });
                anchor.setTag(R.id.TAG_PREVIEW, p);
                anchor.setTag(R.id.workspace, preview);
                anchor.setTag(R.id.icon, bitmaps);
            }
        }
    }
}

From source file:com.skytree.epubtest.BookViewActivity.java

@SuppressLint({ "DrawAllocation", "DrawAllocation" })
@Override/* w  w  w .  j a  v a  2 s  .  c  o  m*/
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();

    float sl, sr, st, sb;
    sl = 0;
    sr = this.getWidth();
    float ah = this.arrowHeight; // arrow Height;
    if (this.isArrowDown) {
        st = 0;
        sb = this.getHeight() - ah;
    } else {
        st = ah - 10;
        sb = this.getHeight() - 10;
    }

    Path boxPath = new Path();
    boxPath.addRoundRect(new RectF(sl, st, sr, sb), 20, 20, Path.Direction.CW);

    if (arrowPosition <= arrowHeight * 1.5f) {
        arrowPosition = arrowHeight * 1.5f;
    } else if (arrowPosition >= this.getWidth() - arrowHeight * 1.5f) {
        arrowPosition = this.getWidth() - arrowHeight * 1.5f;
    }

    Path arrowPath = new Path();
    if (isArrowDown) {
        arrowPath.moveTo(arrowPosition, sb + ah);
        arrowPath.lineTo((float) (arrowPosition - ah * 0.75), sb - 10);
        arrowPath.lineTo((float) (arrowPosition + ah * 0.75), sb - 10);
        arrowPath.close();
    } else {
        arrowPath.moveTo(arrowPosition, 0);
        arrowPath.lineTo((float) (arrowPosition - ah * 0.75), ah + 10);
        arrowPath.lineTo((float) (arrowPosition + ah * 0.75), ah + 10);
        arrowPath.close();
    }

    paint.setColor(this.strokeColor);
    paint.setStyle(Paint.Style.FILL);
    boxPath.addPath(arrowPath);
    canvas.drawPath(boxPath, paint);

    paint.setColor(this.boxColor);
    paint.setStyle(Paint.Style.FILL);
    boxPath.addPath(arrowPath);
    canvas.save();
    float sf = 0.995f;
    float ox = (this.getWidth() - (this.getWidth() * sf)) / 2.0f;
    float oy = ((this.getHeight() - arrowHeight) - ((this.getHeight() - arrowHeight) * sf)) / 2.0f;

    canvas.translate(ox, oy);
    canvas.scale(sf, sf);
    canvas.drawPath(boxPath, paint);
    canvas.restore();

    if (layoutChanged) {
        this.recalcLayout();
        layoutChanged = false;
    }
}

From source file:kr.wdream.ui.PhotoViewer.java

@SuppressLint("NewApi")
private void onDraw(Canvas canvas) {
    if (animationInProgress == 1 || !isVisible && animationInProgress != 2) {
        return;//from  w w  w  .  j av a 2s . c  o  m
    }

    float currentTranslationY;
    float currentTranslationX;
    float currentScale;
    float aty = -1;

    if (imageMoveAnimation != null) {
        if (!scroller.isFinished()) {
            scroller.abortAnimation();
        }

        float ts = scale + (animateToScale - scale) * animationValue;
        float tx = translationX + (animateToX - translationX) * animationValue;
        float ty = translationY + (animateToY - translationY) * animationValue;
        if (currentEditMode == 1) {
            photoCropView.setAnimationProgress(animationValue);
        }

        if (animateToScale == 1 && scale == 1 && translationX == 0) {
            aty = ty;
        }
        currentScale = ts;
        currentTranslationY = ty;
        currentTranslationX = tx;
        containerView.invalidate();
    } else {
        if (animationStartTime != 0) {
            translationX = animateToX;
            translationY = animateToY;
            scale = animateToScale;
            animationStartTime = 0;
            if (currentEditMode == 1) {
                photoCropView.setAnimationProgress(1);
            }
            updateMinMax(scale);
            zoomAnimation = false;
        }
        if (!scroller.isFinished()) {
            if (scroller.computeScrollOffset()) {
                if (scroller.getStartX() < maxX && scroller.getStartX() > minX) {
                    translationX = scroller.getCurrX();
                }
                if (scroller.getStartY() < maxY && scroller.getStartY() > minY) {
                    translationY = scroller.getCurrY();
                }
                containerView.invalidate();
            }
        }
        if (switchImageAfterAnimation != 0) {
            if (switchImageAfterAnimation == 1) {
                setImageIndex(currentIndex + 1, false);
            } else if (switchImageAfterAnimation == 2) {
                setImageIndex(currentIndex - 1, false);
            }
            switchImageAfterAnimation = 0;
        }
        currentScale = scale;
        currentTranslationY = translationY;
        currentTranslationX = translationX;
        if (!moving) {
            aty = translationY;
        }
    }

    if (currentEditMode == 0 && scale == 1 && aty != -1 && !zoomAnimation) {
        float maxValue = getContainerViewHeight() / 4.0f;
        backgroundDrawable
                .setAlpha((int) Math.max(127, 255 * (1.0f - (Math.min(Math.abs(aty), maxValue) / maxValue))));
    } else {
        backgroundDrawable.setAlpha(255);
    }

    ImageReceiver sideImage = null;
    if (currentEditMode == 0) {
        if (scale >= 1.0f && !zoomAnimation && !zooming) {
            if (currentTranslationX > maxX + AndroidUtilities.dp(5)) {
                sideImage = leftImage;
            } else if (currentTranslationX < minX - AndroidUtilities.dp(5)) {
                sideImage = rightImage;
            }
        }
        changingPage = sideImage != null;
    }

    if (sideImage == rightImage) {
        float tranlateX = currentTranslationX;
        float scaleDiff = 0;
        float alpha = 1;
        if (!zoomAnimation && tranlateX < minX) {
            alpha = Math.min(1.0f, (minX - tranlateX) / canvas.getWidth());
            scaleDiff = (1.0f - alpha) * 0.3f;
            tranlateX = -canvas.getWidth() - AndroidUtilities.dp(30) / 2;
        }

        if (sideImage.hasBitmapImage()) {
            canvas.save();
            canvas.translate(getContainerViewWidth() / 2, getContainerViewHeight() / 2);
            canvas.translate(canvas.getWidth() + AndroidUtilities.dp(30) / 2 + tranlateX, 0);
            canvas.scale(1.0f - scaleDiff, 1.0f - scaleDiff);
            int bitmapWidth = sideImage.getBitmapWidth();
            int bitmapHeight = sideImage.getBitmapHeight();

            float scaleX = (float) getContainerViewWidth() / (float) bitmapWidth;
            float scaleY = (float) getContainerViewHeight() / (float) bitmapHeight;
            float scale = scaleX > scaleY ? scaleY : scaleX;
            int width = (int) (bitmapWidth * scale);
            int height = (int) (bitmapHeight * scale);

            sideImage.setAlpha(alpha);
            sideImage.setImageCoords(-width / 2, -height / 2, width, height);
            sideImage.draw(canvas);
            canvas.restore();
        }

        canvas.save();
        canvas.translate(tranlateX, currentTranslationY / currentScale);
        canvas.translate((canvas.getWidth() * (scale + 1) + AndroidUtilities.dp(30)) / 2,
                -currentTranslationY / currentScale);
        radialProgressViews[1].setScale(1.0f - scaleDiff);
        radialProgressViews[1].setAlpha(alpha);
        radialProgressViews[1].onDraw(canvas);
        canvas.restore();
    }

    float translateX = currentTranslationX;
    float scaleDiff = 0;
    float alpha = 1;
    if (!zoomAnimation && translateX > maxX && currentEditMode == 0) {
        alpha = Math.min(1.0f, (translateX - maxX) / canvas.getWidth());
        scaleDiff = alpha * 0.3f;
        alpha = 1.0f - alpha;
        translateX = maxX;
    }
    boolean drawTextureView = Build.VERSION.SDK_INT >= 16 && aspectRatioFrameLayout != null
            && aspectRatioFrameLayout.getVisibility() == View.VISIBLE;
    if (centerImage.hasBitmapImage()) {
        canvas.save();
        canvas.translate(getContainerViewWidth() / 2 + getAdditionX(),
                getContainerViewHeight() / 2 + getAdditionY());
        canvas.translate(translateX, currentTranslationY);
        canvas.scale(currentScale - scaleDiff, currentScale - scaleDiff);

        if (currentEditMode == 1) {
            photoCropView.setBitmapParams(currentScale, translateX, currentTranslationY);
        }

        int bitmapWidth = centerImage.getBitmapWidth();
        int bitmapHeight = centerImage.getBitmapHeight();
        if (drawTextureView && textureUploaded) {
            float scale1 = bitmapWidth / (float) bitmapHeight;
            float scale2 = videoTextureView.getMeasuredWidth() / (float) videoTextureView.getMeasuredHeight();
            if (Math.abs(scale1 - scale2) > 0.01f) {
                bitmapWidth = videoTextureView.getMeasuredWidth();
                bitmapHeight = videoTextureView.getMeasuredHeight();
            }
        }

        float scaleX = (float) getContainerViewWidth() / (float) bitmapWidth;
        float scaleY = (float) getContainerViewHeight() / (float) bitmapHeight;
        float scale = scaleX > scaleY ? scaleY : scaleX;
        int width = (int) (bitmapWidth * scale);
        int height = (int) (bitmapHeight * scale);

        if (!drawTextureView || !textureUploaded || !videoCrossfadeStarted || videoCrossfadeAlpha != 1.0f) {
            centerImage.setAlpha(alpha);
            centerImage.setImageCoords(-width / 2, -height / 2, width, height);
            centerImage.draw(canvas);
        }
        if (drawTextureView) {
            if (!videoCrossfadeStarted && textureUploaded) {
                videoCrossfadeStarted = true;
                videoCrossfadeAlpha = 0.0f;
                videoCrossfadeAlphaLastTime = System.currentTimeMillis();
            }
            canvas.translate(-width / 2, -height / 2);
            videoTextureView.setAlpha(alpha * videoCrossfadeAlpha);
            aspectRatioFrameLayout.draw(canvas);
            if (videoCrossfadeStarted && videoCrossfadeAlpha < 1.0f) {
                long newUpdateTime = System.currentTimeMillis();
                long dt = newUpdateTime - videoCrossfadeAlphaLastTime;
                videoCrossfadeAlphaLastTime = newUpdateTime;
                videoCrossfadeAlpha += dt / 300.0f;
                containerView.invalidate();
                if (videoCrossfadeAlpha > 1.0f) {
                    videoCrossfadeAlpha = 1.0f;
                }
            }
        }
        canvas.restore();
    }
    if (!drawTextureView && (videoPlayerControlFrameLayout == null
            || videoPlayerControlFrameLayout.getVisibility() != View.VISIBLE)) {
        canvas.save();
        canvas.translate(translateX, currentTranslationY / currentScale);
        radialProgressViews[0].setScale(1.0f - scaleDiff);
        radialProgressViews[0].setAlpha(alpha);
        radialProgressViews[0].onDraw(canvas);
        canvas.restore();
    }

    if (sideImage == leftImage) {
        if (sideImage.hasBitmapImage()) {
            canvas.save();
            canvas.translate(getContainerViewWidth() / 2, getContainerViewHeight() / 2);
            canvas.translate(
                    -(canvas.getWidth() * (scale + 1) + AndroidUtilities.dp(30)) / 2 + currentTranslationX, 0);
            int bitmapWidth = sideImage.getBitmapWidth();
            int bitmapHeight = sideImage.getBitmapHeight();

            float scaleX = (float) getContainerViewWidth() / (float) bitmapWidth;
            float scaleY = (float) getContainerViewHeight() / (float) bitmapHeight;
            float scale = scaleX > scaleY ? scaleY : scaleX;
            int width = (int) (bitmapWidth * scale);
            int height = (int) (bitmapHeight * scale);

            sideImage.setAlpha(1.0f);
            sideImage.setImageCoords(-width / 2, -height / 2, width, height);
            sideImage.draw(canvas);
            canvas.restore();
        }

        canvas.save();
        canvas.translate(currentTranslationX, currentTranslationY / currentScale);
        canvas.translate(-(canvas.getWidth() * (scale + 1) + AndroidUtilities.dp(30)) / 2,
                -currentTranslationY / currentScale);
        radialProgressViews[2].setScale(1.0f);
        radialProgressViews[2].setAlpha(1.0f);
        radialProgressViews[2].onDraw(canvas);
        canvas.restore();
    }
}

From source file:org.telegram.ui.ArticleViewer.java

private void drawContent(Canvas canvas) {
    if (photoAnimationInProgress == 1 || !isPhotoVisible && photoAnimationInProgress != 2) {
        return;//from   w  ww .  ja v  a 2  s .co  m
    }

    float currentTranslationY;
    float currentTranslationX;
    float currentScale;
    float aty = -1;

    if (imageMoveAnimation != null) {
        if (!scroller.isFinished()) {
            scroller.abortAnimation();
        }

        float ts = scale + (animateToScale - scale) * animationValue;
        float tx = translationX + (animateToX - translationX) * animationValue;
        float ty = translationY + (animateToY - translationY) * animationValue;

        if (animateToScale == 1 && scale == 1 && translationX == 0) {
            aty = ty;
        }
        currentScale = ts;
        currentTranslationY = ty;
        currentTranslationX = tx;
        photoContainerView.invalidate();
    } else {
        if (animationStartTime != 0) {
            translationX = animateToX;
            translationY = animateToY;
            scale = animateToScale;
            animationStartTime = 0;
            updateMinMax(scale);
            zoomAnimation = false;
        }
        if (!scroller.isFinished()) {
            if (scroller.computeScrollOffset()) {
                if (scroller.getStartX() < maxX && scroller.getStartX() > minX) {
                    translationX = scroller.getCurrX();
                }
                if (scroller.getStartY() < maxY && scroller.getStartY() > minY) {
                    translationY = scroller.getCurrY();
                }
                photoContainerView.invalidate();
            }
        }
        if (switchImageAfterAnimation != 0) {
            if (switchImageAfterAnimation == 1) {
                setImageIndex(currentIndex + 1, false);
            } else if (switchImageAfterAnimation == 2) {
                setImageIndex(currentIndex - 1, false);
            }
            switchImageAfterAnimation = 0;
        }
        currentScale = scale;
        currentTranslationY = translationY;
        currentTranslationX = translationX;
        if (!moving) {
            aty = translationY;
        }
    }

    if (scale == 1 && aty != -1 && !zoomAnimation) {
        float maxValue = getContainerViewHeight() / 4.0f;
        photoBackgroundDrawable
                .setAlpha((int) Math.max(127, 255 * (1.0f - (Math.min(Math.abs(aty), maxValue) / maxValue))));
    } else {
        photoBackgroundDrawable.setAlpha(255);
    }

    ImageReceiver sideImage = null;

    if (scale >= 1.0f && !zoomAnimation && !zooming) {
        if (currentTranslationX > maxX + AndroidUtilities.dp(5)) {
            sideImage = leftImage;
        } else if (currentTranslationX < minX - AndroidUtilities.dp(5)) {
            sideImage = rightImage;
        }
    }
    changingPage = sideImage != null;

    if (sideImage == rightImage) {
        float tranlateX = currentTranslationX;
        float scaleDiff = 0;
        float alpha = 1;
        if (!zoomAnimation && tranlateX < minX) {
            alpha = Math.min(1.0f, (minX - tranlateX) / canvas.getWidth());
            scaleDiff = (1.0f - alpha) * 0.3f;
            tranlateX = -canvas.getWidth() - AndroidUtilities.dp(30) / 2;
        }

        if (sideImage.hasBitmapImage()) {
            canvas.save();
            canvas.translate(getContainerViewWidth() / 2, getContainerViewHeight() / 2);
            canvas.translate(canvas.getWidth() + AndroidUtilities.dp(30) / 2 + tranlateX, 0);
            canvas.scale(1.0f - scaleDiff, 1.0f - scaleDiff);
            int bitmapWidth = sideImage.getBitmapWidth();
            int bitmapHeight = sideImage.getBitmapHeight();

            float scaleX = (float) getContainerViewWidth() / (float) bitmapWidth;
            float scaleY = (float) getContainerViewHeight() / (float) bitmapHeight;
            float scale = scaleX > scaleY ? scaleY : scaleX;
            int width = (int) (bitmapWidth * scale);
            int height = (int) (bitmapHeight * scale);

            sideImage.setAlpha(alpha);
            sideImage.setImageCoords(-width / 2, -height / 2, width, height);
            sideImage.draw(canvas);
            canvas.restore();
        }

        canvas.save();
        canvas.translate(tranlateX, currentTranslationY / currentScale);
        canvas.translate((canvas.getWidth() * (scale + 1) + AndroidUtilities.dp(30)) / 2,
                -currentTranslationY / currentScale);
        radialProgressViews[1].setScale(1.0f - scaleDiff);
        radialProgressViews[1].setAlpha(alpha);
        radialProgressViews[1].onDraw(canvas);
        canvas.restore();
    }

    float translateX = currentTranslationX;
    float scaleDiff = 0;
    float alpha = 1;
    if (!zoomAnimation && translateX > maxX) {
        alpha = Math.min(1.0f, (translateX - maxX) / canvas.getWidth());
        scaleDiff = alpha * 0.3f;
        alpha = 1.0f - alpha;
        translateX = maxX;
    }
    boolean drawTextureView = aspectRatioFrameLayout != null
            && aspectRatioFrameLayout.getVisibility() == View.VISIBLE;
    if (centerImage.hasBitmapImage()) {
        canvas.save();
        canvas.translate(getContainerViewWidth() / 2, getContainerViewHeight() / 2);
        canvas.translate(translateX, currentTranslationY);
        canvas.scale(currentScale - scaleDiff, currentScale - scaleDiff);

        int bitmapWidth = centerImage.getBitmapWidth();
        int bitmapHeight = centerImage.getBitmapHeight();
        if (drawTextureView && textureUploaded) {
            float scale1 = bitmapWidth / (float) bitmapHeight;
            float scale2 = videoTextureView.getMeasuredWidth() / (float) videoTextureView.getMeasuredHeight();
            if (Math.abs(scale1 - scale2) > 0.01f) {
                bitmapWidth = videoTextureView.getMeasuredWidth();
                bitmapHeight = videoTextureView.getMeasuredHeight();
            }
        }

        float scaleX = (float) getContainerViewWidth() / (float) bitmapWidth;
        float scaleY = (float) getContainerViewHeight() / (float) bitmapHeight;
        float scale = scaleX > scaleY ? scaleY : scaleX;
        int width = (int) (bitmapWidth * scale);
        int height = (int) (bitmapHeight * scale);

        if (!drawTextureView || !textureUploaded || !videoCrossfadeStarted || videoCrossfadeAlpha != 1.0f) {
            centerImage.setAlpha(alpha);
            centerImage.setImageCoords(-width / 2, -height / 2, width, height);
            centerImage.draw(canvas);
        }
        if (drawTextureView) {
            if (!videoCrossfadeStarted && textureUploaded) {
                videoCrossfadeStarted = true;
                videoCrossfadeAlpha = 0.0f;
                videoCrossfadeAlphaLastTime = System.currentTimeMillis();
            }
            canvas.translate(-width / 2, -height / 2);
            videoTextureView.setAlpha(alpha * videoCrossfadeAlpha);
            aspectRatioFrameLayout.draw(canvas);
            if (videoCrossfadeStarted && videoCrossfadeAlpha < 1.0f) {
                long newUpdateTime = System.currentTimeMillis();
                long dt = newUpdateTime - videoCrossfadeAlphaLastTime;
                videoCrossfadeAlphaLastTime = newUpdateTime;
                videoCrossfadeAlpha += dt / 300.0f;
                photoContainerView.invalidate();
                if (videoCrossfadeAlpha > 1.0f) {
                    videoCrossfadeAlpha = 1.0f;
                }
            }
        }
        canvas.restore();
    }
    if (!drawTextureView && bottomLayout.getVisibility() != View.VISIBLE) {
        canvas.save();
        canvas.translate(translateX, currentTranslationY / currentScale);
        radialProgressViews[0].setScale(1.0f - scaleDiff);
        radialProgressViews[0].setAlpha(alpha);
        radialProgressViews[0].onDraw(canvas);
        canvas.restore();
    }

    if (sideImage == leftImage) {
        if (sideImage.hasBitmapImage()) {
            canvas.save();
            canvas.translate(getContainerViewWidth() / 2, getContainerViewHeight() / 2);
            canvas.translate(
                    -(canvas.getWidth() * (scale + 1) + AndroidUtilities.dp(30)) / 2 + currentTranslationX, 0);
            int bitmapWidth = sideImage.getBitmapWidth();
            int bitmapHeight = sideImage.getBitmapHeight();

            float scaleX = (float) getContainerViewWidth() / (float) bitmapWidth;
            float scaleY = (float) getContainerViewHeight() / (float) bitmapHeight;
            float scale = scaleX > scaleY ? scaleY : scaleX;
            int width = (int) (bitmapWidth * scale);
            int height = (int) (bitmapHeight * scale);

            sideImage.setAlpha(1.0f);
            sideImage.setImageCoords(-width / 2, -height / 2, width, height);
            sideImage.draw(canvas);
            canvas.restore();
        }

        canvas.save();
        canvas.translate(currentTranslationX, currentTranslationY / currentScale);
        canvas.translate(-(canvas.getWidth() * (scale + 1) + AndroidUtilities.dp(30)) / 2,
                -currentTranslationY / currentScale);
        radialProgressViews[2].setScale(1.0f);
        radialProgressViews[2].setAlpha(1.0f);
        radialProgressViews[2].onDraw(canvas);
        canvas.restore();
    }
}