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:com.comcast.freeflow.core.FreeFlowContainer.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    boolean needsInvalidate = false;

    final int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
    final int width = getMeasuredWidth();

    if (!mLeftEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(270);//from   www.  j av  a2  s .co  m
        canvas.translate(-height + getPaddingTop(), 0);// width);
        mLeftEdge.setSize(height, width);

        needsInvalidate = mLeftEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mTopEdge.isFinished()) {
        final int restoreCount = canvas.save();

        mTopEdge.setSize(width, height);

        needsInvalidate = mTopEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mRightEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(90);
        canvas.translate(0, -width);// width);
        mRightEdge.setSize(height, width);

        needsInvalidate = mRightEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (!mBottomEdge.isFinished()) {
        final int restoreCount = canvas.save();

        canvas.rotate(180);
        canvas.translate(-width + getPaddingTop(), -height);

        mBottomEdge.setSize(width, height);

        needsInvalidate = mBottomEdge.draw(canvas);
        canvas.restoreToCount(restoreCount);
    }

    if (needsInvalidate)
        postInvalidateOnAnimation();

}

From source file:org.akop.crosswords.view.CrosswordView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mPuzzleBitmap != null) {
        canvas.save();//from w w w. j  a v  a 2  s . co  m
        canvas.clipRect(mContentRect);
        canvas.translate(mBitmapOffset.x, mBitmapOffset.y);
        canvas.scale(mBitmapScale, mBitmapScale);
        canvas.drawBitmap(mPuzzleBitmap, 0, 0, mBitmapPaint);

        canvas.restore();
    }
}

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

@Override
protected void onDraw(Canvas canvas) {
    if (currentDialogId == 0) {
        return;//from w w w.  j  av a 2  s . c om
    }

    if (isSelected) {
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), backPaint);
    }

    if (drawNameLock) {
        setDrawableBounds(lockDrawable, nameLockLeft, nameLockTop);
        lockDrawable.draw(canvas);
    } else if (drawNameGroup) {
        setDrawableBounds(groupDrawable, nameLockLeft, nameLockTop);
        groupDrawable.draw(canvas);
    } else if (drawNameBroadcast) {
        setDrawableBounds(broadcastDrawable, nameLockLeft, nameLockTop);
        broadcastDrawable.draw(canvas);
    } else if (drawNameBot) {
        setDrawableBounds(botDrawable, nameLockLeft, nameLockTop);
        botDrawable.draw(canvas);
    }

    if (nameLayout != null) {
        canvas.save();
        canvas.translate(nameLeft, AndroidUtilities.dp(13));
        nameLayout.draw(canvas);
        canvas.restore();
    }

    canvas.save();
    canvas.translate(timeLeft, timeTop);
    timeLayout.draw(canvas);
    canvas.restore();

    if (messageLayout != null) {
        canvas.save();
        canvas.translate(messageLeft, messageTop);
        try {
            messageLayout.draw(canvas);
        } catch (Exception e) {
            FileLog.e("tmessages", e);
        }
        canvas.restore();
    }

    if (drawClock) {
        setDrawableBounds(clockDrawable, checkDrawLeft, checkDrawTop);
        clockDrawable.draw(canvas);
    } else if (drawCheck2) {
        if (drawCheck1) {
            setDrawableBounds(halfCheckDrawable, halfCheckDrawLeft, checkDrawTop);
            halfCheckDrawable.draw(canvas);
            setDrawableBounds(checkDrawable, checkDrawLeft, checkDrawTop);
            checkDrawable.draw(canvas);
        } else {
            setDrawableBounds(checkDrawable, checkDrawLeft, checkDrawTop);
            checkDrawable.draw(canvas);
        }
    }

    if (dialogMuted && !drawVerified) {
        setDrawableBounds(muteDrawable, nameMuteLeft, AndroidUtilities.dp(16.5f));
        muteDrawable.draw(canvas);
    } else if (drawVerified) {
        setDrawableBounds(verifiedDrawable, nameMuteLeft, AndroidUtilities.dp(16.5f));
        verifiedDrawable.draw(canvas);
    }

    if (drawError) {
        setDrawableBounds(errorDrawable, errorLeft, errorTop);
        errorDrawable.draw(canvas);
    } else if (drawCount) {
        if (dialogMuted) {
            setDrawableBounds(countDrawableGrey, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawableGrey.draw(canvas);
        } else {
            setDrawableBounds(countDrawable, countLeft - AndroidUtilities.dp(5.5f), countTop,
                    countWidth + AndroidUtilities.dp(11), countDrawable.getIntrinsicHeight());
            countDrawable.draw(canvas);
        }
        canvas.save();
        canvas.translate(countLeft, countTop + AndroidUtilities.dp(4));
        if (countLayout != null) {
            countLayout.draw(canvas);
        }
        canvas.restore();
    }

    if (useSeparator) {
        if (LocaleController.isRTL) {
            canvas.drawLine(0, getMeasuredHeight() - 1,
                    getMeasuredWidth() - AndroidUtilities.dp(AndroidUtilities.leftBaseline),
                    getMeasuredHeight() - 1, linePaint);
        } else {
            canvas.drawLine(AndroidUtilities.dp(AndroidUtilities.leftBaseline), getMeasuredHeight() - 1,
                    getMeasuredWidth(), getMeasuredHeight() - 1, linePaint);
        }
    }

    avatarImage.draw(canvas);
}

From source file:cn.ieclipse.af.view.StaggeredGridView.java

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

    if (mTopEdge != null) {
        boolean needsInvalidate = false;
        if (!mTopEdge.isFinished()) {
            mTopEdge.draw(canvas);//from  www .j  a  va2s .  c  o m
            needsInvalidate = true;
        }
        if (!mBottomEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            canvas.translate(-width, getHeight());
            canvas.rotate(180, width, 0);
            mBottomEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
            needsInvalidate = true;
        }

        if (needsInvalidate) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }
}

