目录
我可以在其他功能中使用 Canvas 元素来显示在视图上吗
Feb 05, 2024 pm 10:27 PM问题内容
我创建了一个布局,其中包含一些视图,还包含一个用于绘制图形的 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中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章
击败分裂小说需要多长时间?
3 周前
By DDD
仓库:如何复兴队友
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前
By 尊渡假赌尊渡假赌尊渡假赌
公众号网页更新缓存难题:如何避免版本更新后旧缓存影响用户体验?
3 周前
By 王林

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)