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.chenglong.muscle.ui.LazyViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;
    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            canvas.rotate(270);//from  w  w w . ja  v  a  2  s.c o  m
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;
            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -itemCount * (width + mPageMargin) + mPageMargin);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }
    if (needsInvalidate) {
        // Keep animating
        invalidate();
    }
}

From source file:lewa.support.v7.widget.SwitchCompat.java

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

    final Rect padding = mTempRect;
    final Drawable trackDrawable = mTrackDrawable;
    if (trackDrawable != null) {
        trackDrawable.getPadding(padding);
    } else {/*w  w w. ja  va 2s.  co m*/
        padding.setEmpty();
    }

    final int switchTop = mSwitchTop;
    final int switchBottom = mSwitchBottom;
    final int switchInnerTop = switchTop + padding.top;
    final int switchInnerBottom = switchBottom - padding.bottom;

    final Drawable thumbDrawable = mThumbDrawable;
    if (trackDrawable != null) {
        trackDrawable.draw(canvas);
    }

    final int saveCount = canvas.save();

    if (thumbDrawable != null) {
        thumbDrawable.draw(canvas);
    }

    final Layout switchText = getTargetCheckedState() ? mOnLayout : mOffLayout;
    if (switchText != null) {
        final int drawableState[] = getDrawableState();
        if (mTextColors != null) {
            mTextPaint.setColor(mTextColors.getColorForState(drawableState, 0));
        }
        mTextPaint.drawableState = drawableState;

        final int cX;
        if (thumbDrawable != null) {
            final Rect bounds = thumbDrawable.getBounds();
            cX = bounds.left + bounds.right;
        } else {
            cX = getWidth();
        }

        final int left = cX / 2 - switchText.getWidth() / 2;
        final int top = (switchInnerTop + switchInnerBottom) / 2 - switchText.getHeight() / 2;
        canvas.translate(left, top);
        switchText.draw(canvas);
    }

    canvas.restoreToCount(saveCount);
}

From source file:am.widget.indicatortabstrip.IndicatorTabStrip.java

/**
 * //from  ww w  .  j  a  va 2 s. c o  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:de.andacaydin.bidirectionalviewpagerlibrary.BiDirectionalViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;
    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int width = getWidth();
            canvas.rotate(270);/* ww w  .  j  a  v  a 2 s.c  o m*/
            canvas.translate(-height + getPaddingTop(), mFirstOffset * width);
            mLeftEdge.setSize(height, width);
            needsInvalidate |= mLeftEdge.draw(canvas);
            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(), -(mLastOffset + 1) * width);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }
    if (needsInvalidate) {
        // Keep animating
        ViewCompat.postInvalidateOnAnimation(this);
    }
}

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

