選擇Android Application Project
專案名稱: DrawRandCircle
按下<下一步>
MainActivity
按下<執行>按鈕前請先接上實體Android手機
編譯成Android Application
點選src資料夾, 按滑鼠右鍵->新建->類別
輸入類別名稱: CircleCanvas
按下<完成>後如下
CircleCanvas.java寫入程式
package com.example.drawrandcircle;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class CircleCanvas extends View
{
public List<CircleInfo> mCircleInfos = new ArrayList<CircleCanvas.CircleInfo>();
public static class CircleInfo
{
private float x;
private float y;
private float radius;
private int color;
public float getX()
{
return x;
}
public void setX(float x)
{
this.x = x;
}
public float getY()
{
return y;
}
public void setY(float y)
{
this.y = y;
}
public float getRadius()
{
return radius;
}
public void setRadius(float radius)
{
this.radius = radius;
}
public int getColor()
{
return color;
}
public void setColor(int color)
{
this.color = color;
}
}
public CircleCanvas(Context context)
{
super(context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
for (CircleInfo circleInfo : mCircleInfos)
{
Paint paint = new Paint();
paint.setColor(circleInfo.getColor());
canvas.drawCircle(circleInfo.getX(), circleInfo.getY(),
circleInfo.getRadius(), paint);
}
}
}
MainActivity.java
package com.example.drawrandcircle;
import java.util.Random;
import com.example.drawrandcircle.CircleCanvas.CircleInfo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends Activity {
//------------插入[開始]---------------
private CircleCanvas mCircleCanvas;
//------------插入[結束]----------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//------------插入[開始]---------------
ViewGroup viewGroup = (ViewGroup) getLayoutInflater().inflate(
R.layout.activity_main, null);
mCircleCanvas = new CircleCanvas(this);
viewGroup.addView(mCircleCanvas, new LayoutParams(
LayoutParams.FILL_PARENT, 650));
setContentView(viewGroup);
//------------插入[結束]----------------
//setContentView(R.layout.activity_main);
}
public void button1_Click(View view)
{
Random random = new Random();
float randomX = (float) (100 + random.nextInt(100));
float randomY = (float) (100 + random.nextInt(100));
float randomRadius = (float) (20 + random.nextInt(40));
int randomColor = 0;
if (random.nextInt(100) > 50)
{
randomColor = Color.BLUE;
}
else
{
if (random.nextInt(100) > 50)
randomColor = Color.RED;
else
randomColor = Color.GREEN;
}
CircleInfo circleInfo = new CircleInfo();
circleInfo.setX(randomX);
circleInfo.setY(randomY);
circleInfo.setRadius(randomRadius);
circleInfo.setColor(randomColor);
mCircleCanvas.mCircleInfos.add(circleInfo);
mCircleCanvas.invalidate();
// 輸出偵錯資訊
Log.v("VERBOSE_randomX", String.valueOf(randomX));
Log.d("DEBUG_randomY", String.valueOf(randomY));
Log.i("INFO_randomRadius", String.valueOf(randomRadius));
Log.w("WARN_randomColor", String.valueOf(randomColor));
Log.e("ERROR_area", String.valueOf(3.14 * randomRadius * randomRadius));
Log.println(Log.VERBOSE, "VERBOSE_println", "VERBOSE_println");
}
public void button2_Click(View view)
{
mCircleCanvas.mCircleInfos.clear();
mCircleCanvas.invalidate();
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
開啟偵錯工具DDMS( Dalvik Debug Monintor Service)
Window->Open Perspective->Others->選擇DDMS
自動跳出DDMS頁面
DDMS->點擊顯示選單
這也未免太強大了, 輕輕鬆鬆就可以取得畫面
按下Copy複製圖片
按下上圖中<繪圖>, 程式碼中的debug訊息就會出現在LogCat, 超讚的~~~
(v,d,i,w,e)表示verbose, debug, info, warn, error區別不大, 輸出字體顏色不同, 方便區分
手機->啟動USB除錯
開啟Preference中Installed JRE確認已安裝的JRE版本
--------------------------------------------------------------------------------------------------------
專案位置: C:\Users\ryanwang\workspace\DrawRandCircle
參考資料:
1. droiddraw下載
全站熱搜
留言列表