首頁 > Java > 主體

我可以在其他功能中使用 Canvas 元素來顯示在視圖上嗎

王林
發布: 2024-02-05 22:27:06
轉載
917 人瀏覽過
問題內容

我建立了一個佈局,其中包含一些視圖,也包含一個用於繪製圖形的 linearlayout

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>
登入後複製

我正在嘗試使用名為 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);
    }

........
登入後複製

這是我要畫的活動

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

}

           ...........
...................
登入後複製

但我看不到任何線或軸。

我想使用畫布為產品繪製圖形,但看不到任何結果,我的類別或程式碼有什麼問題


正確答案


##將繪製行為放入ondraw() 函數中

@Override
protected void onDraw(Canvas canvas) {
    drawLine(...);
    drawAxisX();
}
登入後複製

以上是我可以在其他功能中使用 Canvas 元素來顯示在視圖上嗎的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!