跳转到主内容
新派编程网 - 新潮编程理念,全新技术课堂!

我可以在其他功能中使用 Canvas 元素来显示在视图上吗

问题内容 我创建了一个布局,其中包含一些视图,还包含一个用于绘制图形的 linearlayout main_page_layout.xml
.... ...
我正在尝试使用名为 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 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(); }

相关文章