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:cn.lanmei.com.dingdong_2.ItemDecoration.StickyHeaderDecoration.java

/**
 * {@inheritDoc}/*from   www. j a  v a 2  s .co  m*/
 */
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    //        final int count = parent.getChildCount();
    LRecyclerViewAdapter adapter = (LRecyclerViewAdapter) parent.getAdapter();
    RecyclerView.Adapter ra = adapter.getInnerAdapter();
    final int count = ra.getItemCount();

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

        final int adapterPos = parent.getChildAdapterPosition(child);

        //            L.MyLog("sticky","onDrawOver:"+adapterPos+"getItemCount"+count);
        if (adapterPos != RecyclerView.NO_POSITION && (layoutPos == 0 || hasHeader(adapterPos))) {

            View header = getHeader(parent, adapterPos).itemView;
            c.save();
            final int left = child.getLeft();
            final int top = getHeaderTop(parent, child, header, adapterPos, layoutPos);
            c.translate(left, top);
            header.setTranslationX(left);
            header.setTranslationY(top);
            header.draw(c);
            c.restore();
        }
    }
}

From source file:kankan.wheel.widget.WheelView.java

/**
 * Draws items/* w w  w  .j  a v a 2s .c  o  m*/
 * @param canvas the canvas for drawing
 */
private void drawItems(Canvas canvas) {
    canvas.save();

    int top = (currentItem - firstItem) * getItemHeight() + (getItemHeight() - getHeight()) / 2;
    canvas.translate(PADDING, -top + scrollingOffset);

    //??
    AbstractWheelTextAdapter adapter = null;
    if (viewAdapter instanceof AbstractWheelTextAdapter) {
        adapter = (AbstractWheelTextAdapter) viewAdapter;
    }
    if (adapter != null && adapter.isEnableMultiTextColor()) {
        int targetIndex = -1;
        int minDis = Integer.MAX_VALUE;
        for (int i = 0; i < itemsLayout.getChildCount(); i++) {
            TextView child = (TextView) itemsLayout.getChildAt(i);
            int realTop = child.getTop() - top + scrollingOffset;
            int viewCenter = realTop + child.getHeight() / 2;
            Rect bounds = centerDrawable.getBounds();
            int dis = Math.abs(viewCenter - bounds.centerY());
            if (dis < minDis) {
                minDis = dis;
                targetIndex = i;
            }
        }

        for (int i = 0; i < itemsLayout.getChildCount(); i++) {
            TextView view = (TextView) itemsLayout.getChildAt(i);
            view.setAlpha(0.5f);
            view.setTextColor(adapter.getTextColor());
            view.setTextSize(adapter.getTextSize());
            adapter.setTextViewPadding(view, adapter.getTextPaddingTop(), adapter.getTextPaddingBottom());

        }

        if (targetIndex != -1) {
            TextView view = (TextView) itemsLayout.getChildAt(targetIndex);
            view.setAlpha(1);
            view.setTextColor(adapter.getTextSelectedColor());
            view.setTextSize(adapter.getTextSize() + 4);
            adapter.setTextViewPadding(view, adapter.getTextPaddingTop() - 2,
                    adapter.getTextPaddingBottom() - 2);
        }

        TextView view = (TextView) itemsLayout.getChildAt(targetIndex - 1);
        if (view != null)
            view.setAlpha(1);
        view = (TextView) itemsLayout.getChildAt(targetIndex + 1);
        if (view != null)
            view.setAlpha(1);
    }

    itemsLayout.draw(canvas);

    canvas.restore();
}

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

private void drawIcon(Canvas canvas) {
    if (mIcon == null)
        return;//  w w w.j a va  2  s  .c  o m
    canvas.save();
    float translationX = calculateIconTranslationX();
    float translationY = calculateIconTranslationY();
    canvas.translate(translationX, translationY);
    float scale = calculateIconScale();
    canvas.scale(scale, scale);
    mIcon.draw(canvas);
    canvas.restore();
}

From source file:org.chromium.chrome.browser.omnibox.SuggestionView.java

/**
 * Constructs a new omnibox suggestion view.
 *
 * @param context The context used to construct the suggestion view.
 * @param locationBar The location bar showing these suggestions.
 *//*from w  w w .ja  va 2 s.  com*/
