Code example for Canvas

Methods: drawRGB, drawRect, restore, save

0
        } 
 
        @Override 
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawRGB(255, 255, 255);
 
            // Gradients 
            canvas.save();
            float top = 40.0f;
            float right = 40.0f + mDrawWidth;
            float left = 40.0f;
            float bottom = 40.0f + mDrawHeight;
 
            mPaint.setShader(mGradient);
 
            mMatrix.setScale(1, mDrawWidth);
            mMatrix.postRotate(90);
            mMatrix.postTranslate(right, top);
            mGradient.setLocalMatrix(mMatrix);
            canvas.drawRect(right - mDrawWidth, top, right, top + mDrawHeight, mPaint);
 
            top += 40.0f + mDrawHeight;
            bottom += 40.0f + mDrawHeight;
 
            mMatrix.setScale(1, mDrawHeight);
            mMatrix.postTranslate(left, top);
            mGradient.setLocalMatrix(mMatrix);
            canvas.drawRect(left, top, right, top + mDrawHeight, mPaint);
 
            left += 40.0f + mDrawWidth;
            right += 40.0f + mDrawWidth;
            top -= 40.0f + mDrawHeight;
            bottom -= 40.0f + mDrawHeight;
 
            mMatrix.setScale(1, mDrawHeight);
            mMatrix.postRotate(180);
            mMatrix.postTranslate(left, bottom);
            mGradient.setLocalMatrix(mMatrix);
            canvas.drawRect(left, bottom - mDrawHeight, right, bottom, mPaint);
 
            top += 40.0f + mDrawHeight;
            bottom += 40.0f + mDrawHeight;
 
            mMatrix.setScale(1, mDrawWidth);
            mMatrix.postRotate(-90);
            mMatrix.postTranslate(left, top);
            mGradient.setLocalMatrix(mMatrix);
            canvas.drawRect(left, top, left + mDrawWidth, bottom, mPaint);
 
            right = left + mDrawWidth;
            left = 40.0f;
            top = bottom + 20.0f;
            bottom = top + 50.0f;
 
            mPaint.setShader(mGradientStops);
 
            mMatrix.setScale(1, mDrawWidth);
            mMatrix.postRotate(90);
            mMatrix.postTranslate(right, top);
            mGradientStops.setLocalMatrix(mMatrix);
            canvas.drawRect(left, top, right, bottom, mPaint);
             
            canvas.restore();
        } 
    } 
}