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.am.pagergradienttab.view.PagerGradientTabStrip.java

/**
 * /*w w  w . j  a  va  2  s .  c o  m*/
 * 
 * @param canvas
 */
private void drawIndicator(Canvas canvas) {

    canvas.save();
    canvas.translate(
            getPaddingLeft() + currectPager * (itemWidth + intervalWidth) + indicatorPadding
                    + indicatorOffset * (itemWidth + intervalWidth),
            getHeight() - underLineHeight - getPaddingBottom() - indicatorHeight);
    if (showIndicator) {
        mTextPaint.setColor(indicatorColor);
        if (tabs.size() > 1)
            canvas.drawRect(0, 0, itemWidth - 2 * indicatorPadding, indicatorHeight, mTextPaint);
        else if (tabs.size() > 0)
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 2 * indicatorPadding, indicatorHeight,
                    mTextPaint);
        else
            canvas.drawRect(itemWidth / 4, 0, itemWidth - itemWidth / 4 - 1 * indicatorPadding, indicatorHeight,
                    mTextPaint);
    }
    canvas.restore();
}

From source file:com.achillesrasquinha.biblegenerator.ImageGenerator.java

public Bitmap getBitmap(String title, String subtitle, String text) {
    Bitmap bitmap = Bitmap.createBitmap(mImageSize.x, mImageSize.y, Bitmap.Config.ARGB_8888);
    //convert bitmap to card view background color
    bitmap.eraseColor(ContextCompat.getColor(mContext, R.color.cardview_light_background));

    Canvas canvas = new Canvas(bitmap);

    //title//from w w  w  .j  a  v a  2  s . co  m
    mTextPaint.setTextSize(mSizeTitle);
    mTextPaint.setColor(mColor1);
    canvas.drawText(title, mX, mYTitle, mTextPaint);

    //subtitle
    mTextPaint.setTextSize(mSizeSubtitle);
    mTextPaint.setColor(mColor2);
    canvas.drawText(subtitle, mX, mYSubtitle, mTextPaint);

    //since text needs to be wrapped, using a StaticLayout
    mTextPaint.setColor(mColor3);
    //text size same as subtitle size
    StaticLayout layout = new StaticLayout(text, mTextPaint, mImageSize.x - 2 * mX,
            Layout.Alignment.ALIGN_NORMAL, 1.0f, mLineSpacing, false);
    //16dp padding as per material design guidelines
    canvas.translate(mX, mYSubtitle + mX);
    layout.draw(canvas);

    return bitmap;
}

From source file:ezy.ui.view.ViewPagerIndicator.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int flags = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
            | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
    int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, flags);

    int wg = mItemWidth + mItemGap;
    int x = (getWidth() - mWidth) / 2;
    int y = (getHeight() - mItemHeight) / 2;

    mItemDrawable.setBounds(0, 0, mItemWidth, mItemHeight);
    mItemDrawableSelected.setBounds(0, 0, mItemWidth, mItemHeight);

    for (int i = 0; i < mItemCount; i++) {
        canvas.save();//from   ww w .  j a  v a2  s .  c o  m
        canvas.translate(x + i * wg, y);
        mItemDrawable.draw(canvas);
        canvas.restore();
    }

    canvas.save();
    canvas.translate(x + (mPosition + mPositionOffset) * wg, y);
    mItemDrawableSelected.draw(canvas);
    canvas.restore();

    canvas.restoreToCount(sc);
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawTitle(@NonNull Canvas canvas, @NonNull String title, int top, boolean isArticleRTL) {
    final int marginBottom = 0;
    final int maxHeight = 70;
    final int maxLines = 2;
    final float maxFontSize = 30.0f;
    final float spacingMultiplier = 0.7f;

    TextPaint textPaint = new TextPaint();
    textPaint.setAntiAlias(true);// w  ww .  j a va2 s .  c om
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(maxFontSize);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setTypeface(SERIF);
    textPaint.setShadowLayer(1.0f, 0.0f, 1.0f, Color.GRAY);

    StaticLayout textLayout = optimizeTextSize(
            new TextLayoutParams(title, textPaint, DESCRIPTION_WIDTH, spacingMultiplier), maxHeight, maxLines,
            maxFontSize, maxFontSize);
    int left = HORIZONTAL_PADDING;
    if (isArticleRTL) {
        left = WIDTH - HORIZONTAL_PADDING - textLayout.getWidth();
    }
    int marginBottomTotal = marginBottom;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        // versions < 5.0 don't compensate for bottom margin correctly when line
        // spacing is less than 1.0, so we'll compensate ourselves
        final int marginBoost = 10;
        marginBottomTotal += marginBoost;
    }

    top = top - marginBottomTotal - textLayout.getHeight();
    canvas.save();
    canvas.translate(left, top);
    textLayout.draw(canvas);
    canvas.restore();
}

