Trung tâm Tin học – ĐH KHTN 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: <?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" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android: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" Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN android: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; Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN import 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); Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN button9 = (Button) findViewById(R.id.button9); button10 = (Button) findViewById(R.id.button10); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem1) { playDung(); } else { playSai(); } } }); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem2) { playDung(); } else { playSai(); } } }); button3.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem3) { playDung(); } else { playSai(); } } }); button4.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Lập trình Android – http://laptrinhdidong.vn Page 5 Trung tâm Tin học – ĐH KHTN int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem4) { playDung(); } else { playSai(); } } }); button5.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem5) { playDung(); } else { playSai(); } } }); button6.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem6) { playDung(); } else { playSai(); } } }); button7.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem7) { playDung(); } else { Lập trình Android – http://laptrinhdidong.vn Page 6 Trung tâm Tin học – ĐH KHTN playSai(); } } }); button8.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem8) { playDung(); } else { playSai(); } } }); button8.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem8) { playDung(); } else { playSai(); } } }); button9.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int t = getCurrentPos(); if(pictureIds[t] == R.drawable.dem9) { playDung(); } else { playSai(); } } }); button10.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Lập trình Android – http://laptrinhdidong.vn Page 7 Trung tâm Tin học – ĐH KHTN // 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(); mp = MediaPlayer.create(this, R.raw.correct); mp.start(); } private void playSai() { stopPlaySound(); mp = MediaPlayer.create(this, R.raw.incorrect); mp.start(); } private void stopPlaySound() { if(mp!=null) { mp.release(); } } private void setStartPosition() { SharedPreferences.Editor e = preference.edit(); e.putInt(current_position, start_position); e.commit(); } Lập trình Android – http://laptrinhdidong.vn Page 8 Trung tâm Tin học – ĐH KHTN private 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 = MediaPlayer.create(this, currentSound); 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) { toado[0] = (int)event.getX(); return true; } else { if(event.getAction() == MotionEvent.ACTION_UP) { Lập trình Android – http://laptrinhdidong.vn Page 9 Trung tâm Tin học – ĐH KHTN toado[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. Lập trình Android – http://laptrinhdidong.vn Page 10 [...]...Trung tâm Tin học – ĐH KHTN Mọ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 Lập trình Android – http://laptrinhdidong.vn Page 11 . Trung tâm Tin học – ĐH KHTN Game đếm số Project name: GameDemSoCuaDac Build Target: Android 2.3.3 Application name: GameDemSoCuaDac Package name: com.dac Create Activity: GameDemSoCuaDacActivity Trong. <Button android:gravity="center" android:layout_marginRight="10px" android:id="@+id/button3" android:text="3" android:textSize="20px" android:layout_width="45px". <Button android:gravity="center" android:layout_marginRight="10px" android:id="@+id/button8" android:text="8" android:textSize="20px" android:layout_width="45px"