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 final void scale(float sx, float sy, float px, float py) 

Source Link

Document

Preconcat the current matrix with the specified scale.

Usage

From source file:android.support.designox.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {//from  w w  w .j a  va 2s . c o  m
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.amitupadhyay.aboutexample.util.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();
    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;
        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;
        final float ascent;
        final float descent;
        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from w  w  w.j av  a2 s . com*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }
        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }
        if (drawTexture) {
            y += ascent;
        }
        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }
        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }
    canvas.restoreToCount(saveCount);
}

From source file:android.support.design.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from  w ww.j a  v a 2  s .  c  o m*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a magenta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.tr4android.support.extension.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;

        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);

        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {/*from   w w w . j  a  v  a  2 s.  c  om*/
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:net.opacapp.multilinecollapsingtoolbar.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;

        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;
        final float ascent;
        // MODIFICATION: removed now unused "descent" variable declaration

        // Update the TextPaint to the current text size
        mTextPaint.setTextSize(mCurrentTextSize);

        // BEGIN MODIFICATION: new drawing code
        if (drawTexture) {
            ascent = 0;/*from   www.j  a  v a  2 s .c om*/
        } else {
            ascent = mTextPaint.ascent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y, mCurrentBounds.right, y + mTextLayout.getHeight() * mScale,
                    DEBUG_DRAW_PAINT);
        }
        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        // Compute where to draw mTextLayout for this frame
        final float currentExpandedX = mCurrentDrawX + mTextLayout.getLineLeft(0) - mExpandedFirstLineDrawX * 2;
        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            // Expanded text
            mTexturePaint.setAlpha((int) (mExpandedTextBlend * 255));
            canvas.drawBitmap(mExpandedTitleTexture, currentExpandedX, y, mTexturePaint);
            // Collapsed text
            mTexturePaint.setAlpha((int) (mCollapsedTextBlend * 255));
            canvas.drawBitmap(mCollapsedTitleTexture, x, y, mTexturePaint);
            // Cross-section between both texts (should stay at alpha = 255)
            mTexturePaint.setAlpha(255);
            canvas.drawBitmap(mCrossSectionTitleTexture, x, y, mTexturePaint);
        } else {
            // positon expanded text appropriately
            canvas.translate(currentExpandedX, y);
            // Expanded text
            mTextPaint.setAlpha((int) (mExpandedTextBlend * 255));
            mTextLayout.draw(canvas);

            // position the overlays
            canvas.translate(x - currentExpandedX, 0);

            // Collapsed text
            mTextPaint.setAlpha((int) (mCollapsedTextBlend * 255));
            canvas.drawText(mTextToDrawCollapsed, 0, mTextToDrawCollapsed.length(), 0, -ascent / mScale,
                    mTextPaint);
            // Cross-section between both texts (should stay at alpha = 255)
            mTextPaint.setAlpha(255);
            canvas.drawText(mTextToDraw, mTextLayout.getLineStart(0), mTextLayout.getLineEnd(0), 0,
                    -ascent / mScale, mTextPaint);
        }
        // END MODIFICATION
    }
    canvas.restoreToCount(saveCount);
}

From source file:io.apptik.multiview.scalablerecyclerview.ScalableRecyclerGridView.java

@Override
public void onDraw(Canvas c) {
    if (interactionListener.currentView != null && interactionListener.scaleGestureDetector.isInProgress()) {
        float px;
        float py;

        //pivot for zooming depends on the ratio of the distance of left and
        // right(top and bottom) of the child to left and right(top and bottom) of the RV
        float dl = interactionListener.currentView.getX() - getX();
        float dw = getWidth() - interactionListener.currentView.getWidth();
        px = interactionListener.currentView.getX() + (dl / dw) * interactionListener.currentView.getWidth();
        float dt = interactionListener.currentView.getY() - getY();
        float dh = getHeight() - interactionListener.currentView.getHeight();
        py = interactionListener.currentView.getY() + (dt / dh) * interactionListener.currentView.getHeight();
        //however we also do not want to have half appearing views so we choose a side
        float midX = getX() + getWidth() / 2;
        float midY = getY() + getHeight() / 2;
        if (getLayoutManager().canScrollHorizontally()) {
            //fix py
            if (py < midY) {
                py = 0;//w  w w .ja v a 2  s  . com
            } else {
                py = getY() + getHeight();
            }
        } else {
            //fix px
            if (px < midX) {
                px = 0;
            } else {
                px = getX() + getWidth();
            }
        }

        c.scale(getCurrScale(), getCurrScale(), px, py);
    }
    super.onDraw(c);
}

From source file:com.hippo.widget.Slider.java

