When I first started learning Android, I found it a little confusing because there are often many different ways to accomplish a single task. In my case, I did not have a strong background in Java, otherwise it probably would have been easier for me to look at code samples an recognize these variations.
The
most obvious example of alternative approaches to solving a single
problem seems to be the various ways you can handle button clicks. As far as I know, there are four different ways to add listeners for handling button clicks. If you know of other ways, please post a comment and share them with us.
Here's a simple application that demonstrates four approaches to handling button clicks. When you click on any of the buttons, an alert will show up on the screen. There are two files of interest. The first is the main xml layout file, which has four buttons in it. This code should look familiar to you, except for the last button. A
new feature was added in Android 2.1 (API Level 7) which allows you to
specify an onClick listener from within the xml layout file.
Here's the xml layout file for our sample application...
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:text="Inner Class (btn1)" android:id="@+id/Button01" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="Anonymous Inner Class (btn2)" android:id="@+id/Button02" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="Implementing an Interface (btn3)" android:id="@+id/Button03" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <Button android:text="Calling From XML Layout (btn4)" android:id="@+id/Button04" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="btn4Listener"> </Button> </LinearLayout>
public class Main extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //method 1 - uses an inner class named btn1Listener... Button btn1 = (Button)findViewById(R.id.Button01); btn1.setOnClickListener(btn1Listener);
//method 2 - use an anonymous inner class as a listener... Button btn2 = (Button)findViewById(R.id.Button02); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showToastMessage("You clicked btn2 - uses an anonymouse inner class"); } });
//method 3 - note that this class implements //the View.OnClickListener interface //which means that we must implement the onClick() //method (which you'll find below).. Button btn3 = (Button)findViewById(R.id.Button03); btn3.setOnClickListener(this);
//method 4 - look at the method btn4Listener() below }
//here's the inner class used as a listener for btn1... private View.OnClickListener btn1Listener = new View.OnClickListener() { @Override public void onClick(View v) { showToastMessage("You clicked btn1 - uses an inner class named btn1Listener"); } };
//here's a method that you must have when your activity implements the //View.OnClickListener interface... @Override public void onClick(View v) { showToastMessage("you clicked on a btn3, which uses this Activity as the listener"); }
//here's the handler for btn4 (declared in the xml layout file)... //note: this method only works with android 2.1 (api level 7), it must be public and //must take a single parameter which is a View public void btn4Listener(View v) { showToastMessage("You clicked btn4 - listener was set up in the XML layout"); }
Whenever sang my songs on the stage on my own Whenever said my words Wishing they would be heard
I saw you smiling at me Was it real or just my fantasy You'd always be there in the corner of this tiny little bar
My last night here for you same old songs just once more My last night here with you Maybe Yes Maybe No
I kind of liked it your way How you shyly placed your eyes on me Did you ever know that I had mine on you?
Darling So there you are with that look on your face As if you've never hurt As if you've never down Shall I be the one for you Who pinches you softly but sure If frown is shown then I will know that you are no dreamer
So let me come to you Close as I wanted to be Close enough for me to feel your heart beating fast
and stay there as I whisper How I loved your peaceful eyes on me Did you ever know that I had mine on you?
Darling So share with me your love if you have enough your tears if you're holding back of pain If that's what it is How can I let you know I'm more than the dress and the voice Just reach me out then you will know that you're not dreaming
後來Julia終於完成了那首歌,〈eyes on me〉,發表後旋即成為當紅歌手,並下嫁一位高級官員。 Laguna聽著這些消息,只能一陣惆悵。 他娶了Caraway,然而Caraway過幾年即病逝,最後Laguna成為了記者,四處旅行。
-- 跟〈eyes on me〉有關的劇情大致上就這些。 王菲的歌詞中許多是那晚他們的對話內容,例如dreamer,那晚的對話便是如此提到。 整首歌歌詞是Julia形容Laguna的字句。
而 Final Fantasy VIII 的男主角史克爾即是Laguna與Caraway的兒子, 而女主角莉諾亞即是Julia的女兒.
==================================================
http://home.gamer.com.tw/creationDetail.php?sn=860190
Final Fantasy VIII OP
I'll be here...(我在這裡) Why...?(為什麼?) I'll be wating...here...(我會在這裡等著) For what...?(為了什麼?) I'll be waitig...for you...so...(我會在這裡等你...所以...) If you come here...(如果你來到這) You'll find me.(你會找到我) I promise.(我保證)
在開頭動畫 我們看到的這些話 道盡了劇情的主軸 也對於劇情有了重大的暗示
從FINAL FANTASY VIII的logo上面 我們可以清楚的看到史克爾擁抱著莉諾亞 也就是代表著 這一代的FINAL FANTASY 是在敘述一段關於愛的故事
Whenever sing my songs (不論在何時唱著我的歌) On the stage on my own (在舞台上也好,孤獨一個人也好) Whenever said my words (不管什麼時候說著我的話) Wishing they would be heard (一直希望能夠讓你聽見) I saw you smiling at me (望著你對我的笑容) Was it real or just my fantasy (不知道這是真實還是幻想) You’d always be there in the corner (你總是一如往日的出現在角落) Of this tiny little bar……………………. (在這狹小的小酒吧)
My last night here for you (昨夜我為你守候了一晚) Same old songs just once more (一遍一遍唱著你我的歌曲) My last night here with you? (昨晚我真的為了你而在這裡嗎?) Maybe yes maybe no (也許是,也許不是) I kind of liked it your way (我好喜歡你的一切) How you shyly placed your eyes on me (你怎麼能夠用如此羞澀的眼神望著我) Oh did you ever know? (喔!你曾知道嗎?) That I had mine on you………………. (我的心早就屬於你)
Darling so there you are (親愛的,所以你就在那裡) With that look on your face (讓我好好的看著你) As if you’re never hurt (就當你從不受傷) As if you’re never down (就當你從不跌倒) Shall I be the one for you (讓我成為你的一部份,好嗎?) Who pinches you softly but sure (誰溫柔但是卻傷了你呢?) If frown is shown then (如果為了挫折就皺眉的話) I will know that you are no dreamer…………………, (我會認為你是沒有夢想的人) So let me come to you (所以,讓我奔向你)
So let me come to you (所以,讓我奔向你) Close as I wanted to be (像我所想像的去接近你) Close enough for me (盡量的靠近我) To feel your heart beating fast (讓我去感受你快速的心跳) And stay there as I whisper (留在那裡,當我低語時) How I loved your peaceful eyes on me (我是如此愛你用溫柔的的眼神看著我) Did you ever know (你是否明瞭) That I had mine on you (我的心早就屬於你)
Dailing, so share with me (親愛的,跟我分享你的所有) Your love if you have enough (與你一起分享足夠的愛) Your tears if you're holding back (當你心情不好時,我會和你一同分擔) Or pain if that's what it is (或不管什麼痛苦,我與你承擔) How can I let you know (要如何才能讓你知道) I'm more than the dress and the voice (華麗的外表和動人的聲音都不夠形容我) Just reach me out then (只管放心的到我身邊) You will know that you're not dreaming (你會明瞭這不只是一場夢)
Darling so there you are (親愛的,所以你就在那裡) With that look on your face (讓我好好的看著你) As if you’re never hurt (就當你從不受傷) As if you’re never down (就當你從不跌倒) Shall I be the one for you (讓我成為你的一部份,好嗎?) Who pinches you softly but sure (誰溫柔但是卻傷了你呢?) If frown is shown then (如果為了挫折就皺眉的話) I will know that you are no dreamer…………………, (我會認為你是沒有夢想的人)
public Bitmap SavePixels(GL10 gl){ int b[] = new int[Width * Height];
IntBuffer ib = IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(0, 0, Width, Height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);
// The bytes within the ints are in the wrong order for android, but convert into a
// bitmap anyway. They're also bottom-to-top rather than top-to-bottom. We'll fix
// this up soon using some fast API calls.
Bitmap glbitmap = Bitmap.createBitmap(b, Width, Height, Bitmap.Config.ARGB_4444);
ib = null; // we're done with ib
b = null; // we're done with b, so allow the memory to be freed
// To swap the color channels, we'll use a ColorMatrix/ColorMatrixFilter. From the Android docs:
//
// This is a 5x4 matrix: [ a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t ]
// When applied to a color [r, g, b, a] the resulting color is computed as (after clamping):
//
// R' = a*R + b*G + c*B + d*A + e;
// G' = f*R + g*G + h*B + i*A + j;
// B' = k*R + l*G + m*B + n*A + o;
// A' = p*R + q*G + r*B + s*A + t;
//
// We want to swap R and B, so the coefficients will be:
// R' = B => 0,0,1,0,0
// G' = G => 0,1,0,0,0
// B' = R => 1,0,0,0,0
// A' = A => 0,0,0,1,0
final float[] cmVals = { 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(cmVals))); // our R<->B swapping paint->
Bitmap bitmap = Bitmap.createBitmap(Width, Height, Config.ARGB_4444); // the bitmap we're going to draw onto
Canvas canvas = new Canvas(bitmap); // we draw to the bitmap through a canvas
canvas.drawBitmap(glbitmap, 0, 0, paint); // draw the opengl bitmap onto the canvas, using the color swapping paint
glbitmap = null; // we're done with glbitmap, let go of its memory
// the image is still upside-down, so vertically flip it
Matrix matrix = new Matrix();
matrix.preScale(1.0f, -1.0f); // scaling: x = x, y = -y, i.e. vertically flip
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // new bitmap, using the flipping matrix
}
If
you want to profile your application to find performance bottlenecks
you can use the traceview tool. This gives you a graphical view of
performance traces of your application.
To create a trace add the following to your code where you want to start tracing:
Debug.startMethodTracing("myapp");
and then put the following when you want to stop tracing:
Debug.stopMethodTracing();
This will create a trace file call myapp.trace in the root directory of the SD Card. As it is written to the SD Card:
If you're using the emulator you'll need to add an SD card to your AVD.
You'll need to give you app permission to write the SD card by adding the following to your Manifest:
Once the file has been created you'll need to copy it to your PC. You can do this using the adb command: