Lập trình Android: Xác định vị trí doc - Pdf 21

Trung tâm Tin học – ĐH KHTN
Xác định vị trí con trỏ
Trong các trò chơi 2d (điển hình như mario) nổi tiếng từ rất lâu thì việc cực kỳ quan
trọng của người lập trình là phải xác định vị trí của nhân vật. Trong bài viết này mình sẽ
tạo 1 ứng dụng xác định vị trí khi bạn nhấn các button lên,xuống,trái,phải. Ứng dụng
này giả sử bạn đang điều khiển con vua ở trong bàn cờ vua, bạn đang ở vị trí chính
giữa bàn cờ. Mặc định vị trí của bạn là center (chính giữa), và khi bạn bấm các button
điều khiển thì con vua sẽ di chuyển theo sự điều khiển của bạn.
Đầu tiên bạn tạo 1 Project như sau:
Project name: ContentSlider
Build Target: Android 2.3.3
Application name: ContentSlider
Activity: ContentSlider
Package name: com.paad.contentslider
Tiếp theo vbạn tao folder anim trong folder res. Folder này giữ các file XML xác định
cách thức di chuyển của bạn. Khi đã tạo xong, bạn tạo tiếp 8 file XML đại diện cho 8 ô
di chuyển của bạn.
+ slide_bottom_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromYDelta="-100%p"
android:toYDelta="0"
android:duration="700"
/>
</set>
+ slide_bottom_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromYDelta="0"

android:duration="700"
/>
</set>
+ slide_right_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
Lập trình Android – http://laptrinhdidong.vn Page 2
Trung tâm Tin học – ĐH KHTN
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="700"
/>
</set>
+ slide_top_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromYDelta="100%p"
android:toYDelta="0"
android:duration="700"
/>
</set>
+ slide_top_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate
android:fromYDelta="0"
android:toYDelta="-100%p"
android:duration="700"

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
slideOutLeft = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
slideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
slideOutRight = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
slideInTop = AnimationUtils.loadAnimation(this, R.anim.slide_top_in);
slideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_top_out);
slideInBottom = AnimationUtils.loadAnimation(this, R.anim.slide_bottom_in);
slideOutBottom = AnimationUtils.loadAnimation(this, R.anim.slide_bottom_out);
myTextView = (TextView)findViewById(R.id.myTextView);
}
private void applyAnimation(Animation _out, Animation _in, String _newText) {
final String text = _newText;
final Animation in = _in;
// Ensure the text stays out of screen when the slide-out
// animation has completed.
_out.setFillAfter(true );
// Create a listener to wait for the slide-out
// animation to complete.
_out.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation _animation) {
// Change the text
myTextView.setText(text);
// Slide it back in to view
myTextView.startAnimation(in);
Lập trình Android – http://laptrinhdidong.vn Page 4
Trung tâm Tin học – ĐH KHTN
}
public void onAnimationRepeat(Animation _animation) {}

// Ie. Tilting right should make left appear.
if (_directionPressed == TextPosition.Bottom)
newPosValue = currentValue - 3;
else if (_directionPressed == TextPosition.Top)
newPosValue = currentValue + 3;
else if (_directionPressed == TextPosition.Right) {
if (currentValue % 3 != 0)
newPosValue = currentValue - 1;
}
else if (_directionPressed == TextPosition.Left) {
if ((currentValue+1) % 3 != 0)
newPosValue = currentValue + 1;
}
Lập trình Android – http://laptrinhdidong.vn Page 5
Trung tâm Tin học – ĐH KHTN
if (newPosValue != currentValue &&
newPosValue > -1 &&
newPosValue < 9){
newPosition = TextPosition.values()[newPosValue];
applyAnimation(in, out, newPosition.toString());
textPosition = newPosition;
}
}

@Override
public boolean onKeyDown(int _keyCode, KeyEvent _event) {
if (super.onKeyDown(_keyCode, _event))
return true;
if (_event.getAction() == KeyEvent.ACTION_DOWN){
switch (_keyCode) {


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status