Can I use Canvas element in other functions to show on view

24 views Asked by At

I have created a layout includes some view within, also it includes a LinearLayout to draw a graphic

main_page_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bgcolor"
    android:id="@+id/main_page_layout"
    >

....

    <LinearLayout
        android:id="@+id/graphicView"
        android:layout_width="match_parent"
        android:layout_height="196dp"
        android:layout_marginTop="64dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:background="@color/white"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/menuLayout" />

...

</androidx.constraintlayout.widget.ConstraintLayout>

And I'm trying to draw graphic on it with my custom class named GraphPlotter

GraphPlotter.java


public class GraphPlotter extends View {
    private Paint paint = null;
    private Bitmap bitmap;
    private Canvas canvas;

 public GraphPlotter(Context context) {
        super(context);
        this.init();
    }

    /* Initiates graph plotter */
    private void init()
    {
        this.paint = new Paint();
        this.paint.setColor(Color.BLUE);
        this.paint.setStrokeWidth(5);
        this.paint.setStyle(Paint.Style.FILL_AND_STROKE);

        // Create a bitmap and a canvas associated with the bitmap
        this.bitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
        this.canvas = new Canvas(bitmap);

    }
    public void setColor(int color) {
        this.paint.setColor(color);
        invalidate(); // Force a redraw
    }
    /* Draws a line with a custom color */
    public void drawLine(float starX, float startY, float stopX, float stopY,int color)
    {
        this.setColor(color);
        this.canvas.drawLine(starX, startY, stopX, stopY, this.paint);
    }

........

And here's my activity to draw

MainPageActivity.java

public class MainPageActivity extends AppCompatActivity {
        private ConnectionHelper connectionHelper = null;
        private SQLQuery sqlQuery = null;
        private Person person = null;
        private TextView txtID = null;
        private RecyclerView recyclerView = null;
        private final ProductAdapter productAdapter = null;
        private AsyncTaskRunner asyncTaskRunner = null;
        private ViewHolder viewHolder = null;
        private Spinner graphicTypeSpinner = null;
        private Button menuButton = null;
        private ConstraintLayout menuView  = null;
        private List<Product> productList  = null;
        private GraphPlotter graphPlotter = null;



        @Override
        protected void onCreate(Bundle savedInstanceState){

            super.onCreate(savedInstanceState); // necessary for onCreate function
            setContentView(R.layout.main_page_layout); // R = resource , R.layout => layouts directory

            init();


            LinearLayout graphicView = (LinearLayout) findViewById(R.id.graphicView);
            graphPlotter = new GraphPlotter(this);

            graphPlotter.drawAxisX(159,159,259, Color.BLUE);

            graphicView.addView(graphPlotter);

}

           ...........
...................

But I can not see any line or axis.

I want to draw a graphic for products using canvas but I can not see any result, what's wrong in my classes or code

1

There are 1 answers

2
NNA On

Put your draw behavior inside onDraw() function

@Override
protected void onDraw(Canvas canvas) {
    drawLine(...);
    drawAxisX();
}