Try and fail, try, try, try...........fail
Hope success in next try
Thứ Ba, 17 tháng 12, 2013
Thứ Năm, 12 tháng 12, 2013
Android
MyScene.registerTouchArea(animatedSprite);//Phải đăng ký là cho phép AnimatedSprite này được lắng nghe sự kiện
BitmapTextureAllas dung de bo nho chua anh
- Font
private BitmapTextureAtlas bitmapTextureAtlas;//Lưu ảnh trong bộ nhớ
private TextureRegion textureRegion;//Lưu khi load ảnh
private Sprite sprite;//Sprite quả bóng
BitmapTextureAtlas + Font +Text dung cho Text
BitmapTextureAtlas + Font +ChangeableText dung cho Text
thay doi Text
//Cập nhật (coi như vòn lặp hoạt cảnh)
MyScene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() {
// TODO Auto-generated method stub
}
//Muốn cập nhật cái gì thì cho vào đây
@Override
public void onUpdate(float pSecondsElapsed) {
try {
//Tạm dừng cập nhật trong 1s
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Cập nhật lại những gì bạn cần vẽ lại liên tục
dem++;
textchange.setText(String.valueOf(dem));//Cứ sau 1s ta lại hiện thị đếm
}
});
BitmapTextureAtlas + TextureRegion +Sprite
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bitmapTextureAtlas, this, "sprite.png", 0, 0, 2, 1);
this.mEngine.getTextureManager().loadTexture(this.bitmapTextureAtlas);
this.mEngine.registerUpdateHandler(new FPSLogger()); //=====================================LẮNG NGHE SỰ KIỆN CHẠM VÀO MÀN HÌNH MyScene======================================================================
//Bắt sự kiện khi chạm vào MyScene
MyScene.setTouchAreaBindingEnabled(true);//cho phép lắng nghe
MyScene.setOnSceneTouchListener(new IOnSceneTouchListener() {
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
System.out.println("Sprite onSceneTouchEvent: chạm vào màn hình");
System.out.println("Tọa độ X = "+pSceneTouchEvent.getX());
System.out.println("Tọa độ Y = "+pSceneTouchEvent.getY());
//Khi chạm vào màn hình ta di chuyển AnimatedSprite theo tọ độ y.
animatedSprite.setPosition(animatedSprite.getX(), pSceneTouchEvent.getY());
return true;
}
});
//Check su kien va cham vao sprite
this.mSpirte = new Sprite(100, 100, textRegion){
//lang nghe su kien cham vao sprite
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN){
System.out.println("Sprite onTouched: Cham vao spirte");
System.out.println("Toa do sprite x: "+pTouchAreaLocalX);
System.out.println("Toa do sprite y: "+pTouchAreaLocalY);
}
return true;
}
};
BitmapTextureAtlas + TitledTextureRegion + AnimatedSprite
BitmapTextureAllas dung de bo nho chua anh
- Font
private BitmapTextureAtlas bitmapTextureAtlas;//Lưu ảnh trong bộ nhớ
private TextureRegion textureRegion;//Lưu khi load ảnh
private Sprite sprite;//Sprite quả bóng
BitmapTextureAtlas + Font +Text dung cho Text
BitmapTextureAtlas + Font +ChangeableText dung cho Text
thay doi Text
//Cập nhật (coi như vòn lặp hoạt cảnh)
MyScene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void reset() {
// TODO Auto-generated method stub
}
//Muốn cập nhật cái gì thì cho vào đây
@Override
public void onUpdate(float pSecondsElapsed) {
try {
//Tạm dừng cập nhật trong 1s
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Cập nhật lại những gì bạn cần vẽ lại liên tục
dem++;
textchange.setText(String.valueOf(dem));//Cứ sau 1s ta lại hiện thị đếm
}
});
BitmapTextureAtlas + TextureRegion +Sprite
BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.bitmapTextureAtlas, this, "sprite.png", 0, 0, 2, 1);
this.mEngine.getTextureManager().loadTexture(this.bitmapTextureAtlas);
this.mEngine.registerUpdateHandler(new FPSLogger()); //=====================================LẮNG NGHE SỰ KIỆN CHẠM VÀO MÀN HÌNH MyScene======================================================================
//Bắt sự kiện khi chạm vào MyScene
MyScene.setTouchAreaBindingEnabled(true);//cho phép lắng nghe
MyScene.setOnSceneTouchListener(new IOnSceneTouchListener() {
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
System.out.println("Sprite onSceneTouchEvent: chạm vào màn hình");
System.out.println("Tọa độ X = "+pSceneTouchEvent.getX());
System.out.println("Tọa độ Y = "+pSceneTouchEvent.getY());
//Khi chạm vào màn hình ta di chuyển AnimatedSprite theo tọ độ y.
animatedSprite.setPosition(animatedSprite.getX(), pSceneTouchEvent.getY());
return true;
}
});
//Check su kien va cham vao sprite
this.mSpirte = new Sprite(100, 100, textRegion){
//lang nghe su kien cham vao sprite
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.getAction()==TouchEvent.ACTION_DOWN){
System.out.println("Sprite onTouched: Cham vao spirte");
System.out.println("Toa do sprite x: "+pTouchAreaLocalX);
System.out.println("Toa do sprite y: "+pTouchAreaLocalY);
}
return true;
}
};
BitmapTextureAtlas + TitledTextureRegion + AnimatedSprite
Thứ Bảy, 7 tháng 12, 2013
ngat trang blog
Sử dụng tiện ích "Chèn dấu ngắt nhảy" (một tiện ích có sẵn của Blogspot, sẽ hiển thị khi soạn thảo, chỉnh sửa bài viết) bằng cách nhấp chuột trái vào đoạn văn bản mà bạn muốn cho hiển thị [1] (đoạn văn bản này tính từ dòng đầu tiên trở xuống - xem ảnh minh họa bên dưới), rồi nhấp chuột vào hình tờ giấy bị "rách" [2] trên thanh tác vụ đăng bài.
Thứ Năm, 10 tháng 10, 2013
Chủ Nhật, 8 tháng 9, 2013
Mùi hoa sữa đầu thu
Bắt đầu thu rùi, hnay đã thấy thơm mùi hoa sữa đâu đó, hơn một năm rùi đấy nhỉ...
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
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ứ Sáu, 22 tháng 2, 2013
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: andoid, lập trình android, lập trình mobile, tìm hiểu android, tạo giao diện andoird, tạo menu trên android
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.
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>
<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”>
<item android:id=”@+id/settings”
android:title=”@string/settings_label”
android:alphabeticShortcut=”@string/settings_shortcut” />
</menu>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>
<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( )
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.
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.
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”>
<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>
<PreferenceScreen
xmlns:android=”http://schemas.android.com/apk/res/android”>
<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;
import android.preference.PreferenceActivity;
public class Prefs extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
@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>
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:
Và chúng ta có thể thay đổi giá trị chọn hay không
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>
<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>
<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;
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();
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)
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);
}
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…
}
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.
Chúng ta thử click vào New Game nào, và kết quả là:
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….. 
Thứ Ba, 29 tháng 1, 2013
Sử dụng relative layout
khi sử dụng relative layout thì sẽ có thêm các thuộc tính hay dùng như
android:layout_toRightof("[id của button]"); đặt ở bên phải
android:layout_marginLeft="40dp"; cách bên trái 40dp
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp" >
<TextView
android:id="@+id/tvTemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/temp" />
<TextView
android:id="@+id/tvHumidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tvTemperature"
android:text="@string/hum"
android:layout_marginLeft="40dp" />
<TextView
android:id="@+id/tvCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tvHumidity"
android:text="@string/cond"
android:layout_marginLeft="40dp"/>
</RelativeLayout>
scr:https://www.youtube.com/watch?v=gCsyiHjP1P0
khi sử dụng relative layout thì sẽ có thêm các thuộc tính hay dùng như
android:layout_toRightof("[id của button]"); đặt ở bên phải
android:layout_marginLeft="40dp"; cách bên trái 40dp
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp" >
<TextView
android:id="@+id/tvTemperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/temp" />
<TextView
android:id="@+id/tvHumidity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tvTemperature"
android:text="@string/hum"
android:layout_marginLeft="40dp" />
<TextView
android:id="@+id/tvCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/tvHumidity"
android:text="@string/cond"
android:layout_marginLeft="40dp"/>
</RelativeLayout>
scr:https://www.youtube.com/watch?v=gCsyiHjP1P0
set dialog in android
Vào file Android manifest thêm element Android:Them="@android:style/Theme.Dialog";
<activity
android:name=".Setting"
android:label="@string/title_activity_weather"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="ssas.Androi.SETTING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
src: https://www.youtube.com/watch?v=CqE0VZXh_kI
<activity
android:name=".Setting"
android:label="@string/title_activity_weather"
android:theme="@android:style/Theme.Dialog" >
<intent-filter>
<action android:name="ssas.Androi.SETTING" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
src: https://www.youtube.com/watch?v=CqE0VZXh_kI
Thứ Hai, 28 tháng 1, 2013
Chỉ là giấc mơ
Nothing
Một giấc mơ mà ta không muốn gặp trong giấc nồng....
Cái gì qua rùi hãy để cho nó ngủ yên, chôn thật sâu, tiếp tục với những khó khăn đang chờ ta phía trước, khi ko còn đường quay lại quá khứ....
Ngày tháng này thấy thật bận rộn, làm ta quay cuồng trong công việc,
Một giấc mơ mà ta không muốn gặp trong giấc nồng....
Cái gì qua rùi hãy để cho nó ngủ yên, chôn thật sâu, tiếp tục với những khó khăn đang chờ ta phía trước, khi ko còn đường quay lại quá khứ....
Ngày tháng này thấy thật bận rộn, làm ta quay cuồng trong công việc,
Đăng ký:
Nhận xét (Atom)

