Example usage for android.graphics Canvas translate

List of usage examples for android.graphics Canvas translate

Introduction

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

Prototype

public void translate(float dx, float dy) 

Source Link

Document

Preconcat the current matrix with the specified translation

Usage

From source file:pl.motyczko.scrollheader.PagerSlidingTabStrip.java

private void drawTabs(Canvas canvas) {
    canvas.save();/*w  w w.ja v a2 s  .com*/
    canvas.translate(0, getHeight() - tabsContainer.getHeight());
    if (isInEditMode() || tabCount == 0) {
        return;
    }

    final int height = tabsContainer.getHeight();

    // draw indicator line

    rectPaint.setColor(indicatorColor);

    // default: line below current tab
    View currentTab = tabsContainer.getChildAt(currentPosition);
    float lineLeft = currentTab.getLeft();
    float lineRight = currentTab.getRight();

    // if there is an offset, start interpolating left and right coordinates between current and next tab
    if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {

        View nextTab = tabsContainer.getChildAt(currentPosition + 1);
        final float nextTabLeft = nextTab.getLeft();
        final float nextTabRight = nextTab.getRight();

        lineLeft = (currentPositionOffset * nextTabLeft + (1f - currentPositionOffset) * lineLeft);
        lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset) * lineRight);
    }

    canvas.drawRect(lineLeft, height - indicatorHeight, lineRight, height, rectPaint);

    // draw underline

    rectPaint.setColor(underlineColor);
    canvas.drawRect(0, height - underlineHeight, tabsContainer.getWidth(), height, rectPaint);

    // draw divider

    dividerPaint.setColor(dividerColor);
    for (int i = 0; i < tabCount - 1; i++) {
        View tab = tabsContainer.getChildAt(i);
        canvas.drawLine(tab.getRight(), dividerPadding, tab.getRight(), height - dividerPadding, dividerPaint);
    }

    canvas.restore();
}

From source file:com.cssweb.android.view.FinanceMini.java

