Back to project page AndroidGraph.
The source code is released under:
MIT License
If you think the Android project AndroidGraph listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.nimble.android_graph.Graph_Base; /*from w w w. j av a2 s . com*/ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; /** * Created by Mike on 25/07/2014. */ public abstract class GraphDrawBackground extends GraphThreading { protected Bitmap currentBackground; GraphDrawBackground(Context context) { super(context); } public GraphDrawBackground(Context context, AttributeSet attr) { super(context, attr); } @Override public void onDraw(Canvas canvas) { if(dimenChange) updateBackgroundBitmap(); currentBackground.prepareToDraw(); canvas.drawBitmap(currentBackground, 0, 0, null); } protected void drawBackground(Canvas canvas) { if (backgroundColour != -1) { paint.setColor(backgroundColour); canvas.drawRect(margin.x, margin.y, graphWidth.y, graphHeight.y, paint); } } protected void drawAxis(Canvas canvas) { paint.setColor(axisColour); // y - axis canvas.drawLine(graphWidth.x, margin.y, graphWidth.x, graphHeight.y, paint); // x - axis canvas.drawLine(graphWidth.x, graphHeight.y, graphWidth.y, graphHeight.y, paint); } protected void drawLabels(Canvas canvas) { if (!useLabels) return; paint.setColor(labelColour); paint.setTextAlign(Paint.Align.CENTER); canvas.drawText(axisLabels.x, graphWidth.length / 2, graphHeight.y + 2*graphHeight.x, paint); //Bitta magic numbers here... paint.setTextAlign(Paint.Align.LEFT); canvas.drawText(axisLabels.y, margin.x, graphHeight.y / 2, paint); } protected void drawGrid(Canvas canvas) { if (!useGrid) return; paint.setColor(gridColour); float lim = graphWidth.y + delta; for (float x = graphWidth.x; x <= lim; x += gridSpacing.x) //vertical canvas.drawLine(x, margin.y, x, graphHeight.y, paint); lim = graphHeight.y + delta; for (float y = margin.y; y <= lim; y += gridSpacing.y) //horizontal canvas.drawLine(graphWidth.x, y, graphWidth.y, y, paint); } protected void updateBackgroundBitmap() { Log.e("Updating Bitmap", "Ok..."); currentBackground = Bitmap.createBitmap( (int) ( graphWidth.length + margin.y ), (int) ( graphHeight.y + 2*graphHeight.x + graphHeight.length), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(currentBackground); drawBackground(canvas); drawLabels(canvas); //label must come before axis. drawAxis(canvas); drawGrid(canvas); } }