From source file:vc908.stickerfactory.ui.advancedrecyclerview.swipeable.RemovingItemDecorator.java

private void fillSwipingItemBackground(Canvas c, Drawable drawable, int height) {
    final Rect bounds = mSwipingItemBounds;
    final int translationX = mInitialTranslationX;
    final int translationY = mTranslationY;

    if (height < 0) {
        height = bounds.height();/*from   w ww . java  2s . co  m*/
    }

    if ((height == 0) || (drawable == null)) {
        return;
    }

    final int savedCount = c.save();

    c.clipRect(bounds.left + translationX, bounds.top + translationY, bounds.right + translationX,
            bounds.top + translationY + height);

    // c.drawColor(0xffff0000); // <-- debug

    c.translate(bounds.left + translationX, bounds.top + translationY - (bounds.height() - height) / 2);
    drawable.setBounds(0, 0, bounds.width(), bounds.height());

    drawable.draw(c);

    c.restoreToCount(savedCount);
}

From source file:com.achep.acdisplay.ui.widgets.CircleView.java

private void drawCornerIcon(@NonNull Canvas canvas, @NonNull Drawable drawable, int xm, int ym) {
    int width = getMeasuredWidth() - drawable.getBounds().width();
    int height = getMeasuredHeight() - drawable.getBounds().height();
    float margin = (1 - 2 * xm) * mCornerMargin;
    // Draw// ww  w  . java2  s  .c  om
    canvas.save();
    canvas.translate(xm * width + margin, ym * height + margin);
    drawable.draw(canvas);
    canvas.restore();
}

From source file:com.github.jdsjlzx.ItemDecoration.StickyHeaderDecoration.java

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

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

        final int adapterPos = parent.getChildAdapterPosition(child);

        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:com.hgdendi.contactslist.common.FloatingBarItemDecoration.java

@Override
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDrawOver(c, parent, state);
    final int position = ((LinearLayoutManager) parent.getLayoutManager()).findFirstVisibleItemPosition();
    if (position == RecyclerView.NO_POSITION) {
        return;// w  ww  .ja va2 s .c om
    }
    View child = parent.findViewHolderForAdapterPosition(position).itemView;
    String initial = getTag(position);
    if (initial == null) {
        return;
    }

    boolean flag = false;
    if (getTag(position + 1) != null && !initial.equals(getTag(position + 1))) {
        if (child.getHeight() + child.getTop() < mTitleHeight) {
            c.save();
            flag = true;
            c.translate(0, child.getHeight() + child.getTop() - mTitleHeight);
        }
    }

    c.drawRect(parent.getPaddingLeft(), parent.getPaddingTop(), parent.getRight() - parent.getPaddingRight(),
            parent.getPaddingTop() + mTitleHeight, mBackgroundPaint);
    c.drawText(initial, child.getPaddingLeft() + mTextStartMargin,
            parent.getPaddingTop() + mTitleHeight - (mTitleHeight - mTextHeight) / 2 - mTextBaselineOffset,
            mTextPaint);

    if (flag) {
        c.restore();
    }
}

From source file:io.plaidapp.core.ui.transitions.ReflowText.java

private Bitmap createBitmap(@NonNull ReflowData data, @NonNull Layout layout) {
    Bitmap bitmap = Bitmap.createBitmap(data.bounds.width(), data.bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.translate(data.textPosition.x, data.textPosition.y);
    layout.draw(canvas);/*  ww w  .  j  av  a2s.c o  m*/
    return bitmap;
}

From source file:com.jungle.toolbaractivity.layout.HorizontalSwipeBackLayout.java

@Override
protected void dispatchDraw(Canvas canvas) {
    if (!mSwipeBackEnable) {
        super.dispatchDraw(canvas);
        return;//w w w .java  2 s. c  o  m
    }

    final int width = getMeasuredWidth();
    final int height = getMeasuredHeight();

    if (mBkgDrawable != null) {
        mBkgDrawable.setBounds((int) (mTranslationX + 0.5f), 0, width, height);
        mBkgDrawable.draw(canvas);
    } else {
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        canvas.drawRect(mTranslationX, 0, width, height, paint);
    }

    if (mTranslationX > 0) {
        canvas.save();
        canvas.translate(mTranslationX - mShadowWidth, 0);
        mEdgeShadowDrawable.setBounds(0, 0, mShadowWidth, height);
        mEdgeShadowDrawable.draw(canvas);
        canvas.restore();
    }

    super.dispatchDraw(canvas);
}