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:de.tud.nhd.petimo.libs.HorizontalPicker.java

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

    int saveCount = canvas.getSaveCount();
    canvas.save();/* w ww.j  a v a2s. c  om*/

    int selectedItem = this.selectedItem;

    float itemWithPadding = itemWidth + dividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * sideItems, 0);

    if (values != null) {
        for (int i = 0; i < values.length; i++) {

            if (i == selectedItem) {
                textPaint.setColor(getResources().getColor(R.color.colorAccent));
                //textPaint.setTextSize(40);
                textPaint.setFakeBoldText(true);
            } else {
                textPaint.setColor(getTextColor(i));
                //textPaint.setTextSize(textSize);
                textPaint.setFakeBoldText(false);
            }

            // get text layout
            BoringLayout layout = layouts[i];

            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;

            float lineWidth = layout.getLineWidth(0);
            if (lineWidth > itemWidth) {
                if (isRtl(values[i])) {
                    x += (lineWidth - itemWidth) / 2;
                } else {
                    x -= (lineWidth - itemWidth) / 2;
                }
            }

            if (marquee != null && i == selectedItem) {
                x += marquee.getScroll();
            }

            // translate vertically to center
            canvas.translate(-x, (canvas.getHeight() - layout.getHeight()) / 2);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = itemClipBounds;
            } else {
                clipBounds = itemClipBoundsOffset;
                clipBounds.set(itemClipBounds);
                clipBounds.offset(x, 0);
            }

            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (marquee != null && i == selectedItem && marquee.shouldDrawGhost()) {
                canvas.translate(marquee.getGhostOffset(), 0);
                layout.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, leftEdgeEffect, 270);
    drawEdgeEffect(canvas, rightEdgeEffect, 90);
}

From source file:com.android.launcher3.folder.FolderIcon.java

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

    if (mReferenceDrawable != null) {
        computePreviewDrawingParams(mReferenceDrawable);
    }//from ww w  .jav a 2 s.c o m

    if (!mBackground.drawingDelegated()) {
        mBackground.drawBackground(canvas, mBgPaint);
    }

    if (mFolder == null)
        return;
    if (mFolder.getItemCount() == 0 && !mAnimating)
        return;

    canvas.save();

    if (mPreviewLayoutRule.clipToBackground()) {
        mBackground.clipCanvas(canvas);
    }

    // The items are drawn in coordinates relative to the preview offset
    canvas.translate(mBackground.basePreviewOffsetX, mBackground.basePreviewOffsetY);

    // The first item should be drawn last (ie. on top of later items)
    for (int i = mDrawingParams.size() - 1; i >= 0; i--) {
        PreviewItemDrawingParams p = mDrawingParams.get(i);
        if (!p.hidden) {
            drawPreviewItem(canvas, p);
        }
    }
    canvas.restore();

    if (mPreviewLayoutRule.clipToBackground() && !mBackground.drawingDelegated()) {
        mBackground.drawBackgroundStroke(canvas, mBgPaint);
    }
}

From source file:com.ionesmile.variousdemo.view.HorizontalPicker.java

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

    int saveCount = canvas.getSaveCount();
    canvas.save();//from   w ww.j av a2 s.  c o  m

    int selectedItem = this.selectedItem;

    float itemWithPadding = itemWidth + dividerSize;

    // translate horizontal to center
    canvas.translate(itemWithPadding * sideItems, 0);

    if (values != null) {
        for (int i = 0; i < values.length; i++) {

            // set text color for item
            textPaint.setColor(getTextColor(i));
            textPaint.setTextSize(getTextSize(i));
            // get text layout
            BoringLayout layout = layouts[i];

            int saveCountHeight = canvas.getSaveCount();
            canvas.save();

            float x = 0;

            float lineWidth = layout.getLineWidth(0);
            if (lineWidth > itemWidth) {
                if (isRtl(values[i])) {
                    x += (lineWidth - itemWidth) / 2;
                } else {
                    x -= (lineWidth - itemWidth) / 2;
                }
            }

            if (marquee != null && i == selectedItem) {
                x += marquee.getScroll();
            }

            // translate vertically to center
            canvas.translate(-x, (canvas.getHeight() - layout.getHeight()) / 2);

            RectF clipBounds;
            if (x == 0) {
                clipBounds = itemClipBounds;
            } else {
                clipBounds = itemClipBoundsOffset;
                clipBounds.set(itemClipBounds);
                clipBounds.offset(x, 0);
            }

            canvas.clipRect(clipBounds);
            layout.draw(canvas);

            if (marquee != null && i == selectedItem && marquee.shouldDrawGhost()) {
                canvas.translate(marquee.getGhostOffset(), 0);
                layout.draw(canvas);
            }

            // restore vertical translation
            canvas.restoreToCount(saveCountHeight);

            // translate horizontal for 1 item
            canvas.translate(itemWithPadding, 0);
        }
    }

    // restore horizontal translation
    canvas.restoreToCount(saveCount);

    drawEdgeEffect(canvas, leftEdgeEffect, 270);
    drawEdgeEffect(canvas, rightEdgeEffect, 90);
}

