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:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * //  w  w w.  java2 s  .  co m
 *
 * @param canvas     
 * @param position   ???
 * @param itemWidth  ?
 * @param itemHeight ?
 */
protected void drawText(Canvas canvas, int position, int itemWidth, int itemHeight) {
    if (getItemText(position) == null)
        return;
    String text = getItemText(position).toString();
    if (text.length() <= 0)
        return;
    mTextPaint.setTextSize(mTextSize);
    if (mTextColor == null) {
        mTextPaint.setColor(DEFAULT_TEXT_COLOR);
    } else {
        final int normalColor = mTextColor.getDefaultColor();
        final int selectedColor = mTextColor.getColorForState(SELECTED_STATE_SET, normalColor);
        if (position == mNextPager) {
            mTextPaint.setColor(getColor(normalColor, selectedColor, mOffset));
        } else if (position == mCurrentPager) {
            mTextPaint.setColor(getColor(normalColor, selectedColor, 1 - mOffset));
        } else {
            mTextPaint.setColor(normalColor);
        }
    }
    final float centerX = ViewCompat.getPaddingStart(this)
            + (itemWidth + getIntervalWidth()) * ((float) position + 0.5f);
    final float centerY = getPaddingTop() + itemHeight * 0.5f;
    float scale;
    if (position == mNextPager) {
        scale = 1 + (mTextScale - 1) * mOffset;
    } else if (position == mCurrentPager) {
        scale = 1 + (mTextScale - 1) * (1 - mOffset);
    } else {
        scale = 1;
    }
    canvas.save();
    canvas.translate(centerX, centerY + mTextDesc);
    if (scale != 1) {
        canvas.scale(scale, scale, 0, -mTextDesc);
    }
    canvas.drawText(text, 0, 0, mTextPaint);
    canvas.restore();
}

From source file:com.ruesga.timelinechart.TimelineChartView.java

private void drawBarItems(Canvas c, LongSparseArray<Pair<double[], int[]>> data, double maxValue) {

    final float halfItemBarWidth = mBarItemWidth / 2;
    final float height = mGraphArea.height();
    final Paint[] seriesBgPaint;
    final Paint[] highlightSeriesBgPaint;
    synchronized (mLock) {
        seriesBgPaint = mSeriesBgPaint;/* ww w .  jav a  2s. co m*/
        highlightSeriesBgPaint = mHighlightSeriesBgPaint;
    }

    // Apply zoom animation
    final float zoom = mCurrentZoom;
    final float cx = mGraphArea.left + (mGraphArea.width() / 2);
    int restoreCount = 0;
    if (zoom != 1.f) {
        restoreCount = c.save();
        c.scale(zoom, zoom, cx, mGraphArea.bottom);
    }

    final int size = data.size() - 1;
    for (int i = mItemsOnScreen[1]; i >= mItemsOnScreen[0]; i--) {
        final float x = cx + mCurrentOffset - (mBarWidth * (size - i));
        float bw = mBarItemWidth / mSeries;
        double[] values = data.valueAt(i).first;
        int[] indexes = data.valueAt(i).second;

        float y1, y2 = height;
        float x1 = x - halfItemBarWidth, x2 = x + halfItemBarWidth;
        if (mGraphMode != GRAPH_MODE_BARS_STACK) {
            int count = values.length - 1;
            for (int j = count, n = 0; j >= 0; j--, n++) {
                y2 = height;
                final Paint paint;
                if (mGraphMode == GRAPH_MODE_BARS_SIDE_BY_SIDE) {
                    y1 = (float) (height - ((height * ((values[n] * 100) / maxValue)) / 100));
                    x1 = x - halfItemBarWidth + (bw * n);
                    x2 = x1 + bw;
                    paint = (x - halfItemBarWidth) < cx && (x + halfItemBarWidth) > cx
                            && (mLastTimestamp == mCurrentTimestamp || (mState != STATE_SCROLLING))
                                    ? highlightSeriesBgPaint[indexes[n]]
                                    : seriesBgPaint[indexes[n]];
                } else {
                    y1 = (float) (height - ((height * ((values[j] * 100) / maxValue)) / 100));
                    paint = x1 < cx && x2 > cx
                            && (mLastTimestamp == mCurrentTimestamp || (mState != STATE_SCROLLING))
                                    ? highlightSeriesBgPaint[indexes[j]]
                                    : seriesBgPaint[indexes[j]];
                }

                c.drawRect(x1, mGraphArea.top + y1, x2, mGraphArea.top + y2, paint);
            }
        } else {
            int count = values.length;
            for (int j = 0; j < count; j++) {
                float h = (float) ((height * ((values[j] * 100) / maxValue)) / 100);
                y1 = y2 - h;

                final Paint paint = x1 < cx && x2 > cx
                        && (mLastTimestamp == mCurrentTimestamp || (mState != STATE_SCROLLING))
                                ? highlightSeriesBgPaint[indexes[j]]
                                : seriesBgPaint[indexes[j]];
                c.drawRect(x1, mGraphArea.top + y1, x2, mGraphArea.top + y2, paint);
                y2 -= h;
            }
        }
    }

    // Restore from zoom
    if (zoom != 1.f) {
        c.restoreToCount(restoreCount);
    }
}