public SuggestionView(Context context, LocationBar locationBar) {
    super(context);
    mLocationBar = locationBar;

    mSuggestionHeight = context.getResources().getDimensionPixelOffset(R.dimen.omnibox_suggestion_height);
    mSuggestionAnswerHeight = context.getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_answer_height);

    TypedArray a = getContext().obtainStyledAttributes(new int[] { R.attr.selectableItemBackground });
    Drawable itemBackground = a.getDrawable(0);
    a.recycle();

    mContentsView = new SuggestionContentsContainer(context, itemBackground);
    addView(mContentsView);

    mRefineView = new View(context) {
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            if (mRefineIcon == null)
                return;
            canvas.save();
            canvas.translate((getMeasuredWidth() - mRefineIcon.getIntrinsicWidth()) / 2f,
                    (getMeasuredHeight() - mRefineIcon.getIntrinsicHeight()) / 2f);
            mRefineIcon.draw(canvas);
            canvas.restore();
        }

        @Override
        public void setVisibility(int visibility) {
            super.setVisibility(visibility);

            if (visibility == VISIBLE) {
                setClickable(true);
                setFocusable(true);
            } else {
                setClickable(false);
                setFocusable(false);
            }
        }

        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();

            if (mRefineIcon != null && mRefineIcon.isStateful()) {
                mRefineIcon.setState(getDrawableState());
            }
        }
    };
    mRefineView.setContentDescription(getContext().getString(R.string.accessibility_omnibox_btn_refine));

    // Although this has the same background as the suggestion view, it can not be shared as
    // it will result in the state of the drawable being shared and always showing up in the
    // refine view.
    mRefineView.setBackground(itemBackground.getConstantState().newDrawable());
    mRefineView.setId(R.id.refine_view_id);
    mRefineView.setClickable(true);
    mRefineView.setFocusable(true);
    mRefineView.setLayoutParams(new LayoutParams(0, 0));
    addView(mRefineView);

    mRefineWidth = getResources().getDimensionPixelSize(R.dimen.omnibox_suggestion_refine_width);

    mUrlBar = (UrlBar) locationBar.getContainerView().findViewById(R.id.url_bar);

    mPhoneUrlBarLeftOffsetPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset);
    mPhoneUrlBarLeftOffsetRtlPx = getResources()
            .getDimensionPixelOffset(R.dimen.omnibox_suggestion_phone_url_bar_left_offset_rtl);
}

From source file:com.example.view.wheel.WheelView.java

/**
 * Draws value and label layout//from w w  w .j  a  v a 2s  .  c om
 * @param canvas the canvas for drawing
 */
private void drawValue(Canvas canvas) {
    valuePaint.setColor(VALUE_TEXT_COLOR);
    valuePaint.drawableState = getDrawableState();

    Rect bounds = new Rect();
    itemsLayout.getLineBounds(visibleItems / 2, bounds);

    // draw label
    if (labelLayout != null) {
        canvas.save();
        canvas.translate(itemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
        labelLayout.draw(canvas);
        canvas.restore();
    }

    // draw current value
    if (valueLayout != null) {
        canvas.save();
        canvas.translate(0, bounds.top + scrollingOffset);
        valueLayout.draw(canvas);
        canvas.restore();
    }
}

From source file:ca.barrenechea.widget.recyclerview.decoration.DoubleHeaderDecoration.java

/**
 * {@inheritDoc}//from   w w w  .j a  va2s.  co m
 */
@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int count = parent.getChildCount();

    boolean headerDrawn = false;
    for (int layoutPos = 0; layoutPos < count; layoutPos++) {
        final View child = parent.getChildAt(layoutPos);
        boolean visible = getAnimatedTop(child) > -child.getHeight()/* && child.getTop() < parent.getHeight()*/;
        final int adapterPos = parent.getChildAdapterPosition(child);
        if (visible && adapterPos != RecyclerView.NO_POSITION && (!headerDrawn || hasSubHeader(adapterPos))) {
            int left, top;
            final View header = getHeader(parent, adapterPos).itemView;
            final View subHeader = getSubHeader(parent, adapterPos).itemView;

            c.save();
            left = child.getLeft();
            top = getSubHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
            c.translate(left, top);
            subHeader.setTranslationX(left);
            subHeader.setTranslationY(top);
            subHeader.draw(c);
            c.restore();

            if (!headerDrawn || hasHeader(adapterPos)) {
                c.save();
                left = child.getLeft();
                top = getHeaderTop(parent, child, header, subHeader, adapterPos, layoutPos);
                c.translate(left, top);
                header.setTranslationX(left);
                header.setTranslationY(top);
                header.draw(c);
                c.restore();
            }

            headerDrawn = true;
        }
    }
}