public void drawPrice(Canvas canvas) {
    //canvas.restore();
    Paint paint = this.mPaint;
    paint.setTypeface(Typeface.DEFAULT_BOLD);
    paint.setAntiAlias(true);/*  w  ww .j a  v a 2s  . co  m*/
    if (quoteData != null) {
        try {
            JSONArray jArr = quoteData.getJSONArray("data");
            JSONObject jo = jArr.getJSONObject(0);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setTextSize(mTextSize);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            int syjd = jo.getInt("syjd");
            switch (syjd) {
            case 1:
                canvas.drawText("()", x, y, paint);
                break;
            case 2:
                canvas.drawText("()", x, y, paint);
                break;
            case 3:
                canvas.drawText("()", x, y, paint);
                break;
            case 4:
                canvas.drawText("()", x, y, paint);
                break;
            default:
                canvas.drawText("", x, y, paint);
                break;
            }

            if (jo.getDouble("wb") < 0)
                paint.setColor(GlobalColor.colorPriceDown);
            else if (jo.getDouble("wb") > 0)
                paint.setColor(GlobalColor.colorpriceUp);
            else
                paint.setColor(GlobalColor.colorPriceEqual);
            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width / 2, -DY * 5);
            if (jo.getInt("tp") == 1)
                canvas.drawText("", x - tips, y, paint);
            else
                canvas.drawText(Utils.dataFormation(jo.getDouble("wb") * 100, 1) + "%", x - tips, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getInt("cjsl"), false), x - tips, y, paint);
            paint.setColor(GlobalColor.colorpriceUp);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("wp"), false), x - tips, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("hs") * 100, 1) + "%", x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("jz"), 1), x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("mgsy"), 2), x - tips, y, paint);

            paint.setTextAlign(Paint.Align.LEFT);
            paint.setColor(GlobalColor.colorLabelName);
            canvas.translate(0, -DY * 5);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("", x, y, paint);
            canvas.translate(0, DY);
            canvas.drawText("?", x, y, paint);
            canvas.translate(0, DY);
            if (NameRule.isBond(type))
                canvas.drawText("", x, y, paint);
            else
                canvas.drawText("PE()", x, y, paint);

            paint.setTextAlign(Paint.Align.RIGHT);
            canvas.translate(width / 2, -DY * 5);
            if (jo.getDouble("wc") < 0) {
                paint.setColor(GlobalColor.colorPriceDown);
                canvas.drawText("-" + Utils.getAmountFormat(Math.abs(jo.getDouble("wc")), true), x - tips, y,
                        paint);
            } else if (jo.getDouble("wc") > 0) {
                paint.setColor(GlobalColor.colorpriceUp);
                canvas.drawText(Utils.getAmountFormat(jo.getDouble("wc"), true), x - tips, y, paint);
            } else {
                paint.setColor(GlobalColor.colorPriceEqual);
                canvas.drawText(Utils.getAmountFormat(jo.getDouble("wc"), false), x - tips, y, paint);
            }
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.dataFormation(jo.getDouble("lb"), 1, jo.getInt("tp")), x - tips, y, paint);
            paint.setColor(GlobalColor.colorPriceDown);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("np"), false), x - tips, y, paint);
            paint.setColor(GlobalColor.colorStockName);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("gb"), true), x - tips, y, paint);
            canvas.translate(0, DY);
            canvas.drawText(Utils.getAmountFormat(jo.getDouble("ltsl") * 100, true), x - tips, y, paint);
            canvas.translate(0, DY);
            if (NameRule.isBond(type)) {
                canvas.drawText(Utils.dataFormation(jo.getDouble("fullprice"), 1), x - tips, y, paint);
            } else {
                canvas.drawText(Utils.dataFormation(jo.getDouble("sy"), 1), x - tips, y, paint);
            }
        } catch (JSONException e) {
            Log.e(TAG, e.toString());
        }
    }
}

From source file:com.emmaguy.todayilearned.PanView.java

/**
 * Draws the overscroll "glow" at the four edges, if necessary
 *
 * @see EdgeEffect/*from   ww  w  .j  a  va2s .co m*/
 */
