Code example for Canvas

Methods: drawCircle, drawRoundRect, restore, save, translate

0
     * @param canvas 
     *            the canvas on which to draw the point at position. 
     */ 
    protected void drawText( int position, Canvas canvas ) {
        Point point = drawViewAdapter.getTextPointAt( position );
        canvas.save();
        canvas.translate( point.x, point.y );
        Layout layout = drawViewAdapter.getTextLayoutAt( position );
        doDrawUnderTextPaint( canvas, layout );
        layout.draw( canvas );
        canvas.restore();
    } 
 
    /** 
     * Draw some surface under text. This method is called just before drawing each {@link LabeledPoint}'s text. 
     *  
     * @param canvas 
     *            the canvas in which we draw. 
     * @param layout 
     *            the {@link Layout} associated to a {@link LabeledPoint}. 
     */ 
    protected void doDrawUnderTextPaint( Canvas canvas, Layout layout ) {
        if ( underTextPaint != null ) {
            int margin = DRAW_UNDER_TEXT_CORNER_RADIUS;
            RectF rect = new RectF( -margin, -margin, layout.getWidth() + margin * 2, layout.getHeight() + margin * 2 );
            canvas.drawRoundRect( rect, 2 * margin, 2 * margin, underTextPaint );
        } 
    } 
 
    /** 
     * Draw the drawable of the point at a given position specified by {@link DrawViewAdapter#getDrawablePoint(int)} 
     *  
     * @param position 
     *            the index of the point to draw. 
     * @param canvas 
     *            the canvas on which to draw the point at position. 
     */ 
    protected void drawDrawable( int position, Canvas canvas ) {
        Drawable drawable = drawViewAdapter.getDrawableAt( position );
        if ( drawable == null ) {
            return; 
        } 
        doUseClearPorterDuffXfermode( canvas, drawable );
        drawable.draw( canvas );
        /* 
         * Shall we add some debug mode ? Paint paint = new Paint(); paint.setColor( 
         * getContext().getResources().getColor( android.R.color.holo_orange_dark ) ); canvas.drawCircle( 
         * drawable.getBounds().exactCenterX(), drawable.getBounds().exactCenterY(), 5, paint ); 
         */ 
    } 
 
    /** 
     * if PorterDuff xfermode is active, this method can be used to remove the background inside the {@link Drawable} 
     * associated to a {@link LabeledPoint}. This method is called just before drawing each {@link LabeledPoint}'s 
     * drawable. 
     *  
     * @param canvas 
     *            the canvas on which to draw the point at position. 
     * @param drawable 
     *            the {@link Drawable} that is going to be drawn. 
     * @see #isClearPorterDuffXfermodeEnabled 
     * @see #setIsClearPorterDuffXfermodeEnabled(boolean) 
     */ 
    protected void doUseClearPorterDuffXfermode( Canvas canvas, Drawable drawable ) {
        if ( isClearPorterDuffXfermodeEnabled ) {
            Paint p = new Paint();
            p.setXfermode( new PorterDuffXfermode( Mode.CLEAR ) );
            int cx = drawable.getBounds().centerX();
            int cy = drawable.getBounds().centerY();
            int radius = drawable.getIntrinsicWidth() / 2 - 3;
            canvas.drawCircle( cx, cy, radius, p );
        } 
    } 
 
    /** 
     * Animate the point to draw on screen.