炫舞吧 android 游戏开发

时间:2015-05-26 04:27:39
【文件属性】:

文件名称:炫舞吧 android 游戏开发

文件大小:10.43MB

文件格式:APK

更新时间:2015-05-26 04:27:39

android 游戏开发 炫舞 单机

模仿qq炫舞游戏做出的单机版游戏 package cn.itcast.game; import java.io.InputStream; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.SoundPool; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.view.SurfaceHolder.Callback; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import android.widget.RadioGroup.OnCheckedChangeListener; public class MainActivity extends Activity { AnimView mAnimView = null; public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // 检测屏幕的方向:纵向或横向 if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { // 当前为横屏, 在此处添加额外的处理代码 } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { // 当前为竖屏, 在此处添加额外的处理代码 } } public void onBackPressed() { // TODO Auto-generated method stub super.onBackPressed(); android.os.Process.killProcess(android.os.Process.myPid()); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 强制为横屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); Display display = getWindowManager().getDefaultDisplay(); // 显示自定义的游戏View mAnimView = new AnimView(this, display.getWidth(), display.getHeight()); setContentView(mAnimView); } public boolean onTouchEvent(MotionEvent event) { // 获得触摸的坐标 int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mAnimView.UpdateEvent(x, y); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return false; } public class AnimView extends SurfaceView implements Callback, Runnable { private Paint mpaint = null; private Bitmap bitmap = null; private Context mContext = null; private Canvas mCanvas = null; private boolean mIsRunning = false; private Thread mthread = null; private SurfaceHolder mSurfaceHolder = null; private Bitmap bitmap_main_1 = null; private Bitmap bitmap_main_2 = null; private Bitmap bitmap_main_3 = null; private Bitmap bitmap_main_4 = null; private int temp = 0; private int bitmap_main_1_Width = 0; private int bitmap_main_1_Height = 0; private int bitmap_main_2_Width = 0; private int bitmap_main_2_Height = 0; private int bitmap_main_3_Width = 0; private int bitmap_main_3_Height = 0; private int bitmap_main_4_Width = 0; private int bitmap_main_4_Height = 0; private MediaPlayer mMediaPlayer = null; private SoundPool mSoundPool = null; private int button_sound = 0; private Bitmap bitmap_up = null; // 屏幕尺寸 private int mScreenWidth = 0; // private int mScreenHeight = 0; // 炫舞吧 图片尺寸 private int bitmap_up_width = 0; private Handler handler = new Handler() { public void handleMessage(Message msg) { try { Log.e("MainActivity", "wenxi11111"); dialog_show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; private RadioGroup m_RadioGroup = null; private RadioButton m_Radio2 = null; private RadioButton m_Radio1 = null; private RadioButton m_Radio3 = null; protected int choose_music = 1; protected int music_play = 0; public int start_music = 0; protected int now_checkid = 0; public AnimView(Context context, int width, int height) { super(context); mScreenWidth = width; // mScreenHeight = height; mContext = context; mpaint = new Paint(); /** 获取mSurfaceHolder **/ mSurfaceHolder = getHolder(); mSurfaceHolder.addCallback(this); init(); } public void dialog_show() { Log.e("MainActivity", "wenxi2222222"); AlertDialog.Builder builder = new AlertDialog.Builder(mContext); Log.e("MainActivity", "wenxi2222223"); LayoutInflater inflater = LayoutInflater.from(mContext); Log.e("MainActivity", "wenxi2222224"); final View textEntryView = inflater.inflate(R.layout.music, null); Log.e("MainActivity", "wenxi2222225"); builder.setIcon(R.drawable.xiyangyang_1); builder.setTitle("请选择背景音乐"); builder.setCancelable(false); builder.setView(textEntryView); Log.e("MainActivity", "wenxi2222226"); m_RadioGroup = (RadioGroup) textEntryView .findViewById(R.id.RadioGroup01); m_Radio1 = (RadioButton) textEntryView .findViewById(R.id.RadioButton1); m_Radio2 = (RadioButton) textEntryView .findViewById(R.id.RadioButton2); m_Radio3 = (RadioButton) textEntryView .findViewById(R.id.RadioButton3); // now_checkid = m_RadioGroup.getCheckedRadioButtonId(); m_RadioGroup.check(now_checkid); m_RadioGroup .setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == m_Radio1.getId()) { Log.e("MainActivity", Integer .toString(checkedId)); Log.e("MainActivity", Integer.toString(m_Radio1 .getId())); choose_music = 1; Toast toast = Toast.makeText(mContext, "你选择了music1", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_HORIZONTAL, toast.getXOffset(), toast.getYOffset()); toast.show(); } else if (checkedId == m_Radio2.getId()) { choose_music = 2; Toast toast = Toast.makeText(mContext, "你选择了music2", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_HORIZONTAL, toast.getXOffset(), toast.getYOffset()); toast.show(); } else if (checkedId == m_Radio3.getId()) { choose_music = 3; Toast toast = Toast.makeText(mContext, "你选择了music3", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER_HORIZONTAL, toast.getXOffset(), toast.getYOffset()); toast.show(); } now_checkid = checkedId; } }); builder.setPositiveButton("保存", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel();// 取消弹出框 if (choose_music == 1) { mMediaPlayer.release(); /** 创建MediaPlayer对象 **/ mMediaPlayer = MediaPlayer.create(mContext, R.raw.bgmusic_1); /** 设置为循环播放 **/ mMediaPlayer.setLooping(true); mMediaPlayer.start(); } else if (choose_music == 2) { mMediaPlayer.release(); /** 创建MediaPlayer对象 **/ mMediaPlayer = MediaPlayer.create(mContext, R.raw.bgmusic_2); /** 设置为循环播放 **/ mMediaPlayer.setLooping(true); mMediaPlayer.start(); } else if (choose_music == 3) { mMediaPlayer.release(); mMediaPlayer = MediaPlayer.create(mContext, R.raw.bgmusic_3); /** 设置为循环播放 **/ mMediaPlayer.setLooping(true); mMediaPlayer.start(); } } }); builder.setNegativeButton("放弃", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel();// 取消弹出框 choose_music = start_music; } }); builder.create().show(); } private void init() { // 主界面静态图 bitmap = ReadBitmap(mContext, R.drawable.main_2_picture); bitmap_up = ReadBitmap(mContext, R.drawable.xuanwuba); bitmap_up_width = bitmap_up.getWidth(); bitmap_main_1 = ReadBitmap(mContext, R.drawable.main_1); bitmap_main_2 = ReadBitmap(mContext, R.drawable.main_2); bitmap_main_3 = ReadBitmap(mContext, R.drawable.main_3); bitmap_main_4 = ReadBitmap(mContext, R.drawable.main_4); bitmap_main_1_Width = bitmap_main_1.getWidth(); bitmap_main_1_Height = bitmap_main_1.getHeight(); bitmap_main_2_Width = bitmap_main_2.getWidth(); bitmap_main_2_Height = bitmap_main_2.getHeight(); bitmap_main_3_Width = bitmap_main_3.getWidth(); bitmap_main_3_Height = bitmap_main_3.getHeight(); bitmap_main_4_Width = bitmap_main_4.getWidth(); bitmap_main_4_Height = bitmap_main_4.getHeight(); // 主界面一个动态图显示 // mAnimation = new Animation(mContext,new int // []{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i,R.drawable.j},true); // mAnimation = new Animation(mContext,new int // []{R.drawable.a_1,R.drawable.a_2,R.drawable.a_3,R.drawable.a_4,R.drawable.a_5,R.drawable.a_6,R.drawable.a_7,R.drawable.a_8,R.drawable.a_9,R.drawable.a_10,R.drawable.a_11,R.drawable.a_12,R.drawable.a_13,R.drawable.a_14,R.drawable.a_15,R.drawable.a_16,R.drawable.a_17,R.drawable.a_18,R.drawable.a_19,R.drawable.a_20},true); // mAnimation = new Animation(mContext,new int // []{R.drawable.b_1,R.drawable.b_2,R.drawable.b_3,R.drawable.b_4,R.drawable.b_5,R.drawable.b_6,R.drawable.b_7,R.drawable.b_8,R.drawable.b_9,R.drawable.b_10, // R.drawable.b_11,R.drawable.b_12,R.drawable.b_14,R.drawable.b_15,R.drawable.b_16,R.drawable.b_17,R.drawable.b_18,R.drawable.b_19,R.drawable.b_20, // R.drawable.b_21,R.drawable.b_22,R.drawable.b_23,R.drawable.b_24,R.drawable.b_25,R.drawable.b_26,R.drawable.b_27,R.drawable.b_28,R.drawable.b_29,R.drawable.b_30, // R.drawable.b_31,R.drawable.b_32,R.drawable.b_33 // },true); /** 创建MediaPlayer对象 **/ mMediaPlayer = MediaPlayer.create(mContext, R.raw.bgmusic_2); /** 设置为循环播放 **/ mMediaPlayer.setLooping(true); /** 创建一个声音播放池 **/ // 参数1为声音池同时播放的流的最大数量 // 参数2为播放流的类型 // 参数3为音乐播放效果 setmSoundPool(new SoundPool(2, AudioManager.STREAM_MUSIC, 100)); } public void UpdateEvent(int x, int y) { temp = IsClick(x, y); switch (temp) { case 1: button_sound++; Intent intent = new Intent(MainActivity.this, MenuActivity.class); startActivity(intent); break; case 2: button_sound++; if (button_sound % 2 == 1) { mMediaPlayer.setVolume(0, 0); } else { mMediaPlayer.setVolume(2, 2); } break; case 3: handler.sendMessage(new Message()); start_music = choose_music; mIsRunning = false; break; case 4: mMediaPlayer.release(); android.os.Process.killProcess(android.os.Process.myPid()); break; } } private int IsClick(int x, int y) { int a = 0; if (x >= 170 + 20 && x <= 170 + bitmap_main_1_Width - 20 && y >= 110 + 20 && y <= 110 + bitmap_main_1_Height - 20) a = 1; else if (x >= 170 + 35 && x <= 170 + bitmap_main_2_Width - 35 && y >= 160 + 20 && y <= 160 + bitmap_main_2_Height - 20) a = 2; else if (x >= 170 + 20 && x <= 170 + bitmap_main_3_Width - 40 && y >= 210 + 20 && y <= 210 + bitmap_main_3_Height - 20) a = 3; else if (x >= 170 + 40 && x <= 170 + bitmap_main_4_Width - 35 && y >= 260 + 20 && y <= 260 + bitmap_main_4_Height - 20) a = 4; return a; } private void Draw() { mCanvas.drawBitmap(bitmap, 0, 0, mpaint); mCanvas.drawBitmap(bitmap_up, (mScreenWidth - bitmap_up_width) / 2, 5, mpaint); mCanvas.drawBitmap(bitmap_main_1, 170, 110, mpaint); mCanvas.drawBitmap(bitmap_main_2, 170, 160, mpaint); mCanvas.drawBitmap(bitmap_main_3, 170, 210, mpaint); mCanvas.drawBitmap(bitmap_main_4, 170, 260, mpaint); mMediaPlayer.start(); mMediaPlayer.setVolume(2, 2); } private Bitmap ReadBitmap(Context context, int id) { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inPreferredConfig = Bitmap.Config.RGB_565; opt.inPurgeable = true; opt.inInputShareable = true; InputStream is = context.getResources().openRawResource(id); return BitmapFactory.decodeStream(is, null, opt); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub mIsRunning = true; mthread = new Thread(this); mthread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub mIsRunning = false; } @Override public void run() { while (mIsRunning) { synchronized (mSurfaceHolder) { mCanvas = mSurfaceHolder.lockCanvas(); Draw(); mSurfaceHolder.unlockCanvasAndPost(mCanvas); } try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } } } public void setmSoundPool(SoundPool mSoundPool) { this.mSoundPool = mSoundPool; } public SoundPool getmSoundPool() { return mSoundPool; } } }


网友评论

  • 没源码.怎么学
  • 运行有问题,不过还可以
  • 没源码,哎,可悲
  • 不是源代码来的
  • 上当了,不是源码啊
  • 是个apk, 不是源代码
  • 上当了,不是源码啊
  • 附件不是源代码,主描述的好象有缺
  • 感觉还好,不过为啥是apk呢
  • 没源码啊,骗子!
  • 运行有报错.....不过还可以
  • 不是源代码,哎
  • APK 不是源代码 没用
  • 附件不是源代码,主描述的好象有缺失
  • 额,这个..下了好半天,是个apk...不能用到源码啊
  • 是apk,不是源码
  • 运行有报错.....不过还可以
  • 这个也是个apk,服气。。。。。。。。
  • APK 不是源代码 没用