From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java

/**
 * Draw the edge effects to the supplied Canvas.
 *
 * @param canvas The Canvas/*from  w  w w. ja  v  a  2 s.  c o  m*/
 */
private void drawEdgeEffects(@NonNull Canvas canvas) {
    boolean invalidate = false;

    if (!mEdgeEffectTop.isFinished()) {
        mEdgeEffectTop.draw(canvas);
        invalidate = true;
    }
    if (!mEdgeEffectBottom.isFinished()) {
        canvas.save();
        canvas.rotate(180, getWidth() / 2, getHeight() / 2);
        mEdgeEffectBottom.draw(canvas);
        canvas.restore();
        invalidate = true;
    }
    if (!mEdgeEffectLeft.isFinished()) {
        canvas.save();
        canvas.translate(0, getHeight());
        canvas.rotate(-90);
        mEdgeEffectLeft.draw(canvas);
        canvas.restore();
        invalidate = true;
    }
    if (!mEdgeEffectRight.isFinished()) {
        canvas.save();
        canvas.translate(getWidth(), 0);
        canvas.rotate(90, 0, 0);
        mEdgeEffectRight.draw(canvas);
        canvas.restore();
        invalidate = true;
    }

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

From source file:com.acceleratedio.pac_n_zoom.DrawSVG.java

public ArrayList<ImageView> DrawSVG(Context context, ImageView orgnlImageView, RelativeLayout rel_anm_lo) {

    paint.setColor(Color.WHITE);//from   w w w. j  a va 2 s. c  o m
    paint.setStyle(Paint.Style.FILL);
    LoadSVG.SVGData data = AnimActivity.svg_data;
    ArrayList<ImageView> imgViews = new ArrayList<ImageView>();
    imgViews.add(orgnlImageView);

    if (data.symbl != null) {

        ArrayList<String> sprt_ordr = data.svg.ordr;
        int sym_mbr = 0;
        Bitmap bitmap;
        Canvas canvas;

        // - Loop through the sprites 
        int sprt_nmbr = sprt_ordr.size();
        data.svg.initScl = new float[sprt_nmbr];
        float[] initScl = data.svg.initScl;

        for (int sprt_mbr = 1; sprt_mbr < sprt_nmbr; sprt_mbr += 1) {

            String sprt_id = sprt_ordr.get(sprt_mbr); // e.g., id="g2_0"

            if (Integer.parseInt(sprt_id.substring(sprt_id.indexOf('_') + 1)) > 0) {
                // The symbol is already drawn; replicate the view   
                String init_sprt = sprt_id.substring(0, sprt_id.indexOf('_') + 1) + '0';
                String svg_ordr = data.svg.svg_ordr;
                String cnt_str = svg_ordr.substring(0, svg_ordr.indexOf(init_sprt));
                ImageView init_vw = imgViews.get(StringUtils.countMatches(cnt_str, ","));
                Bitmap initBmp = ((BitmapDrawable) init_vw.getDrawable()).getBitmap();

                bitmap = Bitmap.createBitmap(initBmp.getWidth(), initBmp.getHeight(), initBmp.getConfig());

                canvas = new Canvas(bitmap);
                canvas.save(Canvas.MATRIX_SAVE_FLAG);
                xfrmInit crt_sprt = getInitSpriteAttrib(sprt_id);
                canvas.scale(crt_sprt.scl, crt_sprt.scl);
                initScl[sprt_mbr] = crt_sprt.scl;
                canvas.translate(0, 0);
            } else {
                // The symbol needs to be drawn; a new view   is used
                bitmap = getCreatBmp(rel_anm_lo);
                canvas = new Canvas(bitmap);
                canvas.save(Canvas.MATRIX_SAVE_FLAG);

                // - Set the init values
                xfrmInit crt_sprt = getInitSpriteAttrib(sprt_id);
                canvas.scale(crt_sprt.scl, crt_sprt.scl);
                initScl[sprt_mbr] = crt_sprt.scl;
                canvas.translate(0, 0);

                // - Draw the bitmap
                LoadSVG.symbol crt_sym = data.symbl.get(sym_mbr);
                ArrayList<LoadSVG.path> pths = crt_sym.pths;
                int pth_nmbr = pths.size();

                // Loop through the paths
                for (int pth_mbr = 0; pth_mbr < pth_nmbr; pth_mbr += 1) {

                    LoadSVG.path crt_pth = pths.get(pth_mbr);
                    final Path path = new Path();
                    final Paint paint = new Paint();
                    /* Debug
                    if (pth_mbr + 1 == pth_nmbr) {
                            
                       String log_str = "Paths: pth_mbr = " +
                          String.valueOf(pth_mbr) + "; color = " + crt_pth.clr;
                            
                       Log.d("DrawSVG", log_str);
                    }
                    */
                    paint.setColor(Color.parseColor(crt_pth.clr));
                    paint.setAntiAlias(true);
                    paint.setStyle(Paint.Style.FILL_AND_STROKE);
                    ld_pth_pnts(crt_pth.pth, path);
                    path.close();
                    path.setFillType(Path.FillType.EVEN_ODD);
                    canvas.drawPath(path, paint);
                }

                canvas.restore();
                sym_mbr += 1;
            }

            ImageView iv = new ImageView(context);
            iv.setImageBitmap(bitmap);

            RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

            iv.draw(canvas);
            iv.setLayoutParams(rlp);
            iv.setBackgroundColor(Color.TRANSPARENT);
            imgViews.add(iv);
        } // sprites
    } // if symbol

    return imgViews;
}

From source file:com.google.appinventor.components.runtime.util.NativeOpenStreetMapController.java

private Drawable rasterizeSVG(MapMarker aiMarker, SVG markerSvg) {
    SVG.Svg svg = markerSvg.getRootElement();
    final float density = view.getContext().getResources().getDisplayMetrics().density;
    float height = aiMarker.Height() <= 0 ? getBestGuessHeight(svg) : aiMarker.Height();
    float width = aiMarker.Width() <= 0 ? getBestGuessWidth(svg) : aiMarker.Width();
    float scaleH = height / getBestGuessHeight(svg);
    float scaleW = width / getBestGuessWidth(svg);
    float scale = (float) Math.sqrt(scaleH * scaleH + scaleW * scaleW);

    // update fill color of SVG <path>
    Paint fillPaint = new Paint();
    Paint strokePaint = new Paint();
    PaintUtil.changePaint(fillPaint, aiMarker.FillColor());
    PaintUtil.changePaint(strokePaint, aiMarker.StrokeColor());
    SVG.Length strokeWidth = new SVG.Length(aiMarker.StrokeWidth() / scale);
    for (SVG.SvgObject element : svg.getChildren()) {
        if (element instanceof SVG.SvgConditionalElement) {
            SVG.SvgConditionalElement path = (SVG.SvgConditionalElement) element;
            path.baseStyle.fill = new SVG.Colour(fillPaint.getColor());
            path.baseStyle.fillOpacity = fillPaint.getAlpha() / 255.0f;
            path.baseStyle.stroke = new SVG.Colour(strokePaint.getColor());
            path.baseStyle.strokeOpacity = strokePaint.getAlpha() / 255.0f;
            path.baseStyle.strokeWidth = strokeWidth;
            if (path.style != null) {
                if ((path.style.specifiedFlags & SPECIFIED_FILL) == 0) {
                    path.style.fill = new SVG.Colour(fillPaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_FILL;
                }// www .  jav  a2s  .c om
                if ((path.style.specifiedFlags & SPECIFIED_FILL_OPACITY) == 0) {
                    path.style.fillOpacity = fillPaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_FILL_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE) == 0) {
                    path.style.stroke = new SVG.Colour(strokePaint.getColor());
                    path.style.specifiedFlags |= SPECIFIED_STROKE;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_OPACITY) == 0) {
                    path.style.strokeOpacity = strokePaint.getAlpha() / 255.0f;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_OPACITY;
                }
                if ((path.style.specifiedFlags & SPECIFIED_STROKE_WIDTH) == 0) {
                    path.style.strokeWidth = strokeWidth;
                    path.style.specifiedFlags |= SPECIFIED_STROKE_WIDTH;
                }
            }
        }
    }

    // draw SVG to Picture and create a BitmapDrawable for rendering
    Picture picture = markerSvg.renderToPicture();
    Picture scaledPicture = new Picture();
    Canvas canvas = scaledPicture.beginRecording((int) ((width + 2.0f * aiMarker.StrokeWidth()) * density),
            (int) ((height + 2.0f * aiMarker.StrokeWidth()) * density));
    canvas.scale(density * scaleW, density * scaleH);
    canvas.translate(strokeWidth.floatValue(), strokeWidth.floatValue());
    picture.draw(canvas);
    scaledPicture.endRecording();
    return new PictureDrawable(scaledPicture);
}

From source file:com.example.stickablelistview.StickListView.java

/** Draws the overscroll edge glow effect on the left and right sides of the horizontal list */
private void drawEdgeGlow(Canvas canvas) {
    if (mEdgeGlowTop != null && !mEdgeGlowTop.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the left side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(-90, 0, 0);/*from   w  w  w .j  a  v a2  s .  c om*/
        canvas.translate(0, -width + getPaddingRight());

        mEdgeGlowTop.setSize(getRenderWidth(), getRenderHeight());
        if (mEdgeGlowTop.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    } else if (mEdgeGlowBottom != null && !mEdgeGlowBottom.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the right side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(90, 0, 0);
        canvas.translate(getPaddingTop(), -width);
        mEdgeGlowBottom.setSize(getRenderWidth(), getRenderHeight());
        if (mEdgeGlowBottom.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    }
}

From source file:com.simon.catkins.views.HorizontalListView.java

/** Draws the overscroll edge glow effect on the top and bottom sides of the vertical list */
private void drawEdgeGlow(Canvas canvas) {
    if (mEdgeGlowLeft != null && !mEdgeGlowLeft.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the top side.
        final int restoreCount = canvas.save();
        final int height = getHeight();

        canvas.rotate(-90, 0, 0);//from  w w w. j  a  v a 2 s. c o m
        canvas.translate(-height + getPaddingBottom(), 0);

        mEdgeGlowLeft.setSize(getRenderHeight(), getRenderWidth());
        if (mEdgeGlowLeft.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    } else if (mEdgeGlowRight != null && !mEdgeGlowRight.isFinished() && isEdgeGlowEnabled()) {
        // The Edge glow is meant to come from the top of the screen, so rotate it to draw on the bottom side.
        final int restoreCount = canvas.save();
        final int width = getWidth();

        canvas.rotate(90, 0, 0);
        canvas.translate(getPaddingTop(), -width);
        mEdgeGlowRight.setSize(getRenderHeight(), getRenderWidth());
        if (mEdgeGlowRight.draw(canvas)) {
            invalidate();
        }

        canvas.restoreToCount(restoreCount);
    }
}

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

private void drawBackground(Canvas canvas) {
    if (mViewBackground == null)
        return;// w  ww . java2  s . c  o m

    if (mViewBackground instanceof KenBurnsDrawable && !((KenBurnsDrawable) mViewBackground).isAnimating()) {
        mViewBackground.setBounds(0, 0, getWidth(), getHeight());
        ((KenBurnsDrawable) mViewBackground).animate();
        mKenBurnsInitialized = true;
    }
    if (mDrawMatrix == null && !(mViewBackground instanceof KenBurnsDrawable)) {
        calculateBackgroundBounds();
    }
    int saveCount = canvas.getSaveCount();
    canvas.save();
    float translation = mParallaxForBackground ? getTranslationY() / 2 : 0;
    canvas.translate(getScrollX(), getScrollY() - translation);
    if (mDrawMatrix != null)
        canvas.concat(mDrawMatrix);

    float fraction = Math.abs(getTranslationY() / getAllowedVerticalScrollLength());
    int color = (Integer) mColorEvaluator.evaluate(fraction, mOverlayColorExpanded, mOverlayColorCollapsed);
    mViewBackground.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    mViewBackground.draw(canvas);
    canvas.restoreToCount(saveCount);
}

From source file:org.connectbot.TerminalView.java

@Override
public void onDraw(Canvas canvas) {
    if (bridge.bitmap != null) {
        // draw the bitmap
        bridge.onDraw();/*from w  w w  .j a  v  a 2s.c  o m*/

        // draw the bridge bitmap if it exists
        canvas.drawBitmap(bridge.bitmap, 0, 0, paint);

        // also draw cursor if visible
        if (bridge.buffer.isCursorVisible()) {
            int cursorColumn = bridge.buffer.getCursorColumn();
            final int cursorRow = bridge.buffer.getCursorRow();

            final int columns = bridge.buffer.getColumns();

            if (cursorColumn == columns)
                cursorColumn = columns - 1;

            if (cursorColumn < 0 || cursorRow < 0)
                return;

            int currentAttribute = bridge.buffer.getAttributes(cursorColumn, cursorRow);
            boolean onWideCharacter = (currentAttribute & VDUBuffer.FULLWIDTH) != 0;

            int x = cursorColumn * bridge.charWidth;
            int y = (bridge.buffer.getCursorRow() + bridge.buffer.screenBase - bridge.buffer.windowBase)
                    * bridge.charHeight;

            // Save the current clip and translation
            canvas.save();

            canvas.translate(x, y);
            canvas.clipRect(0, 0, bridge.charWidth * (onWideCharacter ? 2 : 1), bridge.charHeight);
            canvas.drawPaint(cursorPaint);

            final int deadKey = bridge.getKeyHandler().getDeadKey();
            if (deadKey != 0) {
                canvas.drawText(new char[] { (char) deadKey }, 0, 1, 0, 0, cursorStrokePaint);
            }

            // Make sure we scale our decorations to the correct size.
            canvas.concat(scaleMatrix);

            int metaState = bridge.getKeyHandler().getMetaState();

            if ((metaState & TerminalKeyListener.OUR_SHIFT_ON) != 0)
                canvas.drawPath(shiftCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_SHIFT_LOCK) != 0)
                canvas.drawPath(shiftCursor, cursorPaint);

            if ((metaState & TerminalKeyListener.OUR_ALT_ON) != 0)
                canvas.drawPath(altCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_ALT_LOCK) != 0)
                canvas.drawPath(altCursor, cursorPaint);

            if ((metaState & TerminalKeyListener.OUR_CTRL_ON) != 0)
                canvas.drawPath(ctrlCursor, cursorStrokePaint);
            else if ((metaState & TerminalKeyListener.OUR_CTRL_LOCK) != 0)
                canvas.drawPath(ctrlCursor, cursorPaint);

            // Restore previous clip region
            canvas.restore();
        }

        // draw any highlighted area
        if (bridge.isSelectingForCopy()) {
            SelectionArea area = bridge.getSelectionArea();
            canvas.save(Canvas.CLIP_SAVE_FLAG);
            canvas.clipRect(area.getLeft() * bridge.charWidth, area.getTop() * bridge.charHeight,
                    (area.getRight() + 1) * bridge.charWidth, (area.getBottom() + 1) * bridge.charHeight);
            canvas.drawPaint(cursorPaint);
            canvas.restore();
        }
    }
}