2014年10月13日 星期一

android程式設計實例入門 -Sample改寫分享 (2014/10/14)

android程式設計實例入門 -Sample改寫分享 (2014/10/14)


 


此範例為程式碼(05\Sample07)的改寫,利用一個LinearLayout、一個SampleView實作GUI界面,建立自己的View元件,並利用onTouchEvent觸發取亂數畫出數個圓(其中數個圓的資訊全部存在ArrayList中)的使用範例,程式碼如下所示:


 


 


package com.jashsample;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/*
 * 建立自己的View元件,透過onTouchEvent觸發取亂數畫出數個圓(其中數個圓的資訊全部��在ArrayList中)。
 */
public class MainActivity extends Activity {
static final int num = 100;
SampleView sv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
setContentView(ll);

sv = new SampleView(this);

ll.addView(sv);
}
class SampleView extends View
{
ArrayList<Ball> bl;
float x, y;
public SampleView(Context cn)
{
super(cn);
bl = new ArrayList<Ball>();
Random rn = new Random();
for (int i=0; i<num; i++)
{
Ball b = new Ball();

b.x = (int)x+rn.nextInt(500);
b.y = (int)y+rn.nextInt(1000);
b.r = rn.nextInt(256);
b.g = rn.nextInt(256);
b.b = rn.nextInt(256);
bl.add(b);
}
}

public boolean onTouchEvent(MotionEvent e)
{
x = e.getX();
y = e.getY();
this.invalidate();//觸發onDraw
Random rn = new Random();
bl.clear();
for (int i=0; i<num; i++)
{
Ball b = new Ball();

b.x = (int)x+rn.nextInt(500);
b.y = (int)y+rn.nextInt(1000);
b.r = rn.nextInt(256);
b.g = rn.nextInt(256);
b.b = rn.nextInt(256);
bl.add(b);
}
return true;
}
protected void onDraw(Canvas cs)
{
super.onDraw(cs);
Random rn = new Random();
Paint p = new Paint();
for(int i=0; i<num; i++)
{
Ball b = bl.get(i);

p.setColor(Color.rgb(b.r,b.g,b.b));
p.setStyle(Paint.Style.FILL);
cs.drawCircle(b.x,b.y,10,p);
}
}
}
}
class Ball
{
public int x,y,r,g,b;
}












 




沒有留言:

張貼留言