Activity中界面输入参数,绘制直线、圆形、矩形等
发布日期:2022-02-27 02:37:49 浏览次数:53 分类:技术文章

本文共 9277 字,大约阅读时间需要 30 分钟。

通过在Activity设计好的界面中输入参数,可以绘制出任意状态的直线、圆形、矩形等

布局文件:

实现效果如下:(后面的界面效果,均是在连接华为Nova4真机下操作的)

在这里插入图片描述

【输入以上参数可以绘制出下面的效果图:】

直线:

在这里插入图片描述

圆形:

在这里插入图片描述

矩形:

在这里插入图片描述

期间,实现上面的程序也耗费了不短的时间(刚刚入门不久,基础只是不牢固,每修改一点,总会出现难以排查的错误),后来反复查找与百度,终于找到了破绽!下面分享实现的过程以及完整的代码!请读者耐心阅读哦~

public class MainActivity extends AppCompatActivity {
// 7th Button rect, circle, line; TextView x0, y0, x1, y1; TextView px, py, radius; TextView lx1, lx2, ly1, ly2; @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 7th setContentView(R.layout.draw_graphic); init7(); } public void init7() {
rect = findViewById(R.id.rect); circle = findViewById(R.id.circle); line = findViewById(R.id.line); // 矩形 x0 = findViewById(R.id.x0); y0 = findViewById(R.id.y0); x1 = findViewById(R.id.x1); y1 = findViewById(R.id.y1); // 圆形 px = findViewById(R.id.px); py = findViewById(R.id.py); radius = findViewById(R.id.radius); // 直线 lx1 = findViewById(R.id.lx1); ly1 = findViewById(R.id.ly1); lx2 = findViewById(R.id.lx2); ly2 = findViewById(R.id.ly2); } // 点击事件处理 // 每次获取控件中的参数之前,一定要排除空数据的情况,这个对于后面进行页面跳转时, // 传递空数据会报错(空指针异常)对于刚入门的我来讲,是难以发现的,同时也是致命的 public void onClick_7(View view) {
switch (view.getId()) {
case R.id.rect: if (!isEmpty(x0) && !isEmpty(y0) && !isEmpty(x1) && !isEmpty(y1)) {
String x_0 = x0.getText().toString().trim(); String x_1 = x1.getText().toString().trim(); String y_0 = y0.getText().toString().trim(); String y_1 = y1.getText().toString().trim(); // 跳转页面draw_RCL.java Intent intent = new Intent(MainActivity.this, draw_RCL.class); Bundle bundle = new Bundle(); // 每次绘制图形时,都需要传递该图形的名称(当然也可以传递其他具有标记的参数),为识别后面不同的构造函数做准备 bundle.putString("which", rect.getText().toString().trim()); bundle.putString("x0", x_0); bundle.putString("y0", y_0); bundle.putString("x1", x_1); bundle.putString("y1", y_1); intent.putExtras(bundle); startActivity(intent); // 实现跳转 break; } else {
Toast.makeText(MainActivity.this, "请输入完整数据!", Toast.LENGTH_SHORT).show(); break; } case R.id.circle: if (!isEmpty(px) && !isEmpty(py) && !isEmpty(radius)) {
String p_x = px.getText().toString(); String p_y = py.getText().toString(); String p_radius = radius.getText().toString(); Intent intent = new Intent(MainActivity.this, draw_RCL.class); Bundle bundle = new Bundle(); bundle.putString("which", circle.getText().toString().trim()); bundle.putString("px", p_x); bundle.putString("py", p_y); bundle.putString("radius", p_radius); intent.putExtras(bundle); startActivity(intent); break; } else {
Toast.makeText(MainActivity.this, "请输入完整数据!", Toast.LENGTH_SHORT).show(); break; } case R.id.line: if (!isEmpty(lx1) && !isEmpty(ly1) && !isEmpty(lx2) && !isEmpty(ly2)) {
String lx_1 = lx1.getText().toString().trim(); String lx_2 = lx2.getText().toString().trim(); String ly_1 = ly1.getText().toString().trim(); String ly_2 = ly2.getText().toString().trim(); Intent intent = new Intent(MainActivity.this, draw_RCL.class); Bundle bundle = new Bundle(); bundle.putString("which", line.getText().toString().trim()); bundle.putString("lx1", lx_1); bundle.putString("ly1", ly_1); bundle.putString("lx2", lx_2); bundle.putString("ly2", ly_2); intent.putExtras(bundle); startActivity(intent); break; } else {
Toast.makeText(MainActivity.this, "请输入完整数据!", Toast.LENGTH_SHORT).show(); break; } default: break; } } // 判断文本框是否有数据? public boolean isEmpty(TextView textView) {
if (textView.getText().toString().equals("")) return true; else return false; }

draw_RCL.java文件(处理从上一个Activity传递过来的参数,并识别、调用对应构造函数,以实现绘制出满足要求的图形)

