初涉android
Android Studio study
1.類的定義: 例如﹕class Rose {
private double price;
private int month;
public void say(){
System.out.println("Color is RED.");
} }
◎ 這Rose就是我們新創的類別,將用來誕生 對象,
以描述自然界的玫瑰花。於是可誕 生對象如下﹕
Rose rose = new Rose();
◎ 這new Rose()指令就誕生一個Rose類的對象
public class JMain {
public static void main(String[] args)
{
Rose rose = new Rose();
rose.say();
} }
2.設計模式:
Note:在View類別裏有個onDraw()函數,View類 別體系裏的每一個類別都必須覆寫
(Override) 這個onDraw()函數,來執行實 際繪圖的動作。
3.public class myView extends View {
private Paint paint= new Paint(); private int line_x = 100, line_y = 100;
private float count = 0;
myView(Context ctx)
{ super(ctx); }
@Override protected void onDraw(Canvas canvas)
{ super.onDraw(canvas);
if( count > 12) count = 0;
int x = (int) (75.0 * Math.cos(2*Math.PI * count/12.0));
int y = (int) (75.0 * Math.sin(2*Math.PI * count/12.0));
count++;
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+x, line_y+y, paint);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
canvas.drawRect(line_x-5, line_y - 5,
line_x+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(line_x-3, line_y - 3, line_x+3,
line_y + 3, paint); }
}
遊戲的基本動作就是不斷的進行:繪圖和 刷新(Refresh)畫面。
其中,onDraw()函數 實踐畫圖,將圖形繪製於View的畫布 (Canvas)上,
並顯示出來;而invalidate()
函數則啓動畫面的刷新,重新呼叫一次 onDraw()函數。
public class myView extends View {
private Paint paint= new Paint();
private int line_x = 100, line_y = 100;
myView(Context ctx) { super(ctx); }
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//-----------------------------------------------------
if( count > 12) count = 0;
int x = (int) (75.0 * Math.cos(2*Math.PI * count/12.0));
int y = (int) (75.0 * Math.sin(2*Math.PI * count/12.0));
count++; //---------------------------------------------
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+x, line_y+y, paint);
paint.setStrokeWidth(2); paint.setColor(Color.RED);
canvas.drawRect(line_x-5, line_y - 5, line_x+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(line_x-3, line_y - 3, line_x+3, line_y + 3, paint);
try { Thread.sleep(1000);
}catch (InterruptedException ie) {}
invalidate(); } }
// myActivity.java
// ……
public class myActivity extends Activity
implements OnClickListener {
private myView mv = null;
private Button ibtn;
@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle); public void show_layout_01()
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
mv = new myView(this); LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(200, 200);
param.topMargin = 10; param.leftMargin = 10;
layout.addView(mv, param);
ibtn = new Button(this);
ibtn.setOnClickListener(this);
ibtn.setText("Exit");
ibtn.setBackgroundResource(R.drawable.gray);
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(200, 65);
param1.topMargin = 10; param1.leftMargin = 10;
layout.addView(ibtn, param1);
//-----------------------------------------------
setContentView(layout);
}
public void onClick(View v)
{
finish();
} }
在Android框架裏,處處可見Factory Method模式。例如,
Activity、Service等 抽象類別裏都定義了onCreate()函數,
它 就是一個典型的FactoryMethod函數。工廠:產品
// GraphicView.java
// ……..
public class GraphicView extends View
{
private Paint paint= new Paint();
GraphicView(Context ctx)
{ super(ctx); }
@Override protected void onDraw(Canvas canvas)
{ int line_x = 10; int line_y = 50;
canvas.drawColor(Color.WHITE);
paint.setColor(Color.GRAY); paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+120, line_y, paint);
paint.setColor(Color.BLACK); paint.setStrokeWidth(2);
canvas.drawText("這是繪圖區", line_x, line_y + 50, paint);
int pos = 70; paint.setColor(Color.RED);
canvas.drawRect(pos-5, line_y - 5, pos+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(pos-3, line_y - 3, pos+3, line_y + 3, paint); }}
// myActivity.java
// ……..
public class ac01 extends Activity {
private GraphicView gv = null;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
gv = new GraphicView(this);
setContentView(gv);
} }
3.GoF的Observer設計模式
定義對象間的1:N依賴關係,以便當一個主體 對象(如A)的狀態發生改變時,
所有依賴於它 的衆多對象(如B, C, D …)都得到通知,然後 可向主體對象(A)取得最新狀態內容。
如果Observer是個純粹抽象類別(Pure Abstract Class),它扮演接口角色,
就相當 於Java語言的interface機制。
雖然父類別Observer已經變爲IObserver接 口了,其卡榫函數還是存在那裏,只是形 式有些變化而已。
• Android的View體系,其對象(又稱爲控件) 會呈現於UI畫面上。
• 當用戶按下該控件,引發控件的狀態改變 了,
就會透過onClickListener接口來通知 所有的Listener對象
// myActivity.java // ……..
public class myActivity extends Activity
implements onClickListener
{
@Override public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button btn = new Button(this);
btn.setText("OK");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(clickListener);
setContentView(btn);
}
@Override public void onClick(View v)
{
String name = ((Button)v).getText().toString();
setTitle( name + " button clicked");
} }
4,GoF的Composite模式
• 此模式能建立多層級的包含關係(即多層級 Whole-part關係)。
• 在自然界中常見這種關係,例如,樹葉是 樹的一部分,
但樹葉又是個整體,其內含 着葉脈、葉綠素等「部分」對象。
• 在Android平臺裏,像Button或 ImageButton等屏幕控件皆通稱爲View。
• 多個View能組合在一起,就會各種排列方 式,即稱爲「佈局」(Layout)。
• 這Layout類別就是從ViewGroup父類別衍 生出來的。
• 其中,把OK和Exit兩個按鈕看成一組,構 成一組垂直型的佈局,稱爲垂直 LinearLayout。
• 然後,把這個垂直LinearLayout與Cancel 按鈕看成一組,
構成一組水平型的佈局, 稱爲水平LinearLayout
// Axc01.java
// ……
public class Ax01 extends Activity implements OnClickListener
{
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2; private EditText et;
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
et = new EditText(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(FP, WC);
layout.addView(et, param);
btn = new Button(this); btn.setText("OK");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(WC, WC);
param2.topMargin = 5;
btn.setBackgroundResource(R.drawable.ok_blue);
btn.setOnClickListener(this);
layout.addView(btn, param2);
btn2 = new Button(this); btn2.setTextColor(Color.RED);
btn2.setText("Exit");
btn2.setBackgroundResource(R.drawable.exit_gray);
btn2.setOnClickListener(this); layout.addView(btn2, param2);
setContentView(layout);
}
public void onClick(View v) {
if(v == btn)
setTitle(et.getText());
else if(v == btn2)
finish();
} }
實例架構任務:
1.class Task implements Runnable {
public void run()
{ int sum = 0;
for (int i = 0; i <= 100; i++)
sum += i; System.out.println("Result: " + sum);
} }
2.class Task extends Thread {
public void run() {
int sum = 0;
for (int i = 0; i <= 100; i++)
sum += i;
System.out.println("Result: " + sum);
} }
3.public class JMain {
public static void main(String[] args)
{
Thread t = new Thread(new Task());
t.start();
System.out.println("Waiting...");
} }
4.public class JMain {
public static void main(String[] args)
{
Thread t = new Task();
t.start();
System.out.println("Waiting...");
} }
5.EIT模式:Engine(引擎)Interface(接口)Tire(輪胎)
◎ 主要任務:(強龍的)架構師依循EIT造形, 分出<E、I、T>三種要素
◎ 產品效益:讓<T>容納地面的變化, <E&I>就能通用於各平臺(如海灘、街道、 高山等地面)了
◎ 分工模式:
1. 架構師做EIT設計
2. 強龍做<E&I>
3. 地頭蛇配<T>
◎<E>是控制點,透過<I>來驅動<T>
◎<E>+<I> = 框架(Framework)
◎<T> = 插件(Plug-in)
分工模式:
1. 架構師做EIT設計
2. 強龍做框架
3. 地頭蛇配插件
依循EIT,進行演練…
1. 架構師依循EIT造形,分出<E、I、T> 三部分
2. 把強龍知識寫入於<E>裏 3. 把地頭蛇(即買主)知識寫入到<T>裏
3. 詳細定義接口<I> 4. <E>是控制點,透過<I>來調用<T>。
JNI架構原理
1.java native interface:提供API實現java和其他語言的通信
/* cx-02.c */
#include <stdio.h>
#include <string.h>
struct smile
{
char sna;
float price;
};
int main(void)
{
struct smile x;
struct smile *px;
px = &x; px->sna = 'R';
px->price = 26.8;
printf( "Sna=[%c], Price=%.1f", x.sna, x.price );
return 0;
}
• px是struct smile型態的指針﹐x 是struct smile型態的變量﹐
px可以指向x 變量。 • “&” 運算能把x 變量的地址存入px中﹐ 使得px指向x 變量。
• 指令: px->sna = 'R'; px->price = 26.8;
• 把數據存入結構(變量)
• malloc()和free()是最常用的動態內存分配 函數。如果在執行時需要空間來儲存數據
﹐宜使用malloc()函數。用完了就用free() 釋放該空間。malloc()之格式爲﹕
指針 = malloc( 空間大小 )
• 例如﹕ ptr = malloc(100);
/* cx03.c */
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
struct kiki {
char na[10];
short int age;
}; typedef struct kiki NODE;
int main(void)
{
NODE *pn;
pn = (NODE *) malloc (sizeof(NODE));
if( pn==NULL )
{
printf("malloc() failed\n");
exit(0);
}
strcpy( pn->na,"Alvin");
pn->age = 28;
printf("AGE=%d", pn->age);
free(pn);
return 0;
}
• typedef 指令定義的新型態──NODE是 struct kiki 的別名。
• 如果你計算機的sizeof(NODE)值爲 16
﹐malloc()就索取16 bytes的空間﹐並令 pn指向此區域了。
1.類的定義: 例如﹕class Rose {
private double price;
private int month;
public void say(){
System.out.println("Color is RED.");
} }
◎ 這Rose就是我們新創的類別,將用來誕生 對象,
以描述自然界的玫瑰花。於是可誕 生對象如下﹕
Rose rose = new Rose();
◎ 這new Rose()指令就誕生一個Rose類的對象
public class JMain {
public static void main(String[] args)
{
Rose rose = new Rose();
rose.say();
} }
2.設計模式:
Note:在View類別裏有個onDraw()函數,View類 別體系裏的每一個類別都必須覆寫
(Override) 這個onDraw()函數,來執行實 際繪圖的動作。
3.public class myView extends View {
private Paint paint= new Paint(); private int line_x = 100, line_y = 100;
private float count = 0;
myView(Context ctx)
{ super(ctx); }
@Override protected void onDraw(Canvas canvas)
{ super.onDraw(canvas);
if( count > 12) count = 0;
int x = (int) (75.0 * Math.cos(2*Math.PI * count/12.0));
int y = (int) (75.0 * Math.sin(2*Math.PI * count/12.0));
count++;
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+x, line_y+y, paint);
paint.setStrokeWidth(2);
paint.setColor(Color.RED);
canvas.drawRect(line_x-5, line_y - 5,
line_x+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(line_x-3, line_y - 3, line_x+3,
line_y + 3, paint); }
}
遊戲的基本動作就是不斷的進行:繪圖和 刷新(Refresh)畫面。
其中,onDraw()函數 實踐畫圖,將圖形繪製於View的畫布 (Canvas)上,
並顯示出來;而invalidate()
函數則啓動畫面的刷新,重新呼叫一次 onDraw()函數。
public class myView extends View {
private Paint paint= new Paint();
private int line_x = 100, line_y = 100;
myView(Context ctx) { super(ctx); }
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//-----------------------------------------------------
if( count > 12) count = 0;
int x = (int) (75.0 * Math.cos(2*Math.PI * count/12.0));
int y = (int) (75.0 * Math.sin(2*Math.PI * count/12.0));
count++; //---------------------------------------------
canvas.drawColor(Color.WHITE);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+x, line_y+y, paint);
paint.setStrokeWidth(2); paint.setColor(Color.RED);
canvas.drawRect(line_x-5, line_y - 5, line_x+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(line_x-3, line_y - 3, line_x+3, line_y + 3, paint);
try { Thread.sleep(1000);
}catch (InterruptedException ie) {}
invalidate(); } }
// myActivity.java
// ……
public class myActivity extends Activity
implements OnClickListener {
private myView mv = null;
private Button ibtn;
@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle); public void show_layout_01()
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
mv = new myView(this); LinearLayout.LayoutParams param =
new LinearLayout.LayoutParams(200, 200);
param.topMargin = 10; param.leftMargin = 10;
layout.addView(mv, param);
ibtn = new Button(this);
ibtn.setOnClickListener(this);
ibtn.setText("Exit");
ibtn.setBackgroundResource(R.drawable.gray);
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(200, 65);
param1.topMargin = 10; param1.leftMargin = 10;
layout.addView(ibtn, param1);
//-----------------------------------------------
setContentView(layout);
}
public void onClick(View v)
{
finish();
} }
在Android框架裏,處處可見Factory Method模式。例如,
Activity、Service等 抽象類別裏都定義了onCreate()函數,
它 就是一個典型的FactoryMethod函數。工廠:產品
// GraphicView.java
// ……..
public class GraphicView extends View
{
private Paint paint= new Paint();
GraphicView(Context ctx)
{ super(ctx); }
@Override protected void onDraw(Canvas canvas)
{ int line_x = 10; int line_y = 50;
canvas.drawColor(Color.WHITE);
paint.setColor(Color.GRAY); paint.setStrokeWidth(3);
canvas.drawLine(line_x, line_y, line_x+120, line_y, paint);
paint.setColor(Color.BLACK); paint.setStrokeWidth(2);
canvas.drawText("這是繪圖區", line_x, line_y + 50, paint);
int pos = 70; paint.setColor(Color.RED);
canvas.drawRect(pos-5, line_y - 5, pos+5, line_y + 5, paint);
paint.setColor(Color.YELLOW);
canvas.drawRect(pos-3, line_y - 3, pos+3, line_y + 3, paint); }}
// myActivity.java
// ……..
public class ac01 extends Activity {
private GraphicView gv = null;
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate(savedInstanceState);
gv = new GraphicView(this);
setContentView(gv);
} }
3.GoF的Observer設計模式
定義對象間的1:N依賴關係,以便當一個主體 對象(如A)的狀態發生改變時,
所有依賴於它 的衆多對象(如B, C, D …)都得到通知,然後 可向主體對象(A)取得最新狀態內容。
如果Observer是個純粹抽象類別(Pure Abstract Class),它扮演接口角色,
就相當 於Java語言的interface機制。
雖然父類別Observer已經變爲IObserver接 口了,其卡榫函數還是存在那裏,只是形 式有些變化而已。
• Android的View體系,其對象(又稱爲控件) 會呈現於UI畫面上。
• 當用戶按下該控件,引發控件的狀態改變 了,
就會透過onClickListener接口來通知 所有的Listener對象
// myActivity.java // ……..
public class myActivity extends Activity
implements onClickListener
{
@Override public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
Button btn = new Button(this);
btn.setText("OK");
btn.setBackgroundResource(R.drawable.heart);
btn.setOnClickListener(clickListener);
setContentView(btn);
}
@Override public void onClick(View v)
{
String name = ((Button)v).getText().toString();
setTitle( name + " button clicked");
} }
4,GoF的Composite模式
• 此模式能建立多層級的包含關係(即多層級 Whole-part關係)。
• 在自然界中常見這種關係,例如,樹葉是 樹的一部分,
但樹葉又是個整體,其內含 着葉脈、葉綠素等「部分」對象。
• 在Android平臺裏,像Button或 ImageButton等屏幕控件皆通稱爲View。
• 多個View能組合在一起,就會各種排列方 式,即稱爲「佈局」(Layout)。
• 這Layout類別就是從ViewGroup父類別衍 生出來的。
• 其中,把OK和Exit兩個按鈕看成一組,構 成一組垂直型的佈局,稱爲垂直 LinearLayout。
• 然後,把這個垂直LinearLayout與Cancel 按鈕看成一組,
構成一組水平型的佈局, 稱爲水平LinearLayout
// Axc01.java
// ……
public class Ax01 extends Activity implements OnClickListener
{
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
private Button btn, btn2; private EditText et;
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
et = new EditText(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(FP, WC);
layout.addView(et, param);
btn = new Button(this); btn.setText("OK");
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(WC, WC);
param2.topMargin = 5;
btn.setBackgroundResource(R.drawable.ok_blue);
btn.setOnClickListener(this);
layout.addView(btn, param2);
btn2 = new Button(this); btn2.setTextColor(Color.RED);
btn2.setText("Exit");
btn2.setBackgroundResource(R.drawable.exit_gray);
btn2.setOnClickListener(this); layout.addView(btn2, param2);
setContentView(layout);
}
public void onClick(View v) {
if(v == btn)
setTitle(et.getText());
else if(v == btn2)
finish();
} }
實例架構任務:
1.class Task implements Runnable {
public void run()
{ int sum = 0;
for (int i = 0; i <= 100; i++)
sum += i; System.out.println("Result: " + sum);
} }
2.class Task extends Thread {
public void run() {
int sum = 0;
for (int i = 0; i <= 100; i++)
sum += i;
System.out.println("Result: " + sum);
} }
3.public class JMain {
public static void main(String[] args)
{
Thread t = new Thread(new Task());
t.start();
System.out.println("Waiting...");
} }
4.public class JMain {
public static void main(String[] args)
{
Thread t = new Task();
t.start();
System.out.println("Waiting...");
} }
5.EIT模式:Engine(引擎)Interface(接口)Tire(輪胎)
◎ 主要任務:(強龍的)架構師依循EIT造形, 分出<E、I、T>三種要素
◎ 產品效益:讓<T>容納地面的變化, <E&I>就能通用於各平臺(如海灘、街道、 高山等地面)了
◎ 分工模式:
1. 架構師做EIT設計
2. 強龍做<E&I>
3. 地頭蛇配<T>
◎<E>是控制點,透過<I>來驅動<T>
◎<E>+<I> = 框架(Framework)
◎<T> = 插件(Plug-in)
分工模式:
1. 架構師做EIT設計
2. 強龍做框架
3. 地頭蛇配插件
依循EIT,進行演練…
1. 架構師依循EIT造形,分出<E、I、T> 三部分
2. 把強龍知識寫入於<E>裏 3. 把地頭蛇(即買主)知識寫入到<T>裏
3. 詳細定義接口<I> 4. <E>是控制點,透過<I>來調用<T>。
JNI架構原理
1.java native interface:提供API實現java和其他語言的通信
/* cx-02.c */
#include <stdio.h>
#include <string.h>
struct smile
{
char sna;
float price;
};
int main(void)
{
struct smile x;
struct smile *px;
px = &x; px->sna = 'R';
px->price = 26.8;
printf( "Sna=[%c], Price=%.1f", x.sna, x.price );
return 0;
}
• px是struct smile型態的指針﹐x 是struct smile型態的變量﹐
px可以指向x 變量。 • “&” 運算能把x 變量的地址存入px中﹐ 使得px指向x 變量。
• 指令: px->sna = 'R'; px->price = 26.8;
• 把數據存入結構(變量)
• malloc()和free()是最常用的動態內存分配 函數。如果在執行時需要空間來儲存數據
﹐宜使用malloc()函數。用完了就用free() 釋放該空間。malloc()之格式爲﹕
指針 = malloc( 空間大小 )
• 例如﹕ ptr = malloc(100);
/* cx03.c */
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
struct kiki {
char na[10];
short int age;
}; typedef struct kiki NODE;
int main(void)
{
NODE *pn;
pn = (NODE *) malloc (sizeof(NODE));
if( pn==NULL )
{
printf("malloc() failed\n");
exit(0);
}
strcpy( pn->na,"Alvin");
pn->age = 28;
printf("AGE=%d", pn->age);
free(pn);
return 0;
}
• typedef 指令定義的新型態──NODE是 struct kiki 的別名。
• 如果你計算機的sizeof(NODE)值爲 16
﹐malloc()就索取16 bytes的空間﹐並令 pn指向此區域了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.