[英]I want draw a rectangle in MainActivity.java but my code can not draw a rectangle in main Activity
我想在 MainActivity.java 中繪制一個矩形,但我的代碼無法繪制一個矩形,我只添加了以下函數
公共布爾繪圖(畫布畫布)
它運行成功,但無法在主活動中繪制矩形。
包 com.examplee.pro;
導入 android.os.Bundle; 導入 android.app.Activity; 導入 android.view.Menu; 導入 android.graphics.*;
公共類 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; } }
問題出在哪兒?
您需要做的是在 onDraw 方法中編寫代碼。
雖然,我不確定您如何使用畫布。我相信您可以,但我從未使用過它。但是,這里有一個有用的鏈接,可以確保您完成工作。花點時間閱讀它,一切都是詳細而清晰!
http://developer.android.com/reference/android/graphics/Rect.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.