public class draw_RCL extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 获取上一个界面的“图形”名称 Bundle bundle = this.getIntent().getExtras(); String draw_name = bundle.getString("which");// FrameLayout f = findViewById(R.id.frame); if (draw_name.equals("矩形")) {
// f.addView(new PaintRCL(draw_RCL.this, draw_name, x0, y0, x1, y1)); float x0 = Float.parseFloat(bundle.getString("x0")); float y0 = Float.parseFloat(bundle.getString("y0")); float x1 = Float.parseFloat(bundle.getString("x1")); float y1 = Float.parseFloat(bundle.getString("y1")); setContentView(new PaintRCL(this, draw_name, x0, y0, x1, y1)); // 矩形的构造函数 } // 绘制圆形 if (draw_name.equals("圆形")) {
float px = Float.parseFloat(bundle.getString("px")); float py = Float.parseFloat(bundle.getString("py")); float radius = Float.parseFloat(bundle.getString("radius"));// f.addView(new PaintRCL(this, draw_name, px, py, radius)); setContentView(new PaintRCL(this, draw_name, px, py, radius)); } // 绘制直线 if (draw_name.equals("直线")) {
// f.addView(new PaintRCL(draw_RCL.this, lx1, ly1, lx2, ly2)); float lx1 = Float.parseFloat(bundle.getString("lx1")); float ly1 = Float.parseFloat(bundle.getString("ly1")); float lx2 = Float.parseFloat(bundle.getString("lx2")); float ly2 = Float.parseFloat(bundle.getString("ly2")); setContentView(new PaintRCL(this, lx1, ly1, lx2, ly2)); } }}

PaintRCL继承View,并重写Ondraw()方法

public class PaintRCL extends View {
private float x0, y0, x1, y1; private float px, py, radius; private float lx1, lx2, ly1, ly2; private String name = ""; // 矩形的构造方法 public PaintRCL(Context context, String name, float x0, float y0, float x1, float y1) {
super(context); this.name = name; this.x0 = x0; this.y0 = y0; this.x1 = x1; this.y1 = y1; } // 圆形的构造方法 public PaintRCL(Context context, String name, float px, float py, float radius) {
super(context); this.name = name; this.px = px; this.py = py; this.radius = radius; } // 直线的构造方法 public PaintRCL(Context context, float lx1, float ly1, float lx2, float ly2) {
super(context); this.name = "直线"; this.lx1 = lx1; this.ly1 = ly1; this.lx2 = lx2; this.ly2 = ly2; } protected void onDraw(Canvas canvas) {
// 绘制矩形 if (name.equals("矩形")) {
Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(40); canvas.drawText("画矩形:", 10, 80, paint); paint.setStrokeWidth(5); paint.setStyle(Paint.Style.STROKE); canvas.drawRect(x0, y0, x1, y1, paint); } // 绘制圆形 if (name.equals("圆形")) {
Paint paint = new Paint(); paint.setColor(Color.GREEN); paint.setTextSize(50); paint.setStrokeWidth(2); paint.setStyle(Paint.Style.STROKE); canvas.drawText("画圆形:", 10, 80, paint); canvas.drawCircle(px, py, radius, paint);// canvas.drawCircle(800, 800, 100, paint); } // 绘制直线 if (name.equals("直线")) {
Paint paint = new Paint(); paint.setColor(Color.BLUE); paint.setStrokeWidth(5); paint.setStyle(Paint.Style.STROKE); Path path = new Path(); paint.setTextSize(50); canvas.drawText("画直线:", 10, 80, paint); path.moveTo(lx1, ly1); // 移动到该点,但不绘制路径// path.lineTo(lx1, ly1); path.lineTo(lx2, ly2); // 从起点一直绘制路径到该点位置 path.close(); canvas.drawPath(path, paint); } super.onDraw(canvas); }}

写博文的经验较少,在书面表述上可能存在诸多问题,希望在以后的文章里能够有所提升。上述代码的完成,少不了参照CSDN平台上其他优秀大佬的经验分享,在这里也非常开心能够分享自己的一点点经验,像优秀的大佬看齐,乐于分享自己的所得。

转载地址:https://blog.csdn.net/weixin_43376042/article/details/117299393 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【IDEA2019.3版本】AJAX请求报错404:URL设置与Servlet映射不一致所致
下一篇:IDEA配置事务管理器xml时无法添加transaction-manager属性

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月15日 04时46分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

库克:苹果收取 30% 佣金很合理! 2019-04-27
不定期总结程序员常见误区 2019-04-27
特斯拉已在中国建立数据中心 2019-04-27
阿里云:我们为全面服务政企市场做好了准备! 2019-04-27
不打好评不给用!苹果竟然把这种“流氓” App 都放出来? 2019-04-27
达摩院重要科技突破!空天数据库引擎Ganos解读 2019-04-27
Python 之父:移动设备中的 Python 应用“又大又慢”! 2019-04-27
从“嵌入式”到“物联网”有哪些变化? 2019-04-27
雷军:年轻人入职半年内不要提意见,不靠谱;微信表情新彩蛋遭疯狂吐槽:满屏“炸屎”;谷歌正式推出 Fuchsia OS... 2019-04-27
稳定性小结 2019-04-27
为什么我十分喜欢C,却很不喜欢C++ 2019-04-27
程序员必备的思维能力:抽象思维 2019-04-27
想快速体验谷歌 Fuchsia OS?FImage 项目来了! 2019-04-27
假如苹果构建了一个搜索引擎 2019-04-27
“我辞职了,决定全职去开发我的操作系统!” 2019-04-27
蚂蚁自研数据库OceanBase基于木兰公共协议正式开源 2019-04-27
“我被苹果人脸识别系统「坑」进了监狱” 2019-04-27
美团员工被指用钓鱼邮件获拼多多薪资;华为回应暂无其它手机厂商接入HarmonyOS;GCC 放弃版权转让政策... 2019-04-27
云原生是全云开发的敲门砖?蒋涛独家对话阿里云贾扬清、华先胜等大咖 2019-04-27
云上创新,与时代前行(阿里云游记) 2019-04-27