@Override
protected void onDraw(@NonNull Canvas canvas) {
    int width = getWidth();
    int height = getHeight();
    if (width < LayoutUtils.dp2pix(mContext, 24)) {
        canvas.drawRect(0, 0, width, getHeight(), mPaint);
    } else {// w  w w.ja  v  a2s  . c  o m
        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();
        float thickness = mThickness;
        float radius = mRadius;
        float halfThickness = thickness / 2;

        int saved = canvas.save();

        canvas.translate(0, paddingTop + ((height - paddingTop - paddingBottom) / 2));

        float currentX = paddingLeft + radius + (width - radius - radius - paddingLeft - paddingRight)
                * (mReverse ? (1.0f - mDrawPercent) : mDrawPercent);

        mLeftRectF.set(paddingLeft + radius, -halfThickness, currentX, halfThickness);
        mRightRectF.set(currentX, -halfThickness, width - paddingRight - radius, halfThickness);

        // Draw bar
        if (mReverse) {
            canvas.drawRect(mRightRectF, mPaint);
            canvas.drawRect(mLeftRectF, mBgPaint);
        } else {
            canvas.drawRect(mLeftRectF, mPaint);
            canvas.drawRect(mRightRectF, mBgPaint);
        }

        // Draw controller
        float scale = 1.0f - mDrawBubbleScale;
        if (scale != 0.0f) {
            canvas.scale(scale, scale, currentX, 0);
            canvas.drawCircle(currentX, 0, radius, mPaint);
        }

        canvas.restoreToCount(saved);
    }
}

From source file:android.support.design.widget.CustomCollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;
        float subY = mCurrentSubY;
        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {//from   ww w . j av a  2 s  .  c o  m
            ascent = mTitlePaint.ascent() * mScale;
            descent = mTitlePaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a magenta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        //region modification
        final int saveCountSub = canvas.save();
        if (mSub != null) {
            if (mSubScale != 1f) {
                canvas.scale(mSubScale, mSubScale, x, subY);
            }
            canvas.drawText(mSub, 0, mSub.length(), x, subY, mSubPaint);
            canvas.restoreToCount(saveCountSub);
        }
        //endregion

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTitlePaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.muzakki.ahmad.widget.CollapsingTextHelper.java

public void draw(Canvas canvas) {
    final int saveCount = canvas.save();

    if (mTextToDraw != null && mDrawTitle) {
        float x = mCurrentDrawX;
        float y = mCurrentDrawY;
        float subX = mCurrentSubX;
        float subY = mCurrentSubY;
        final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;

        final float ascent;
        final float descent;
        if (drawTexture) {
            ascent = mTextureAscent * mScale;
            descent = mTextureDescent * mScale;
        } else {//from   w  w  w. j  a va 2s.  c o m
            ascent = mTextPaint.ascent() * mScale;
            descent = mTextPaint.descent() * mScale;
        }

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a magenta rect in the text bounds
            canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
                    DEBUG_DRAW_PAINT);
        }

        if (drawTexture) {
            y += ascent;
        }

        // begin modification
        // draw subtitle
        final int saveCountSub = canvas.save();
        if (mSub != null) {
            if (mSubScale != 1f) {
                canvas.scale(mSubScale, mSubScale, subX, subY);
            }
            canvas.drawText(mSub, 0, mSub.length(), subX, subY, mSubPaint);
            canvas.restoreToCount(saveCountSub);
        }
        // end modification

        if (mScale != 1f) {
            canvas.scale(mScale, mScale, x, y);
        }

        if (drawTexture) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
        } else {
            canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:com.android.launcher3.Utilities.java

/**
 * @param scale the scale to apply before drawing {@param icon} on the canvas
 *///from w  w  w.  ja  va 2s. c  o  m
public static Bitmap createIconBitmap(Drawable icon, Context context, float scale) {
    synchronized (sCanvas) {
        final int iconBitmapSize = getIconBitmapSize();

        int width = iconBitmapSize;
        int height = iconBitmapSize;

        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap != null && bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // Scale the icon proportionally to the icon dimensions
            final float ratio = (float) sourceWidth / sourceHeight;
            if (sourceWidth > sourceHeight) {
                height = (int) (width / ratio);
            } else if (sourceHeight > sourceWidth) {
                width = (int) (height * ratio);
            }
        }

        // no intrinsic size --> use default size
        int textureWidth = iconBitmapSize;
        int textureHeight = iconBitmapSize;

        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);

        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;

        @SuppressWarnings("all") // suppress dead code warning
        final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }

        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(scale, scale, textureWidth / 2, textureHeight / 2);
        icon.draw(canvas);
        canvas.restore();
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);

        return bitmap;
    }
}