Android Open Source - AndroidGraph Graph Draw Background






From Project

Back to project page AndroidGraph.

License

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.

Java Source Code

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);

    }
}




Java Source Code List

Nimble.com.androidgraph.ApplicationTest.java
com.nimble.android_graph.Activities.FragmentList.java
com.nimble.android_graph.Activities.GraphActivity.java
com.nimble.android_graph.Activities.GraphFragment.java
com.nimble.android_graph.Activities.GroupsFragment.java
com.nimble.android_graph.Graph.CurveGraph.java
com.nimble.android_graph.Graph.LineGraph.java
com.nimble.android_graph.Graph.Line.java
com.nimble.android_graph.Graph.ScatterGraph.java
com.nimble.android_graph.Graph.SineWave.java
com.nimble.android_graph.Graph_Base.Coord2d.java
com.nimble.android_graph.Graph_Base.GraphDataSettings.java
com.nimble.android_graph.Graph_Base.GraphDrawBackground.java
com.nimble.android_graph.Graph_Base.GraphRelateToScreen.java
com.nimble.android_graph.Graph_Base.GraphThreading.java
com.nimble.android_graph.Graph_Base.GraphTouchEvent.java
com.nimble.android_graph.Graph_Base.GraphViewDimensions.java
com.nimble.android_graph.Graph_Base.GraphView.java
com.nimble.android_graph.Graph_Base.Point.java
com.nimble.android_graph.Graph_Base.Range2d.java
com.nimble.android_graph.Graph.sine_scatter.java
com.nimble.android_graph.generics.Methods.java
com.nimble.android_graph.generics.ViewManipulators.java
com.nimble.android_graph.generics.testingSuite.java