private void drawEdgeEffects(Canvas canvas) {
    // The methods below rotate and translate the canvas as needed before drawing the glow,
    // since EdgeEffect always draws a top-glow at 0,0.

    boolean needsInvalidate = false;

    if (!mEdgeEffectTop.isFinished()) {
        final int restoreCount = canvas.save();
        mEdgeEffectTop.setSize(mWidth, mHeight);
        if (mEdgeEffectTop.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectBottom.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(-mWidth, mHeight);
        canvas.rotate(180, mWidth, 0);
        mEdgeEffectBottom.setSize(mWidth, mHeight);
        if (mEdgeEffectBottom.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectLeft.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(0, mHeight);
        canvas.rotate(-90, 0, 0);
        //noinspection SuspiciousNameCombination
        mEdgeEffectLeft.setSize(mHeight, mWidth);
        if (mEdgeEffectLeft.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (!mEdgeEffectRight.isFinished()) {
        final int restoreCount = canvas.save();
        canvas.translate(mWidth, 0);
        canvas.rotate(90, 0, 0);
        //noinspection SuspiciousNameCombination
        mEdgeEffectRight.setSize(mHeight, mWidth);
        if (mEdgeEffectRight.draw(canvas)) {
            needsInvalidate = true;
        }
        canvas.restoreToCount(restoreCount);
    }

    if (needsInvalidate) {
        invalidate();
    }
}

From source file:com.evilduck.piano.views.instrument.PianoView.java

@Override
protected void onDraw(Canvas canvas) {
    if (isInEditMode()) {
        canvas.drawColor(Color.GRAY);
        return;//from  w  w  w. j a  v a 2 s. c om
    }

    if (measurementChanged) {
        measurementChanged = false;
        keyboard.initializeInstrument(getMeasuredHeight(), getContext());

        float oldInstrumentWidth = instrumentWidth;
        instrumentWidth = keyboard.getWidth();

        float ratio = (float) instrumentWidth / oldInstrumentWidth;
        xOffset = (int) (xOffset * ratio);
    }

    int localXOffset = getOffsetInsideOfBounds();

    canvas.save();
    canvas.scale(scaleX, 1.0f);
    canvas.translate(-localXOffset, 0);

    keyboard.updateBounds(localXOffset, canvasWidth + localXOffset);
    keyboard.draw(canvas);

    if (!notesToDraw.isEmpty()) {
        keyboard.drawOverlays(notesToDraw, canvas);
    }

    canvas.restore();
}

From source file:com.android.photos.views.GalleryThumbnailView.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    if (!mLeftEdge.isFinished()) {
        final int restoreCount = canvas.save();
        final int height = getHeight() - getPaddingTop() - getPaddingBottom();

        canvas.rotate(270);/* ww  w .j  a v  a2  s . c om*/
        canvas.translate(-height + getPaddingTop(), 0);
        mLeftEdge.setSize(height, getWidth());
        if (mLeftEdge.draw(canvas)) {
            postInvalidateOnAnimation();
        }
        canvas.restoreToCount(restoreCount);
    }
    if (!mRightEdge.isFinished()) {
        final int restoreCount = canvas.save();
        final int width = getWidth();
        final int height = getHeight() - getPaddingTop() - getPaddingBottom();

        canvas.rotate(90);
        canvas.translate(-getPaddingTop(), width);
        mRightEdge.setSize(height, width);
        if (mRightEdge.draw(canvas)) {
            postInvalidateOnAnimation();
        }
        canvas.restoreToCount(restoreCount);
    }
}

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

@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    boolean result = super.drawChild(canvas, child, drawingTime);
    if (child == imageView) {
        if (countLayout != null) {
            int top = AndroidUtilities.dp(6);
            int left = AndroidUtilities.dp(54);
            int x = left - AndroidUtilities.dp(5.5f);
            if (MessagesController.getInstance().isDialogMuted(dialog_id)) {
                countDrawableGrey.setBounds(x, top, x + countWidth + AndroidUtilities.dp(11),
                        top + countDrawableGrey.getIntrinsicHeight());
                countDrawableGrey.draw(canvas);
            } else {
                countDrawable.setBounds(x, top, x + countWidth + AndroidUtilities.dp(11),
                        top + countDrawable.getIntrinsicHeight());
                countDrawable.draw(canvas);
            }//from  w ww .  jav a  2  s . c  om
            canvas.save();
            canvas.translate(left, top + AndroidUtilities.dp(4));
            countLayout.draw(canvas);
            canvas.restore();
        }
    }
    return result;
}

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 ww. j  a  va 2s  . c om*/
        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.v7.testutils.TestUtils.java

/**
 * This method takes a view and returns a single bitmap that is the layered combination
 * of background drawables of this view and all its ancestors. It can be used to abstract
 * away the specific implementation of a view hierarchy that is not exposed via class APIs
 * or a view hierarchy that depends on the platform version. Instead of hard-coded lookups
 * of particular inner implementations of such a view hierarchy that can break during
 * refactoring or on newer platform versions, calling this API returns a "combined" background
 * of the view.// w ww. j  a v  a  2  s  . c  o  m
 *
 * For example, it is useful to get the combined background of a popup / dropdown without
 * delving into the inner implementation details of how that popup is implemented on a
 * particular platform version.
 */
public static Bitmap getCombinedBackgroundBitmap(View view) {
    final int bitmapWidth = view.getWidth();
    final int bitmapHeight = view.getHeight();

    // Create a bitmap
    final Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
    // Create a canvas that wraps the bitmap
    final Canvas canvas = new Canvas(bitmap);

    // As the draw pass starts at the top of view hierarchy, our first step is to traverse
    // the ancestor hierarchy of our view and collect a list of all ancestors with non-null
    // and visible backgrounds. At each step we're keeping track of the combined offsets
    // so that we can properly combine all of the visuals together in the next pass.
    List<View> ancestorsWithBackgrounds = new ArrayList<>();
    List<Pair<Integer, Integer>> ancestorOffsets = new ArrayList<>();
    int offsetX = 0;
    int offsetY = 0;
    while (true) {
        final Drawable backgroundDrawable = view.getBackground();
        if ((backgroundDrawable != null) && backgroundDrawable.isVisible()) {
            ancestorsWithBackgrounds.add(view);
            ancestorOffsets.add(Pair.create(offsetX, offsetY));
        }
        // Go to the parent
        ViewParent parent = view.getParent();
        if (!(parent instanceof View)) {
            // We're done traversing the ancestor chain
            break;
        }

        // Update the offsets based on the location of current view in its parent's bounds
        offsetX += view.getLeft();
        offsetY += view.getTop();

        view = (View) parent;
    }

    // Now we're going to iterate over the collected ancestors in reverse order (starting from
    // the topmost ancestor) and draw their backgrounds into our combined bitmap. At each step
    // we are respecting the offsets of our original view in the coordinate system of the
    // currently drawn ancestor.
    final int layerCount = ancestorsWithBackgrounds.size();
    for (int i = layerCount - 1; i >= 0; i--) {
        View ancestor = ancestorsWithBackgrounds.get(i);
        Pair<Integer, Integer> offsets = ancestorOffsets.get(i);

        canvas.translate(offsets.first, offsets.second);
        ancestor.getBackground().draw(canvas);
        canvas.translate(-offsets.first, -offsets.second);
    }

    return bitmap;
}

From source file:com.justplay1.shoppist.features.search.widget.internal.SuggestionItemDecorator.java

@Override
public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    int visibleCount = parent.getChildCount();
    int count = state.getItemCount();
    RecyclerView.Adapter adapter = parent.getAdapter();
    int adapterCount = adapter != null ? adapter.getItemCount() : 0;

    for (int i = 0; i < visibleCount; i++) {
        View view = parent.getChildAt(i);
        int position = parent.getChildAdapterPosition(view);
        float translationX = ViewCompat.getTranslationX(view);
        float translationY = ViewCompat.getTranslationY(view);
        float alpha = ViewCompat.getAlpha(view);
        RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

        int shadows = LEFT | RIGHT;
        if (position == count - 1 && adapterCount != 0)
            shadows |= BOTTOM;/*w  ww  . jav a 2s  .  c  om*/

        drawable.setAlpha((int) (255 * alpha));
        drawable.setShadow(shadows);
        drawable.setBounds(0, 0, parent.getWidth(), view.getHeight());
        int saved = canvas.save();
        canvas.translate(parent.getPaddingLeft() + translationX,
                view.getTop() + params.topMargin + translationY);
        drawable.draw(canvas);
        canvas.restoreToCount(saved);
    }
}

From source file:com.chartiq.chartiqsample.ui.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*from   w  w  w  . j  av a 2  s.co m*/
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();
    long previousHeaderId = -1;

    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        final int adapterPos = parent.getChildAdapterPosition(child);
        if (child.getVisibility() == View.GONE) {
            continue;
        }

        if (adapterPos != RecyclerView.NO_POSITION && hasHeader(adapterPos)) {
            long headerId = mAdapter.getHeaderId(adapterPos);

            if (headerId != previousHeaderId) {
                previousHeaderId = headerId;
                View header = getHeader(parent, adapterPos).itemView;
                canvas.save();

                final int left = child.getLeft();
                final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
                canvas.translate(left, top);

                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(canvas);
                canvas.restore();
            }
        }
    }
}