Table of Contents
Correct answer
Home Java Can I use the Canvas element in other functions to display on the view

Can I use the Canvas element in other functions to display on the view

Feb 05, 2024 pm 10:27 PM

Question content

I created a layout that contains some views and also contains a linearlayout for drawing graphics

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>
Copy after login

I'm trying to draw a graph on it using a custom class called 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);
    }

........
Copy after login

This is the activity I want 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);

}

           ...........
...................
Copy after login

But I can't see any lines or axes.

I want to draw a graphic for a product using canvas but I can't see any results, what's wrong with my class or code


Correct answer


will draw the behavior Put it in the ondraw() function

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

The above is the detailed content of Can I use the Canvas element in other functions to display on the view. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)