@Override
protected void onDraw(Canvas canvas) {
    if (mCoordinates == null) {
        LogUtils.e(LOG_TAG, "null coordinates in ConversationItemView#onDraw");
        return;//from  w w w.  j a  v  a2s  .com
    }

    Utils.traceBeginSection("CIVC.draw");

    // Contact photo
    if (mGadgetMode == ConversationItemViewCoordinates.GADGET_CONTACT_PHOTO) {
        canvas.save();
        Utils.traceBeginSection("draw senders image");
        drawSendersImage(canvas);
        Utils.traceEndSection();
        canvas.restore();
    }

    // Senders.
    boolean isUnread = mHeader.unread;
    // Old style senders; apply text colors/ sizes/ styling.
    canvas.save();
    if (mHeader.sendersDisplayLayout != null) {
        sPaint.setTextSize(mCoordinates.sendersFontSize);
        sPaint.setTypeface(SendersView.getTypeface(isUnread));
        sPaint.setColor(sSendersTextColor);
        canvas.translate(mSendersX, mCoordinates.sendersY + mHeader.sendersDisplayLayout.getTopPadding());
        mHeader.sendersDisplayLayout.draw(canvas);
    } else {
        drawSenders(canvas);
    }
    canvas.restore();

    // Subject.
    sPaint.setTypeface(Typeface.DEFAULT);
    canvas.save();
    drawSubject(canvas);
    canvas.restore();

    canvas.save();
    drawSnippet(canvas);
    canvas.restore();

    // Folders.
    if (mConfig.areFoldersVisible()) {
        mHeader.folderDisplayer.drawFolders(canvas, mCoordinates, ViewUtils.isViewRtl(this));
    }

    // If this folder has a color (combined view/Email), show it here
    if (mConfig.isColorBlockVisible()) {
        sFoldersPaint.setColor(mHeader.conversation.color);
        sFoldersPaint.setStyle(Paint.Style.FILL);
        canvas.drawRect(mCoordinates.colorBlockX, mCoordinates.colorBlockY,
                mCoordinates.colorBlockX + mCoordinates.colorBlockWidth,
                mCoordinates.colorBlockY + mCoordinates.colorBlockHeight, sFoldersPaint);
    }

    // Draw the reply state. Draw nothing if neither replied nor forwarded.
    if (mConfig.isReplyStateVisible()) {
        if (mHeader.hasBeenRepliedTo && mHeader.hasBeenForwarded) {
            canvas.drawBitmap(STATE_REPLIED_AND_FORWARDED, mCoordinates.replyStateX, mCoordinates.replyStateY,
                    null);
        } else if (mHeader.hasBeenRepliedTo) {
            canvas.drawBitmap(STATE_REPLIED, mCoordinates.replyStateX, mCoordinates.replyStateY, null);
        } else if (mHeader.hasBeenForwarded) {
            canvas.drawBitmap(STATE_FORWARDED, mCoordinates.replyStateX, mCoordinates.replyStateY, null);
        } else if (mHeader.isInvite) {
            canvas.drawBitmap(STATE_CALENDAR_INVITE, mCoordinates.replyStateX, mCoordinates.replyStateY, null);
        }
    }

    if (mConfig.isPersonalIndicatorVisible()) {
        canvas.drawBitmap(mHeader.personalLevelBitmap, mCoordinates.personalIndicatorX,
                mCoordinates.personalIndicatorY, null);
    }

    // Info icon
    if (mHeader.infoIcon != null) {
        canvas.drawBitmap(mHeader.infoIcon, mInfoIconX, mCoordinates.infoIconY, sPaint);
    }

    // Date.
    sPaint.setTextSize(mCoordinates.dateFontSize);
    sPaint.setTypeface(isUnread ? SANS_SERIF_BOLD : SANS_SERIF_LIGHT);
    sPaint.setColor(isUnread ? sDateTextColorUnread : sDateTextColorRead);
    drawText(canvas, mHeader.dateText, mDateX, mCoordinates.dateYBaseline, sPaint);

    // Paper clip icon.
    if (mHeader.paperclip != null) {
        canvas.drawBitmap(mHeader.paperclip, mPaperclipX, mCoordinates.paperclipY, sPaint);
    }

    // Star.
    if (mStarEnabled) {
        canvas.drawBitmap(getStarBitmap(), mCoordinates.starX, mCoordinates.starY, sPaint);
    }

    // Divider.
    if (mDividerEnabled) {
        final int dividerBottomY = getHeight();
        final int dividerTopY = dividerBottomY - sDividerHeight;
        canvas.drawRect(0, dividerTopY, getWidth(), dividerBottomY, sDividerPaint);
    }

    // The focused bar
    final SwipeableListView listView = getListView();
    if (listView != null && listView.isConversationSelected(getConversation())) {
        final int w = FOCUSED_CONVERSATION_HIGHLIGHT.getIntrinsicWidth();
        final boolean isRtl = ViewUtils.isViewRtl(this);
        // This bar is on the right side of the conv list if it's RTL
        FOCUSED_CONVERSATION_HIGHLIGHT.setBounds((isRtl) ? getWidth() - w : 0, 0, (isRtl) ? getWidth() : w,
                getHeight());
        FOCUSED_CONVERSATION_HIGHLIGHT.draw(canvas);
    }

    Utils.traceEndSection();
}

From source file:org.immopoly.android.widget.ViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(270);//from ww w . jav a2 s . com
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = forcedChildWidth > 0 ? forcedChildWidth : getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -itemCount * (width + mPageMargin) + mPageMargin);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    }
}

From source file:beichen.douban.ui.view.LazyViewPager.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    boolean needsInvalidate = false;

    final int overScrollMode = ViewCompat.getOverScrollMode(this);
    if (overScrollMode == ViewCompat.OVER_SCROLL_ALWAYS
            || (overScrollMode == ViewCompat.OVER_SCROLL_IF_CONTENT_SCROLLS && mAdapter != null
                    && mAdapter.getCount() > 1)) {
        if (!mLeftEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();

            canvas.rotate(270);/*from   w  w  w .j  a  v a  2 s . c  om*/
            canvas.translate(-height + getPaddingTop(), 0);
            mLeftEdge.setSize(height, getWidth());
            needsInvalidate |= mLeftEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
        if (!mRightEdge.isFinished()) {
            final int restoreCount = canvas.save();
            final int width = getWidth();
            final int height = getHeight() - getPaddingTop() - getPaddingBottom();
            final int itemCount = mAdapter != null ? mAdapter.getCount() : 1;

            canvas.rotate(90);
            canvas.translate(-getPaddingTop(), -itemCount * (width + mPageMargin) + mPageMargin);
            mRightEdge.setSize(height, width);
            needsInvalidate |= mRightEdge.draw(canvas);
            canvas.restoreToCount(restoreCount);
        }
    } else {
        mLeftEdge.finish();
        mRightEdge.finish();
    }

    if (needsInvalidate) {
        // Keep animating
        invalidate();
    }
}