From source file:org.zywx.wbpalmstar.engine.universalex.EUExWindow.java

public void handleSetSlidingWin(String[] param) {
    String jsonStr = param[0];//from   w  ww .  j  a  v a 2 s .co m
    EBrowserActivity activity = (EBrowserActivity) mContext;
    try {
        JSONObject jsonObject = new JSONObject(jsonStr);
        int with = 0;
        String url;
        int slidingMode = SlidingMenu.LEFT;
        boolean isAttach = false;
        JSONObject leftJsonObj = null;
        JSONObject rightJsonObj = null;
        View menuView;
        if (activity.globalSlidingMenu.getParent() != null) {
            return;
        }

        String animationId = jsonObject.optString("animationId");
        if (jsonObject.has("leftSliding")) {
            leftJsonObj = new JSONObject(jsonObject.getString("leftSliding"));
            if (leftJsonObj != null) {
                slidingMode = SlidingMenu.LEFT;
                with = leftJsonObj.getInt("width");
                url = leftJsonObj.getString("url");
                if (with > 0) {
                    activity.globalSlidingMenu.setBehindWidth(with);
                }
                menuView = LayoutInflater.from(mContext).inflate(finder.getLayoutId("menu_frame"), null);
                activity.globalSlidingMenu.setMenu(menuView);
                addBrowserWindowToSldingWin(url, EBrowserWindow.rootLeftSlidingWinName);
                isAttach = true;
            }
        }

        if (jsonObject.has("rightSliding")) {
            rightJsonObj = new JSONObject(jsonObject.getString("rightSliding"));
            if (rightJsonObj != null) {
                slidingMode = SlidingMenu.RIGHT;
                with = rightJsonObj.getInt("width");
                url = rightJsonObj.getString("url");
                if (with > 0) {
                    activity.globalSlidingMenu.setBehindWidth(with);
                }
                menuView = LayoutInflater.from(mContext).inflate(finder.getLayoutId("menu_frame_two"), null);
                activity.globalSlidingMenu.setSecondaryMenu(menuView);
                activity.globalSlidingMenu.setSecondaryShadowDrawable(finder.getDrawable("shadowright"));
                addBrowserWindowToSldingWin(url, EBrowserWindow.rootRightSlidingWinName);
                isAttach = true;
            }
        }

        if ("1".equals(animationId)) {
            //QQ?
            activity.globalSlidingMenu.setBehindCanvasTransformer(new SlidingMenu.CanvasTransformer() {
                @Override
                public void transformCanvas(Canvas canvas, float percentOpen) {
                    float scale = (float) (percentOpen * 0.25 + 0.75);
                    canvas.scale(scale, scale, 0, canvas.getHeight() / 2);
                }
            });
            activity.globalSlidingMenu.setAboveCanvasTransformer(new SlidingMenu.CanvasTransformer() {
                @Override
                public void transformCanvas(Canvas canvas, float percentOpen) {
                    float scale = (float) (1 - percentOpen * 0.20);
                    canvas.scale(scale, scale, canvas.getWidth() / 2, canvas.getHeight() / 2);
                }
            });
            activity.globalSlidingMenu.setFadeEnabled(false);
        } else {
            activity.globalSlidingMenu.setShadowWidthRes(EUExUtil.getResDimenID("shadow_width"));
            if (!jsonObject.has("leftSliding") && jsonObject.has("rightSliding")) {
                activity.globalSlidingMenu.setShadowDrawable(EUExUtil.getResDrawableID("shadowright"));
            } else {
                activity.globalSlidingMenu.setShadowDrawable(EUExUtil.getResDrawableID("shadow"));
            }
            activity.globalSlidingMenu.setFadeDegree(0.35f);
        }

        String bg = jsonObject.optString("bg");
        if (!TextUtils.isEmpty(bg)) {
            setViewBackground(activity.globalSlidingMenu, bg, mBrwView.getCurrentWidget().m_indexUrl);
        }

        if (leftJsonObj != null && rightJsonObj != null) {
            slidingMode = SlidingMenu.LEFT_RIGHT;
        }

        if (isAttach == true) {
            activity.globalSlidingMenu.setMode(slidingMode);
            activity.globalSlidingMenu.attachToActivity(activity, SlidingMenu.SLIDING_CONTENT);
            mBrwView.setBackgroundColor(Color.TRANSPARENT);
        }
    } catch (JSONException e) {
    }
}

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