From source file:com.am.pagergradienttab.view.PagerGradientTabStrip.java

/**
 * /*  ww w. j ava 2s  .  c om*/
 * 
 * @param canvas
 */
private void drawTag(Canvas canvas) {
    // TODO
    if (mTagAdapter != null) {
        canvas.save();
        float canvasOffset = 0;
        int x = getPaddingLeft();
        int y = getPaddingTop();
        float textTop = 0;
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextAlign(Align.LEFT);
        for (int position = 0; position < tabs.size(); position++) {
            canvas.translate(canvasOffset, 0);
            canvasOffset = itemWidth + intervalWidth;
            canvas.save();
            if (mTagAdapter.isEnable(position)) {
                int textWidth;
                int textHeight;
                mTextPaint.setTextSize(mTagAdapter.getTextSize(position));
                String tag = mTagAdapter.getTag(position) == null ? "" : mTagAdapter.getTag(position);
                textWidth = (int) Math.ceil(mTextPaint.measureText(tag));
                FontMetrics fontMetrics = mTextPaint.getFontMetrics();
                textHeight = (int) Math.ceil(fontMetrics.descent - fontMetrics.ascent);
                textTop = textHeight - (-fontMetrics.ascent - fontMetrics.descent
                        + (fontMetrics.bottom - fontMetrics.descent) * density);
                textHeight += textTop;
                if ("".equals(tag)) {
                    textHeight = 0;
                }
                final Drawable drawable = mTagAdapter.getBackground(position);
                int drawableWidth = drawable == null ? 0 : drawable.getMinimumWidth();
                int drawableHeight = drawable == null ? 0 : drawable.getMinimumHeight();
                float offsetX = Math.max(textWidth + mTagAdapter.getPaddingLeft(position)
                        + mTagAdapter.getPaddingRight(position), drawableWidth);
                float offsetTextX = (offsetX - (textWidth + mTagAdapter.getPaddingLeft(position)
                        + mTagAdapter.getPaddingRight(position))) * 0.5f;
                float offsetY = Math.max(textHeight + mTagAdapter.getPaddingBottom(position)
                        + mTagAdapter.getPaddingTop(position), drawableHeight);
                float offsetTextY = (offsetY - (textHeight + mTagAdapter.getPaddingBottom(position)
                        + mTagAdapter.getPaddingTop(position))) * 0.5f;

                mTextPaint.setTextSize(showTextScale ? textSize * (1 + magnification) : textSize);
                float myTextWidth = mTextPaint.measureText(tabs.get(position));
                TagAlign ta = mTagAdapter.getTagAlign(position);
                switch (ta) {
                default:
                case LEFTTOP:
                    canvas.translate(mTagAdapter.getMarginLeft(position), mTagAdapter.getMarginTop(position));
                    break;
                case LEFTCENTER:
                    canvas.translate(mTagAdapter.getMarginLeft(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;
                case LEFTBOTTOM:
                    canvas.translate(mTagAdapter.getMarginLeft(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;
                case RIGHTTOP:
                    canvas.translate(itemWidth - offsetX - mTagAdapter.getMarginRight(position),
                            mTagAdapter.getMarginTop(position));
                    break;
                case RIGHTCENTER:
                    canvas.translate(itemWidth - offsetX - mTagAdapter.getMarginRight(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;
                case RIGHTBOTTOM:
                    canvas.translate(itemWidth - offsetX - mTagAdapter.getMarginRight(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;
                case LEFTTOPTEXT:
                    canvas.translate(
                            (itemWidth - myTextWidth) / 2 - offsetX - mTagAdapter.getMarginRight(position),
                            mTagAdapter.getMarginTop(position));
                    break;
                case LEFTCENTERTEXT:
                    canvas.translate(
                            (itemWidth - myTextWidth) / 2 - offsetX - mTagAdapter.getMarginRight(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;
                case LEFTBOTTOMTEXT:
                    canvas.translate(
                            (itemWidth - myTextWidth) / 2 - offsetX - mTagAdapter.getMarginRight(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;
                case RIGHTTOPTEXT:
                    canvas.translate((itemWidth + myTextWidth) / 2 + mTagAdapter.getMarginLeft(position),
                            mTagAdapter.getMarginTop(position));
                    break;
                case RIGHTCENTERTEXT:
                    canvas.translate((itemWidth + myTextWidth) / 2 + mTagAdapter.getMarginLeft(position),
                            (getHeight() - underLineHeight - offsetY) / 2);
                    break;

                case RIGHTBOTTOMTEXT:
                    canvas.translate((itemWidth + myTextWidth) / 2 + mTagAdapter.getMarginLeft(position),
                            getHeight() - offsetY - underLineHeight - mTagAdapter.getMarginBottom(position));
                    break;

                }
                mTextPaint.setTextSize(mTagAdapter.getTextSize(position));
                if (drawable != null) {
                    drawable.setBounds(x, y, (int) (x + offsetX), (int) (y + offsetY));
                    drawable.draw(canvas);
                }
                canvas.drawText(tag, x + offsetTextX + mTagAdapter.getPaddingLeft(position),
                        y + offsetY - textTop - offsetTextY - mTagAdapter.getPaddingTop(position), mTextPaint);
            }
            canvas.restore();
        }
        canvas.restore();
    }
}

From source file:com.waz.zclient.views.images.CircularSeekBar.java

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

    canvas.translate(this.getWidth() / 2f, this.getHeight() / 2f);

    canvas.drawPath(circlePath, circlePaint);
    canvas.drawPath(circleProgressPath, circleProgressGlowPaint);
    canvas.drawPath(circleProgressPath, circleProgressPaint);

    canvas.drawPath(circlePath, circleFillPaint);

    if (showPointer) {
        canvas.drawCircle(pointerPositionXY[0], pointerPositionXY[1], pointerRadius + pointerHaloWidth,
                pointerHaloPaint);//from  w  w  w .  j  a  va2s .  com
        canvas.drawCircle(pointerPositionXY[0], pointerPositionXY[1], pointerRadius, pointerPaint);
        if (userIsMovingPointer) {
            canvas.drawCircle(pointerPositionXY[0], pointerPositionXY[1],
                    pointerRadius + pointerHaloWidth + (pointerHaloBorderWidth / 2f), pointerHaloBorderPaint);
        }
    }
}

From source file:com.dongdong.wheel.WheelView.java

/**
 * Draws value and mLabel layout/* w w  w . j av  a  2s. com*/
 *
 * @param canvas the canvas for drawing
 */
private void drawValue(Canvas canvas) {
    mValuePaint.setColor(VALUE_TEXT_COLOR);
    mValuePaint.drawableState = getDrawableState();

    Rect bounds = new Rect();
    mItemsLayout.getLineBounds(mVisibleItems / 2, bounds);

    // draw mLabel
    if (mLabelLayout != null) {
        canvas.save();
        canvas.translate(mItemsLayout.getWidth() + LABEL_OFFSET, bounds.top);
        mLabelLayout.draw(canvas);
        canvas.restore();
    }

    // draw current value
    if (mValueLayout != null) {
        canvas.save();
        canvas.translate(0, bounds.top + mScrollingOffset);
        mValueLayout.draw(canvas);
        canvas.restore();
    }
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.SimpleMonthView.java

@Override
protected void onDraw(Canvas canvas) {
    if (Config.DEBUG) {
        Log.i(TAG, "onDraw(Canvas)");
    }//from  w  w w.ja v a2 s  . c o m

    final int paddingLeft = getPaddingLeft();
    final int paddingTop = getPaddingTop();
    canvas.translate(paddingLeft, paddingTop);

    drawMonth(canvas);
    drawDaysOfWeek(canvas);
    drawDays(canvas);

    canvas.translate(-paddingLeft, -paddingTop);
}