Inhaltsverzeichnis
Richtige Antwort
Heim Java Kann ich das Canvas-Element in anderen Funktionen verwenden, um es in der Ansicht anzuzeigen?

Kann ich das Canvas-Element in anderen Funktionen verwenden, um es in der Ansicht anzuzeigen?

Feb 05, 2024 pm 10:27 PM

Frageninhalt

Ich habe ein Layout erstellt, das einige Ansichten und auch ein lineares Layout zum Zeichnen von Grafiken enthält

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>
Nach dem Login kopieren

Ich versuche, mit einer benutzerdefinierten Klasse namens graphplotter ein Diagramm darauf zu zeichnen

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

........
Nach dem Login kopieren

Das ist die Aktivität, die ich zeichnen möchte

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

}

           ...........
...................
Nach dem Login kopieren

Aber ich kann keine Linie oder Achse sehen.

Ich möchte eine Grafik für ein Produkt auf Leinwand zeichnen, kann aber keine Ergebnisse sehen. Was stimmt mit meiner Klasse oder meinem Code nicht?


Richtige Antwort


Fügen Sie das Zeichenverhalten in die Funktion ondraw() ein

@Override
protected void onDraw(Canvas canvas) {
    drawLine(...);
    drawAxisX();
}
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonKann ich das Canvas-Element in anderen Funktionen verwenden, um es in der Ansicht anzuzeigen?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)