選擇Android Application Project

image

專案名稱: DrawRandCircle

image

按下<下一步>

image

image

image

MainActivity

image

按下<執行>按鈕前請先接上實體Android手機

image

編譯成Android Application

image

點選src資料夾, 按滑鼠右鍵->新建->類別

image

輸入類別名稱: CircleCanvas

image

按下<完成>後如下

image

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)

image

Window->Open Perspective->Others->選擇DDMS

image

自動跳出DDMS頁面

image

DDMS->點擊顯示選單

image

這也未免太強大了, 輕輕鬆鬆就可以取得畫面

image

按下Copy複製圖片

image

按下上圖中<繪圖>, 程式碼中的debug訊息就會出現在LogCat, 超讚的~~~

(v,d,i,w,e)表示verbose, debug, info, warn, error區別不大, 輸出字體顏色不同, 方便區分

image

手機->啟動USB除錯

image

開啟Preference中Installed JRE確認已安裝的JRE版本

image

--------------------------------------------------------------------------------------------------------

專案位置: C:\Users\ryanwang\workspace\DrawRandCircle

image

範例程式download

參考資料:

1. droiddraw下載

2. Android 的視覺化介面設計工具 -- Droiddraw

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()