Code example for Canvas

Methods: drawOval, restore, rotate, save, scale, translate

0
			// Let go of the old background 
			mBackground.recycle();
		} 
		// Create a new background according to the new width and height 
		mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
		final Canvas canvas = new Canvas(mBackground);
		final float scale = Math.min(getWidth(), getHeight());
		canvas.scale(scale, scale);
		canvas.translate((scale == getHeight()) ? ((getWidth()-scale) /2)/scale : 0 
				,(scale == getWidth()) ? ((getHeight()-scale) /2 )/scale: 0);
 
		drawRim(canvas);
		drawFace(canvas);
 
		if (mShowRanges) {
			drawScale(canvas);
		} 
	} 
 
	@Override 
	protected void onDraw(final Canvas canvas) {
		drawBackground(canvas);
 
		final float scale = Math.min(getWidth(), getHeight());
		canvas.scale(scale, scale);
		canvas.translate((scale == getHeight()) ? ((getWidth()-scale) /2)/scale : 0 
				,(scale == getWidth()) ? ((getHeight()-scale) /2 )/scale: 0);
		 
		if (mShowNeedle) {
			drawNeedle(canvas);
		} 
 
		if (mShowText) {
			drawText(canvas);
		} 
 
		computeCurrentValue(); 
	} 
 
	private void drawBackground(final Canvas canvas) {
		if (null == mBackground) {
			Logger.log.error("Background not created."); 
		} 
		else { 
			canvas.drawBitmap(mBackground, 0, 0, mBackgroundPaint);
		} 
	} 
	private void drawRim(final Canvas canvas) {
		if(!isInEditMode()){
			if (mShowOuterShadow) {
				canvas.drawOval(mOuterShadowRect, mOuterShadowPaint);
			} 
		} 
		if (mShowOuterBorder) {
			canvas.drawOval(mOuterBorderRect, mOuterBorderPaint);
		} 
		if (mShowOuterRim) {
			canvas.drawOval(mOuterRimRect, mOuterRimPaint);
		} 
		if (mShowInnerRim) {
			canvas.drawOval(mInnerRimRect, mInnerRimPaint);
			canvas.drawOval(mInnerRimRect, mInnerRimBorderLightPaint);
			canvas.drawOval(mInnerRimBorderRect, mInnerRimBorderDarkPaint);
		} 
	} 
 
	private void drawFace(final Canvas canvas) {
		// Draw the face gradient 
		canvas.drawOval(mFaceRect, mFacePaint);
		// Draw the face border 
		canvas.drawOval(mFaceRect, mFaceBorderPaint);
		// Draw the inner face shadow 
		canvas.drawOval(mFaceRect, mFaceShadowPaint);
	} 
 
	private void drawText(final Canvas canvas) {
		final String textValue = !TextUtils.isEmpty(mTextValue) ? mTextValue : valueString(mCurrentValue);
		final float textValueWidth = mTextValuePaint.measureText(textValue);
		final float textUnitWidth = !TextUtils.isEmpty(mTextUnit) ? mTextUnitPaint.measureText(mTextUnit) : 0;
 
		final float startX = CENTER - textUnitWidth / 2;
		final float startY = CENTER + 0.1f;
 
		canvas.drawText(textValue, startX, startY, mTextValuePaint);
 
		if (!TextUtils.isEmpty(mTextUnit)) {
			canvas.drawText(mTextUnit, CENTER + textValueWidth / 2 + 0.03f, CENTER, mTextUnitPaint);
		} 
	} 
 
	private void drawScale(final Canvas canvas) {
		canvas.save(Canvas.MATRIX_SAVE_FLAG);
		// On canvas, North is 0 degrees, East is 90 degrees, South is 180 etc. 
		// We start the scale somewhere South-West so we need to first rotate the canvas. 
		canvas.rotate(mScaleRotation, 0.5f, 0.5f);
 
		final int totalTicks = mDivisions * mSubdivisions + 1;
		for (int i = 0; i < totalTicks; i++) {
			final float y1 = mScaleRect.top;
			final float y2 = y1 + 0.015f; // height of division
			final float y3 = y1 + 0.045f; // height of subdivision
 
			final float value = getValueForTick(i);
			final Paint paint = getRangePaint(value);
			if (0 == value % mDivisions) {
				// Draw a division tick 
				canvas.drawLine(0.5f, y1, 0.5f, y3, paint);
				// Draw the text 0.15 away from the division tick 
				canvas.drawText(valueString(value), 0.5f, y3 + 0.045f, paint);
			} 
			else { 
				// Draw a subdivision tick 
				canvas.drawLine(0.5f, y1, 0.5f, y2, paint);
			} 
			canvas.rotate(mSubdivisionAngle, 0.5f, 0.5f);
		} 
		canvas.restore();
	} 
 
	private String valueString(final float value) {
		return String.format("%d", (int) value);
	}