From source file:com.mjhram.geodata.GpsMainActivity.java

public Bitmap drawMultilineTextToBitmap(Context gContext, Bitmap bitmap, String gText) {
    // prepare canvas
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;
    //Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//from   www .ja v a 2 s . com
    // resource bitmaps are imutable,
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);

    // new antialiased Paint
    TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (20 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);
    //canvas.drawText("This is", 100, 100, paint);
    //canvas.drawText("multi-line", 100, 150, paint);
    //canvas.drawText("text", 100, 200, paint);

    // set text width to canvas width minus 16dp padding
    int textWidth = canvas.getWidth() - (int) (16 * scale);

    // init StaticLayout for text
    StaticLayout textLayout = new StaticLayout(gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f,
            0.0f, false);

    // get height of multiline text
    int textHeight = textLayout.getHeight();

    // get position of text's top left corner
    float x = (bitmap.getWidth() - textWidth) / 2;
    float y = bitmap.getHeight() - textHeight;

    // draw text to the Canvas center
    canvas.save();
    canvas.translate(x, y);
    textLayout.draw(canvas);
    canvas.restore();

    return bitmap;
}

From source file:com.jude.easyrecyclerview.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}// w  w w  .j  av a  2 s .  c  o m
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    final int count = parent.getChildCount();
    long previousHeaderId = -1;

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

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof RecyclerArrayAdapter) {
                int headerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((RecyclerArrayAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((RecyclerArrayAdapter) parent.getAdapter()).getCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

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

From source file:com.youth.xframe.adapter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*w  ww .  j  a  v a  2s  .co  m*/
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    final int count = parent.getChildCount();
    long previousHeaderId = -1;

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

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof XRecyclerViewAdapter) {
                int headerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((XRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((XRecyclerViewAdapter) parent.getAdapter()).getDataCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

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

From source file:com.sanjie.zy.adpter.decoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*ww w  . j  a va 2 s  .c o m*/
 */
@Override
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    if (parent.getAdapter() == null) {
        return;
    }

    final int count = parent.getChildCount();
    long previousHeaderId = -1;

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

        if (!mIncludeHeader) {
            if (parent.getAdapter() instanceof ZYRecyclerViewAdapter) {
                int headerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getHeaderCount();
                int footerCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getFooterCount();
                int dataCount = ((ZYRecyclerViewAdapter) parent.getAdapter()).getDataCount();
                if (adapterPos < headerCount) {
                    continue;
                }
                if (adapterPos >= headerCount + dataCount) {
                    continue;
                }
                if (adapterPos >= headerCount) {
                    adapterPos -= headerCount;
                }

            }
        }

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

From source file:com.android.mail.browse.ConversationItemView.java

private void drawSnippet(Canvas canvas) {
    // if folders exist, their width will be the max width - actual width
    final int folderWidth = mCoordinates.maxSnippetWidth - mSnippetTextView.getWidth();

    // in RTL layouts we move the snippet to the right so it doesn't overlap the folders
    final int x = mCoordinates.snippetX + (ViewUtils.isViewRtl(this) ? folderWidth : 0);
    canvas.translate(x, mCoordinates.snippetY);
    mSnippetTextView.draw(canvas);//from   w w w .  ja v a 2 s. co  m
}

From source file:com.irccloud.android.DrawerArrowDrawable.java

@Override
public void draw(Canvas canvas) {
    Rect bounds = getBounds();// w w w  . j a  v a  2s .  c o  m
    final boolean isRtl = isLayoutRtl();
    // Interpolated widths of arrow bars
    final float arrowSize = lerp(mBarSize, mTopBottomArrowSize, mProgress);
    final float middleBarSize = lerp(mBarSize, mMiddleArrowSize, mProgress);
    // Interpolated size of middle bar
    final float middleBarCut = lerp(0, mBarThickness / 2, mProgress);
    // The rotation of the top and bottom bars (that make the arrow head)
    final float rotation = lerp(0, ARROW_HEAD_ANGLE, mProgress);

    // The whole canvas rotates as the transition happens
    final float canvasRotate = lerp(isRtl ? 0 : -180, isRtl ? 180 : 0, mProgress);
    final float topBottomBarOffset = lerp(mBarGap + mBarThickness, 0, mProgress);
    mPath.rewind();

    final float arrowEdge = -middleBarSize / 2;
    // draw middle bar
    mPath.moveTo(arrowEdge + middleBarCut, 0);
    mPath.rLineTo(middleBarSize - middleBarCut, 0);

    final float arrowWidth = Math.round(arrowSize * Math.cos(rotation));
    final float arrowHeight = Math.round(arrowSize * Math.sin(rotation));

    // top bar
    mPath.moveTo(arrowEdge, topBottomBarOffset);
    mPath.rLineTo(arrowWidth, arrowHeight);

    // bottom bar
    mPath.moveTo(arrowEdge, -topBottomBarOffset);
    mPath.rLineTo(arrowWidth, -arrowHeight);
    mPath.moveTo(0, 0);
    mPath.close();

    canvas.save();
    // Rotate the whole canvas if spinning, if not, rotate it 180 to get
    // the arrow pointing the other way for RTL.
    if (mSpin) {
        canvas.rotate(canvasRotate * ((mVerticalMirror ^ isRtl) ? -1 : 1), bounds.centerX(), bounds.centerY());
    } else if (isRtl) {
        canvas.rotate(180, bounds.centerX(), bounds.centerY());
    }
    canvas.translate(bounds.centerX(), bounds.centerY());
    canvas.drawPath(mPath, mPaint);

    canvas.restore();
}