@Override
protected void onDraw(Canvas canvas) {
    if (this.isReversed) {
        float px = this.getWidth() / 2.0f;
        float py = this.getHeight() / 2.0f;
        canvas.scale(-1, 1, px, py);
    }/*from w  ww . j a v  a 2 s  .c o  m*/
    super.onDraw(canvas);
}

From source file:org.telegram.ui.Cells.ContextLinkCell.java

@Override
protected void onDraw(Canvas canvas) {
    if (titleLayout != null) {
        canvas.save();//  w w w.  j  a  v a 2 s .c  om
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                titleY);
        titleLayout.draw(canvas);
        canvas.restore();
    }

    if (descriptionLayout != null) {
        descriptionTextPaint.setColor(DESCRIPTION_TEXT_COLOR/*0xff8a8a8a*/);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                descriptionY);
        descriptionLayout.draw(canvas);
        canvas.restore();
    }

    if (linkLayout != null) {
        descriptionTextPaint.setColor(Theme.MSG_LINK_TEXT_COLOR);
        canvas.save();
        canvas.translate(AndroidUtilities.dp(LocaleController.isRTL ? 8 : AndroidUtilities.leftBaseline),
                linkY);
        linkLayout.draw(canvas);
        canvas.restore();
    }

    if (!mediaWebpage) {
        if (documentAttachType == DOCUMENT_ATTACH_TYPE_AUDIO
                || documentAttachType == DOCUMENT_ATTACH_TYPE_MUSIC) {
            radialProgress.setProgressColor(buttonPressed ? Theme.MSG_IN_AUDIO_SELECTED_PROGRESS_COLOR
                    : Theme.MSG_IN_AUDIO_PROGRESS_COLOR);
            radialProgress.draw(canvas);
        } else if (inlineResult != null && inlineResult.type.equals("file")) {
            int w = Theme.inlineDocDrawable.getIntrinsicWidth();
            int h = Theme.inlineDocDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (AndroidUtilities.dp(52) - w) / 2;
            int y = linkImageView.getImageY() + (AndroidUtilities.dp(52) - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + AndroidUtilities.dp(52),
                    linkImageView.getImageY() + AndroidUtilities.dp(52), LetterDrawable.paint);
            Theme.inlineDocDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineDocDrawable.draw(canvas);
        } else if (inlineResult != null
                && (inlineResult.type.equals("audio") || inlineResult.type.equals("voice"))) {
            int w = Theme.inlineAudioDrawable.getIntrinsicWidth();
            int h = Theme.inlineAudioDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (AndroidUtilities.dp(52) - w) / 2;
            int y = linkImageView.getImageY() + (AndroidUtilities.dp(52) - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + AndroidUtilities.dp(52),
                    linkImageView.getImageY() + AndroidUtilities.dp(52), LetterDrawable.paint);
            Theme.inlineAudioDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineAudioDrawable.draw(canvas);
        } else if (inlineResult != null
                && (inlineResult.type.equals("venue") || inlineResult.type.equals("geo"))) {
            int w = Theme.inlineLocationDrawable.getIntrinsicWidth();
            int h = Theme.inlineLocationDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (AndroidUtilities.dp(52) - w) / 2;
            int y = linkImageView.getImageY() + (AndroidUtilities.dp(52) - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + AndroidUtilities.dp(52),
                    linkImageView.getImageY() + AndroidUtilities.dp(52), LetterDrawable.paint);
            Theme.inlineLocationDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineLocationDrawable.draw(canvas);
        } else {
            letterDrawable.draw(canvas);
        }
    } else {
        if (inlineResult != null && (inlineResult.send_message instanceof TLRPC.TL_botInlineMessageMediaGeo
                || inlineResult.send_message instanceof TLRPC.TL_botInlineMessageMediaVenue)) {
            int w = Theme.inlineLocationDrawable.getIntrinsicWidth();
            int h = Theme.inlineLocationDrawable.getIntrinsicHeight();
            int x = linkImageView.getImageX() + (linkImageView.getImageWidth() - w) / 2;
            int y = linkImageView.getImageY() + (linkImageView.getImageHeight() - h) / 2;
            canvas.drawRect(linkImageView.getImageX(), linkImageView.getImageY(),
                    linkImageView.getImageX() + linkImageView.getImageWidth(),
                    linkImageView.getImageY() + linkImageView.getImageHeight(), LetterDrawable.paint);
            Theme.inlineLocationDrawable.setBounds(x, y, x + w, y + h);
            Theme.inlineLocationDrawable.draw(canvas);
        }
    }
    if (drawLinkImageView) {
        canvas.save();
        if (scaled && scale != 0.8f || !scaled && scale != 1.0f) {
            long newTime = System.currentTimeMillis();
            long dt = (newTime - lastUpdateTime);
            lastUpdateTime = newTime;
            if (scaled && scale != 0.8f) {
                scale -= dt / 400.0f;
                if (scale < 0.8f) {
                    scale = 0.8f;
                }
            } else {
                scale += dt / 400.0f;
                if (scale > 1.0f) {
                    scale = 1.0f;
                }
            }
            invalidate();
        }
        canvas.scale(scale, scale, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
        linkImageView.draw(canvas);
        canvas.restore();
    }
    if (mediaWebpage && (documentAttachType == DOCUMENT_ATTACH_TYPE_PHOTO
            || documentAttachType == DOCUMENT_ATTACH_TYPE_GIF)) {
        radialProgress.setProgressColor(0xffffffff);
        radialProgress.draw(canvas);
    }

    if (needDivider && !mediaWebpage) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, paint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, paint);
        }
    }
    if (needShadow && shadowDrawable != null) {
        shadowDrawable.setBounds(0, 0, getMeasuredWidth(), AndroidUtilities.dp(3));
        shadowDrawable.draw(canvas);
    }
}