Game đếm sốProject name: GameDemSoCuaDac Build Target: Android 2.3.3 Application name: GameDemSoCuaDac Package name: com.dac Create Activity: GameDemSoCuaDacActivity Trong file main.xml
Trang 1Game đếm số
Project name: GameDemSoCuaDac
Build Target: Android 2.3.3
Application name: GameDemSoCuaDac
Package name: com.dac
Create Activity: GameDemSoCuaDacActivity
Trong file main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="335px">
<ImageSwitcher android:id="@+id/image_switcher"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:clickable="true" />
</LinearLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="bottom"
android:paddingBottom="3px"
<TableRow android:id="@+id/tableRow1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<! android:background="@drawable/android_button" >
<Button android:gravity="center"
android:layout_marginRight="10px"
Trang 2android:layout_marginLeft="10px"
android:text="1"
android:textSize="20px"
android:id="@+id/button1"
android:layout_width="45px"
android:layout_height="45px"></Button>
<! android:background="@drawable/android_btn2" > <Button android:gravity="center"
android:layout_marginRight="10px"
android:text="2"
android:textSize="20px"
android:id="@+id/button2"
android:layout_width="45px"
android:layout_height="45px"></Button>
<! android:background="@drawable/android_btn3" > <Button android:gravity="center"
android:layout_marginRight="10px"
android:id="@+id/button3"
android:text="3"
android:textSize="20px"
android:layout_width="45px"
android:layout_height="45px"></Button>
<! android:background="@drawable/android_btn4" > <Button android:layout_width="45px"
android:layout_marginRight="10px"
android:layout_height="45px"
android:text="4"
android:textSize="20px"
android:id="@+id/button4"
android:gravity="center"></Button>
<! android:background="@drawable/android_btn5"" > <Button android:layout_width="45px"
android:layout_marginRight="10px"
android:layout_height="45px"
android:text="5"
android:textSize="20px"
android:id="@+id/button5"
android:gravity="center"></Button>
</TableRow>
<TableRow android:id="@+id/tableRow2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<! android:background="@drawable/android_btn6" > <Button android:gravity="center"
android:layout_marginRight="10px"
android:layout_marginLeft="10px"
android:id="@+id/button6"
android:text="6"
android:textSize="20px"
android:layout_width="45px"
Trang 3android:layout_height="45px"></Button> <! android:background="@drawable/android_btn7" > <Button android:gravity="center"
android:layout_marginRight="10px"
android:text="7"
android:textSize="20px"
android:id="@+id/button7"
android:layout_width="45px"
android:layout_height="45px"></Button>
<! android:background="@drawable/android_btn8" > <Button android:gravity="center"
android:layout_marginRight="10px"
android:id="@+id/button8"
android:text="8"
android:textSize="20px"
android:layout_width="45px"
android:layout_height="45px"></Button>
<! android:background="@drawable/android_btn9" > <Button android:layout_width="45px"
android:layout_marginRight="10px"
android:text="9"
android:textSize="20px"
android:layout_height="45px"
android:id="@+id/button9"
android:gravity="center"></Button>
<! android:background="@drawable/android_btn10" > <Button android:layout_width="45px"
android:layout_marginRight="10px"
android:text="10"
android:textSize="10px"
android:layout_height="45px"
android:id="@+id/button10"
android:gravity="center"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
Trong file Activity:
package com.dac;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
Trang 4import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class GameDemSoCuaDacActivity extends Activity {
Button button1, button2, button3, button4, button5;
Button button6, button7, button8, button9, button10;
private static final int Quaylai = -1;
private static final int Tiep =1;
private MediaPlayer mp;
private SharedPreferences preference;
private static final String current_position = "current";
private static final int start_position = 0;
private static final int MIN_SWIPE_WIDTH = 50;
private static final String Tag="TCGB";
private int[] toado = {0,0};
private static int[] pictureIds = {
R.drawable.dem1, R.drawable.dem2, R.drawable.dem3, R.drawable.dem4,
R.drawable.dem5,
R.drawable.dem6, R.drawable.dem7, R.drawable.dem8, R.drawable.dem9,
R.drawable.dem10
};
private int[] soundIds =
{R.raw.caydan,R.raw.conca,R.raw.ngoisao,R.raw.conbuom,
R.raw.conbo,R.raw.caichuong,R.raw.caibanh,R.raw.traitim,R.raw.mayanh,R.raw.caiphao}; /** Called when the activity is first created */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
button6 = (Button) findViewById(R.id.button6);
button7 = (Button) findViewById(R.id.button7);
button8 = (Button) findViewById(R.id.button8);
Trang 5button9 = (Button) findViewById(R.id.button9);
button10 = (Button) findViewById(R.id.button10);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem1) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem2) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem3) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Trang 6int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem4) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem5) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem6) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem7) {
playDung();
}
else
{
Trang 7} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem8) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem8) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem9) {
playDung();
}
else
{
playSai();
} }
});
@Override
public void onClick(View v) {
Trang 8// TODO Auto-generated method stub
int t = getCurrentPos();
if(pictureIds[t] == R.drawable.dem10) {
playDung();
}
else
{
playSai();
} }
});
preference = getPreferences(MODE_PRIVATE);
setStartPosition();
ImageSwitcher i = (ImageSwitcher)findViewById(R.id.image_switcher); i.setFactory(new MyImageSwitcherFactory());
i.setOnTouchListener(o);
displayCurrentImange();
playCurrentSound();
}
private int getCurrentPos()
{
return preference.getInt(current_position, 0);
}
private void playDung()
{
stopPlaySound();
}
private void playSai()
{
stopPlaySound();
}
private void stopPlaySound()
{
if(mp!=null)
{
}
}
private void setStartPosition()
{
SharedPreferences.Editor e = preference.edit();
e.putInt(current_position, start_position);
e.commit();
}
Trang 9private void updateCureentPosition(int d)
{
int t = getCurrentPos();
int updatePosition = (pictureIds.length + t +d) % pictureIds.length; Editor e = preference.edit();
e.putInt(current_position, updatePosition);
e.commit();
}
private void updateAnimation(int d)
{
ImageSwitcher i = (ImageSwitcher)findViewById(R.id.image_switcher);
switch (d) {
case Quaylai:
i.setInAnimation(this, R.anim.slide_in_left);
i.setOutAnimation(this, R.anim.slide_out_right);
break;
case Tiep:
i.setInAnimation(this, R.anim.slide_in_right);
i.setOutAnimation(this, R.anim.slide_out_left);
break;
default:
break; }
}
private void playCurrentSound()
{
stopPlaySound();
int t = getCurrentPos();
int currentSound = soundIds[t];
mp.start();
}
private void displayCurrentImange()
{
int t = getCurrentPos();
ImageSwitcher i = (ImageSwitcher)findViewById(R.id.image_switcher); i.setImageResource(pictureIds[t]);
}
OnTouchListener o = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true; }
else {
if(event.getAction() == MotionEvent.ACTION_UP) {
Trang 10toado[1] =(int)event.getX();
if(toado[1] - toado[0] > MIN_SWIPE_WIDTH) {
Log.d(Tag, "User swiped right");
updateCureentPosition(Quaylai);
updateAnimation(Quaylai);
stopPlaySound();
playCurrentSound();
displayCurrentImange();
}
else if (toado[0] - toado[1] > MIN_SWIPE_WIDTH) {
Log.d(Tag, "User swiped left");
updateCureentPosition(Tiep);
updateAnimation(Tiep);
stopPlaySound();
playCurrentSound();
displayCurrentImange();
}
return true; }
return false; }
} };
private class MyImageSwitcherFactory implements ViewFactory
{
@Override
public View makeView() {
// TODO Auto-generated method stub ImageView i = new ImageView(GameDemSoCuaDacActivity.this); i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new
ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return i;
} }
}
Ngoài ra còn 1 số file hình ảnh, âm thanh khác các bạn download Project để tìm hiểu thêm.
Trang 11Mọi ý kiến đóng góp các bạn vui lòng gữi bài về forum: www.forum.laptrinhdidong.vn Rất mong nhận được sự phản hồi từ các bạn.