简体   繁体   English

我想在 MainActivity.java 中绘制一个矩形,但我的代码无法在主 Activity 中绘制一个矩形

[英]I want draw a rectangle in MainActivity.java but my code can not draw a rectangle in main Activity

I want draw a rectangle in MainActivity.java but my code can not draw a rectangle and I added only the following function我想在 MainActivity.java 中绘制一个矩形,但我的代码无法绘制一个矩形,我只添加了以下函数

public boolean draw(Canvas canvas)公共布尔绘图(画布画布)

it runs successfully but it can not draw a rectangle in main Activity.它运行成功,但无法在主活动中绘制矩形。

package com.examplee.pro;包 com.examplee.pro;

import android.os.Bundle;导入 android.os.Bundle; import android.app.Activity;导入 android.app.Activity; import android.view.Menu;导入 android.view.Menu; import android.graphics.*;导入 android.graphics.*;

public class MainActivity extends Activity {公共类 MainActivity 扩展 Activity {

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean draw(Canvas canvas){ Paint o = new Paint(); o.setColor(0); canvas.drawRect(100,100,100,100,o); return true; } }

Where is the problem?问题出在哪儿?

What you need to do is to write your code in the onDraw method.您需要做的是在 onDraw 方法中编写代码。

Although, I am not sure how you can use the canvas.I am sure you can but I never used it.However, here is a helpful link that will get your job done for sure.Just take the time and read it,everything is detailed and clear!虽然,我不确定您如何使用画布。我相信您可以,但我从未使用过它。但是,这里有一个有用的链接,可以确保您完成工作。花点时间阅读它,一切都是详细而清晰!

http://developer.android.com/reference/android/graphics/Rect.html http://developer.android.com/reference/android/graphics/Rect.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM