Thứ Bảy, 23 tháng 2, 2013

Begining with E3roid in Android

Đầu tiên tạo mới 1 project
Step1: copy thư viên E3roid.jar vào thư mục libs trong project, sau đó right click chọn build path->add to build path.

Step2: Sửa lại file Activity chính thay extends Activity la extends từ E3Activity, đồng thời xóa bỏ code khi lúc tạo project tự rend ra.

Step3: Add các hàm overide của class E3Activity, thêm hàm bắt sự kiên touch onTouchEvent nữa
Trong đó: E3engine là để tạo một frame để chứa E3Scene các hình ảnh,
hàm onLoadResource để lấy các hình ảnh vào nhờ texture
- sprite là một hình ảnh tạo ra bằng một texture
-scene.getTopLayer().add(sprite); de them sprite vao scene
Source code:http://www.mediafire.com/?5xjd8gp62ie35r2


Thứ Hai, 18 tháng 2, 2013

Tao menu tren Android


Lập trình Android – Tạo menu trên Android (Adding Menu – Adding Setting) 15/01/2011

Posted by hnb1988 in Android Development
Tags: 
add a comment
Lâu ngày quá rồi mới quay lại phần lập trình mobile với Android. :-)
Hôm nay chúng ta tiếp tục với phần Add menu lên chương trình của chúng ta.
Project chúng ta tiếp tục là game Sudoku :-)
Tài liệu tham khảo:
  • Hello Android bắt đầu từ trang 64.
Các bài viết trước :
—————————————————————————————————————
Rồi chúng ta mở lại project trước và bắt đầu thực hiện nào.
Adding một Menu
Android hỗ trợ cho chúng ta 2 loại  menu chính
  • Menu button (với các checkbox)
  • Menu pop-up
Đầu tiên chúng ta làm về cái menu-button.
Và chúng ta áp dụng loại này tạo một menu-setting (sound, hint của game chẳng hạn)
OK, chúng ta sẽ làm một cái màn hình setting.
Load setting
Load setting
Và việc đầu tiên chúng ta tạo một màn hình load-setting như hình  trên. Load-setting nằm phía dưới màn hình (bottom).
Chúng ta add thêm đoạn định nghĩa các chuỗi string  cho nó trong file:/res/values/strings.xml
<string name=”settings_label”>Settings…</string>
<string name=”settings_title”>Sudoku settings</string>
<string name=”settings_shortcut”>s</string>
<string name=”music_title”>Music</string>
<string name=”music_summary”>Play background music</string>
<string name=”hints_title”>Hints</string>
<string name=”hints_summary”>Show hints during play</string>
Chúng ta tạo một định nghĩa layout cho menu-setting: res/menu/menu.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”&gt;
<item android:id=”@+id/settings”
android:title=”@string/settings_label”
android:alphabeticShortcut=”@string/settings_shortcut” />
</menu>
Rồi tới đây chúng ta tiếp tục chỉnh sửa lại file sudoku.java
Nhớ import những trường này vào trước:
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
Chúng ta cần override lại phương thức: Sudoku.onCreateOptionsMenu( )
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
getMenuInflater( ) trả về một thực thể của MenuInflater mà chúng ta sẽ sử dụng để đọc những định nghĩa từ XML lên. Khi chúng ta select bất kỳ menu-item nào thì onOptionsItemSelected( ) sẽ được gọi.
Và dưới đây là phương thức đó.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings:
startActivity(new Intent(this, Prefs.class));
return true;
// More items go here (if any) …
}
return false;
}
Để hiểu rõ kỹ hơn bạn xem thêm phần giải thích trong tài liệu mình nói phía trên.
Prefs là một lớp chúng ta sẽ định nghĩa sau. Nó hiển thị những gì chúng ta định nghĩa và cho người dùng thay đổi lựa chọn trên menu.
Rồi giờ chúng ta add những giá trị trong menu setting. Định nghĩa thêm một file: res/xml/settings.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<PreferenceScreen
xmlns:android=”http://schemas.android.com/apk/res/android”&gt;
<CheckBoxPreference
android:key=”music”
android:title=”@string/music_title”
android:summary=”@string/music_summary”
android:defaultValue=”true” />
<CheckBoxPreference
android:key=”hints”
android:title=”@string/hints_title”
android:summary=”@string/hints_summary”
android:defaultValue=”true” />
</PreferenceScreen>
Trong này chúng ta add hai lựa chọn: Background music và Hiển thị hint trong khi chơi hay không.
Rồi giờ chúng ta định nghĩa lớp Prefs lúc nảy nói ở trên. Bạn tạo file /Prefs.java trong gói ứng dụng của bạn.
package org.example.sudoku;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Phương thức addPreferencesFromResource( ) đọc các giá trị định nghĩa trong file XML chúng ta định nghĩa ở trên.
Giờ chúng ta chạy thử lên xem nào và đừng quyên đăng ký trong fileAndroidManifest.xml ha :-)
<activity android:name=”.Prefs”
android:label=”@string/settings_title”>
</activity>
Kết quả khi chúng ta chạy chương trình mình lên này:
Menu setting
Menu setting
Và chúng ta có thể thay đổi giá trị chọn hay không
Menu setting
Menu setting
Rồi giờ chúng ta thử tạo một Menu pop-up. Và chúng ta sẽ tạo một menu lựa chọn mức độ khó dễ của game.
Chúng ta định nghĩa những giá trị trong : res/values/strings.xml
<string name=”new_game_title”>Difficulty</string>
<string name=”easy_label”>Easy</string>
<string name=”medium_label”>Medium</string>
<string name=”hard_label”>Hard</string>
Tạo file định nghĩa các mức độ game như sau: res/values/arrays.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
<array name=”difficulty”>
<item>@string/easy_label</item>
<item>@string/medium_label</item>
<item>@string/hard_label</item>
</array>
</resources>
Giờ chúng ta sẽ chỉnh sửa lại một tí file Sudoku.java
Import các trường sau vào:
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
Các bạn tìm tới phần switch() case hôm trước làm ở phương thức onClick() add thêm cho thằng new-game như sau:
case R.id.new_button:
openNewGameDialog();
break;
openNewGameDialog() sẽ thực hiện mở dialog hiển thị các mức độ khó dễ của game và nó như sau:
private static final String TAG = “Sudoku”;
private void openNewGameDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.new_game_title)
.setItems(R.array.difficulty,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
startGame(i);
}
}).show();
}
private void startGame(int i) {
Log.d(TAG, “clicked on ” + i);
// Start game here…
}

Rồi như thế là xong rồi. Giờ chúng ta xem kết quả như thế nào khi chạy game mình lên ha.
Menu game
Menu game
Chúng ta thử click vào New Game nào, và kết quả là:
Menu mức độ game
Menu mức độ game
Nhìn khá gọn và đẹp phải không các bạn…
Chúc các bạn thành công ha….. :-)