From source file:com.ftinc.kit.attributr.ui.widget.StickyRecyclerHeadersElevationDecoration.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override// w w w  .j av  a 2s.c om
public void onDrawOver(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(canvas, parent, state);
    int orientation = getOrientation(parent);
    mHeaderRects.clear();

    if (parent.getChildCount() > 0 && mAdapter.getItemCount() > 0) {

        // draw the first visible child's header at the top of the view
        View firstView = parent.getChildAt(0);
        int firstPosition = parent.getChildPosition(firstView);

        if (mAdapter.getHeaderId(firstPosition) > -1) {

            View firstHeader = getHeaderView(parent, firstPosition);
            View nextView = getNextView(parent);

            int translationX = Math.max(parent.getChildAt(0).getLeft() - firstHeader.getWidth(), 0);
            int translationY = Math.max(parent.getChildAt(0).getTop() - firstHeader.getHeight(), 0);
            int nextPosition = parent.getChildPosition(nextView);

            if (nextPosition > 0 && hasNewHeader(nextPosition)) {

                View secondHeader = getHeaderView(parent, nextPosition);

                //Translate the topmost header so the next header takes its place, if applicable
                if (orientation == LinearLayoutManager.VERTICAL
                        && nextView.getTop() - secondHeader.getHeight() - firstHeader.getHeight() < 0) {

                    translationY += nextView.getTop() - secondHeader.getHeight() - firstHeader.getHeight();

                } else if (orientation == LinearLayoutManager.HORIZONTAL
                        && nextView.getLeft() - secondHeader.getWidth() - firstHeader.getWidth() < 0) {

                    translationX += nextView.getLeft() - secondHeader.getWidth() - firstHeader.getWidth();

                }
            }

            boolean shouldDrawShadow = true;
            if (firstPosition == 0 && (firstView.getTop() == firstHeader.getHeight())) {
                shouldDrawShadow = false;
            }

            if (translationY == 0 && shouldDrawShadow) {

                ImageView shadow = ButterKnife.findById(firstHeader, R.id.shadow);
                shadow.setVisibility(View.VISIBLE);

            } else {

                ImageView shadow = ButterKnife.findById(firstHeader, R.id.shadow);
                shadow.setVisibility(View.GONE);

            }

            canvas.save();
            canvas.translate(translationX, translationY);
            firstHeader.draw(canvas);
            canvas.restore();
            mHeaderRects.put(firstPosition, new Rect(translationX, translationY,
                    translationX + firstHeader.getWidth(), translationY + firstHeader.getHeight()));

        }

        if (parent.getChildCount() > 1 && mAdapter.getItemCount() > 1) {
            for (int i = 1; i < parent.getChildCount(); i++) {

                int position = parent.getChildPosition(parent.getChildAt(i));
                if (hasNewHeader(position)) {

                    // this header is different than the previous, it must be drawn in the correct place
                    int translationX = 0;
                    int translationY = 0;
                    View header = getHeaderView(parent, position);

                    if (orientation == LinearLayoutManager.VERTICAL) {

                        translationY = parent.getChildAt(i).getTop() - header.getHeight();

                    } else {

                        translationX = parent.getChildAt(i).getLeft() - header.getWidth();

                    }

                    // don't render shadow
                    ImageView shadow = ButterKnife.findById(header, R.id.shadow);
                    shadow.setVisibility(View.GONE);

                    canvas.save();
                    canvas.translate(translationX, translationY);
                    header.draw(canvas);
                    canvas.restore();
                    mHeaderRects.put(position, new Rect(translationX, translationY,
                            translationX + header.getWidth(), translationY + header.getHeight()));
                }
            }
        }
    }
}

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

@Override
public void onDraw(Canvas canvas) {
    mPaint.reset();//  w w  w  .j a  v  a  2  s .com
    mPaint.setAntiAlias(true);
    mPaint.setFilterBitmap(true);
    MasterImage.getImage().setImageShowSize(getWidth() - 2 * mShadowMargin, getHeight() - 2 * mShadowMargin);

    MasterImage img = MasterImage.getImage();
    // Hide the loading indicator as needed
    if (mActivity.isLoadingVisible() && getFilteredImage() != null) {
        if ((img.getLoadedPreset() == null)
                || (img.getLoadedPreset() != null && img.getLoadedPreset().equals(img.getCurrentPreset()))) {
            mActivity.stopLoadingIndicator();
        } else if (img.getLoadedPreset() != null) {
            return;
        }
        mActivity.stopLoadingIndicator();
    }

    canvas.save();

    mShadowDrawn = false;

    Bitmap highresPreview = MasterImage.getImage().getHighresImage();
    Bitmap fullHighres = MasterImage.getImage().getPartialImage();

    boolean isDoingNewLookAnimation = MasterImage.getImage().onGoingNewLookAnimation();

    if (highresPreview == null || isDoingNewLookAnimation) {
        drawImageAndAnimate(canvas, getFilteredImage());
    } else {
        drawImageAndAnimate(canvas, highresPreview);
    }

    drawHighresImage(canvas, fullHighres);
    drawCompareImage(canvas, getGeometryOnlyImage());

    canvas.restore();

    if (!mEdgeEffect.isFinished()) {
        canvas.save();
        float dx = (getHeight() - getWidth()) / 2f;
        if (getWidth() > getHeight()) {
            dx = -(getWidth() - getHeight()) / 2f;
        }
        if (mCurrentEdgeEffect == EDGE_BOTTOM) {
            canvas.rotate(180, getWidth() / 2, getHeight() / 2);
        } else if (mCurrentEdgeEffect == EDGE_RIGHT) {
            canvas.rotate(90, getWidth() / 2, getHeight() / 2);
            canvas.translate(0, dx);
        } else if (mCurrentEdgeEffect == EDGE_LEFT) {
            canvas.rotate(270, getWidth() / 2, getHeight() / 2);
            canvas.translate(0, dx);
        }
        if (mCurrentEdgeEffect != 0) {
            mEdgeEffect.draw(canvas);
        }
        canvas.restore();
        invalidate();
    } else {
        mCurrentEdgeEffect = 0;
    }
}

From source file:com.aizou.core.widget.pagerIndicator.indicator.FixedIndicatorView.java

private void drawSlideBar(Canvas canvas) {
    if (mAdapter == null || scrollBar == null) {
        return;// w w  w.ja v a  2  s  . c  om
    }
    final int count = mAdapter.getCount();
    if (count == 0) {
        return;
    }
    if (getCurrentItem() >= count) {
        setCurrentItem(count - 1);
        return;
    }
    float offsetX = 0;
    int offsetY = 0;
    switch (this.scrollBar.getGravity()) {
    case CENTENT_BACKGROUND:
    case CENTENT:
        offsetY = (getHeight() - scrollBar.getHeight(getHeight())) / 2;

        break;
    case TOP:
    case TOP_FLOAT:
        offsetY = 0;
        break;
    case BOTTOM:
    case BOTTOM_FLOAT:
    default:
        offsetY = getHeight() - scrollBar.getHeight(getHeight());
        break;
    }
    View currentView = null;
    if (!inRun.isFinished() && inRun.computeScrollOffset()) {
        offsetX = inRun.getCurrentX();
        int position = 0;
        for (int i = 0; i < count; i++) {
            currentView = getChildAt(i);
            if (currentView.getLeft() <= offsetX && offsetX < currentView.getRight()) {
                position = i;
                break;
            }
        }
        int width = currentView.getWidth();
        int positionOffsetPixels = (int) (offsetX - currentView.getLeft());
        float positionOffset = (offsetX - currentView.getLeft()) / width;
        notifyPageScrolled(position, positionOffset, positionOffsetPixels);
    } else if (mPositionOffset - 0.0f > 0.01) {
        currentView = getChildAt(mPosition);
        int width = currentView.getWidth();
        offsetX = currentView.getLeft() + width * mPositionOffset;
        notifyPageScrolled(mPosition, mPositionOffset, mPositionOffsetPixels);
    } else {
        currentView = getChildAt(mSelectedTabIndex);
        if (currentView == null) {
            return;
        }
        offsetX = currentView.getLeft();
    }
    int tabWidth = currentView.getWidth();
    int width = scrollBar.getWidth(tabWidth);
    width = Math.min(tabWidth, width);
    offsetX += (tabWidth - width) / 2;
    int saveCount = canvas.save();
    canvas.translate(offsetX, offsetY);
    canvas.clipRect(0, 0, width, scrollBar.getHeight(getHeight())); // needed

    int preHeight = scrollBar.getSlideView().getHeight();
    int preWidth = scrollBar.getSlideView().getWidth();
    if (preHeight != scrollBar.getHeight(getHeight()) || preWidth != scrollBar.getWidth(tabWidth)) {
        measureScrollBar(true);
    }

    scrollBar.getSlideView().draw(canvas);
    canvas.restoreToCount(saveCount);
}