安卓开发实例代码

绘图实例:
public class GameView extends View
{
 /* 定义Alpha动画 */
 private Animation mAnimationAlpha  = null;
 
 /* 定义Scale动画 */
 private Animation mAnimationScale  = null;
 
 /* 定义Translate动画 */
 private Animation mAnimationTranslate = null;
 
 /* 定义Rotate动画 */
 private Animation mAnimationRotate = null;
 
 /* 定义Bitmap对象 */
 Bitmap    mBitQQ    = null;
 private int c=1;
 Context mContext = null;
 public GameView(Context context)
 {
  super(context);
  
  mContext = context;
  
  /* 装载资源 */
  mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.qq)).getBitmap();
 }
 
 public void onDraw(Canvas canvas)
 {
  super.onDraw(canvas);
  
  /* 绘制图片 */
  canvas.drawBitmap(mBitQQ, 0, 0, null);
 }
 
 public boolean onKeyUp(int keyCode, KeyEvent event)
 {
  switch ( keyCode )
  {
  case KeyEvent.KEYCODE_DPAD_UP:
   /* 装载动画布局 */
   mAnimationAlpha = AnimationUtils.loadAnimation(mContext,R.anim.alpha_animation);
   /* 开始播放动画 */
   this.startAnimation(mAnimationAlpha);
   break;
  case KeyEvent.KEYCODE_DPAD_DOWN:
   /* 装载动画布局 */
   mAnimationScale = AnimationUtils.loadAnimation(mContext,R.anim.scale_animation);
   /* 开始播放动画 */
   this.startAnimation(mAnimationScale);
   break;
  case KeyEvent.KEYCODE_DPAD_LEFT:
   /* 装载动画布局 */
   mAnimationTranslate = AnimationUtils.loadAnimation(mContext,R.anim.translate_animation);
   /* 开始播放动画 */
   this.startAnimation(mAnimationTranslate);
   break;
  case KeyEvent.KEYCODE_DPAD_RIGHT:
   /* 装载动画布局 */
   mAnimationRotate = AnimationUtils.loadAnimation(mContext,R.anim.rotate_animation);
   /* 开始播放动画 */
   this.startAnimation(mAnimationRotate);
   break;
  }
  return true;
 }
 
 // 触笔事件
  public boolean onTouchEvent(MotionEvent event)
  {
   switch(c)
   {
    case 1:
     mAnimationAlpha = AnimationUtils.loadAnimation(mContext,R.anim.alpha_animation);
     /* 开始播放动画 */
     this.startAnimation(mAnimationAlpha);
     c++;
     break;
    case 2:
     mAnimationScale = AnimationUtils.loadAnimation(mContext,R.anim.scale_animation);
     /* 开始播放动画 */
     this.startAnimation(mAnimationScale);
     c++;
     break;
    case 3:
     mAnimationTranslate = AnimationUtils.loadAnimation(mContext,R.anim.translate_animation);
     /* 开始播放动画 */
     this.startAnimation(mAnimationTranslate);
     c++;
     break;
    case 4:
     mAnimationRotate = AnimationUtils.loadAnimation(mContext,R.anim.rotate_animation);
     /* 开始播放动画 */
     this.startAnimation(mAnimationRotate);
     c=1;
     break;    
   }
   return true;
  }
}

媒体播放实例:
public class Activity01 extends Activity implements OnClickListener
{
 private Button button1,button2;
 private MIDIPlayer mMIDIPlayer = null;
 private boolean  mbMusic  = false;
 private TextView mTextView = null;
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button1 = (Button)findViewById(R.id.button1);
  button2 = (Button)findViewById(R.id.button2);
  button1.setOnClickListener(this);
  button2.setOnClickListener(this);
  
  mTextView = (TextView) this.findViewById(R.id.TextView01);
  mMIDIPlayer = new MIDIPlayer(this);
  /* 读取文件数据  */
  load();
  if (mbMusic)
  {
   mTextView.setText("当前音乐状态:开");
   mbMusic = true;
   mMIDIPlayer.PlayMusic();
  }
  else
  {
   mTextView.setText("当前音乐状态:关");
  }
 }
 
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  if(v.getId() == R.id.button1){
   mTextView.setText("当前音乐状态:开");
   mbMusic = true;
   mMIDIPlayer.PlayMusic();
  }
  else if(v.getId() == R.id.button2){
   mTextView.setText("当前音乐状态:关");
   mbMusic = false;
   mMIDIPlayer.FreeMusic();
  }
 }
 public boolean onKeyUp(int keyCode, KeyEvent event)
 {
  switch (keyCode)
  {
   case KeyEvent.KEYCODE_DPAD_UP:
    mTextView.setText("当前音乐状态:开");
    mbMusic = true;
    mMIDIPlayer.PlayMusic();
    break;
   case KeyEvent.KEYCODE_DPAD_DOWN:
    mTextView.setText("当前音乐状态:关");
    mbMusic = false;
    mMIDIPlayer.FreeMusic();
    break;
  }
  return true;
 }
 public boolean onKeyDown(int keyCode, KeyEvent event)
 {
  if (keyCode == KeyEvent.KEYCODE_BACK)
  {
   /* 退出应用程序时保存数据 */
   save();
   
   if ( mbMusic )
   {
    mMIDIPlayer.FreeMusic();
   }
   this.finish();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }
 
 /* 装载、读取数据 */
 void load()
 {
  /* 构建Properties对对象 */
  Properties properties = new Properties();
  try
  {
   /* 开发文件 */
   FileInputStream stream = this.openFileInput("music.cfg");
   /* 读取文件内容 */
   properties.load(stream);
  }
  catch (FileNotFoundException e)
  {
   return;
  }
  catch (IOException e)
  {
   return;
  }
  /* 取得数据 */
  mbMusic = Boolean.valueOf(properties.get("bmusic").toString());
 }
 
 /* 保存数据 */
 boolean save()
 {
  Properties properties = new Properties();
  
  /* 将数据打包成Properties */
  properties.put("bmusic", String.valueOf(mbMusic));
  try
  {
   FileOutputStream stream = this.openFileOutput("music.cfg", Context.MODE_WORLD_WRITEABLE);
   
   /* 将打包好的数据写入文件中 */
   properties.store(stream, "");
  }
  catch (FileNotFoundException e)
  {
   return false;
  }
  catch (IOException e)
  {
   return false;
  }
  return true;
 }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
60个Android开发精典案例 Android软件源码: 2-1(Activity生命周期) 3-1(Button与点击监听器) 3-10-1(列表之ArrayAdapter适配) 3-10-2(列表之SimpleAdapter适配) 3-11(Dialog对话框) 3-12-5(Activity跳转与操作) 3-12-6(横竖屏切换处理) 3-3(ImageButton图片按钮) 3-4(EditText文本编辑) 3-5(CheckBox与监听) 3-6(RadioButton与监听) 3-7(ProgressBar进度条) 3-8(SeekBar 拖动条) 3-9(Tab分页式菜单) 4-10(可视区域) 4-11-1(Animation动画) 4-11-2-1(动态位图) 4-11-2-2(帧动画) 4-11-2-3(剪切图动画) 4-13(操作游戏主角) 4-14-1(矩形碰撞) 4-14-2(圆形碰撞) 4-14-4(多矩形碰撞) 4-14-5(Region碰撞检测) 4-15-1(MediaPlayer音乐) 4-15-2(SoundPool音效) 4-16-1(游戏保存之SharedPreference) 4-16-2(游戏保存之Stream) 4-3(View游戏框架) 4-4(SurfaceView游戏框架) 4-7-1(贝塞尔曲线) 4-7-2(Canvas画布) 4-8(Paint画笔) 4-9(Bitmap位图渲染与操作) 5-1(飞行射击游戏实战) 6-1(360°平滑游戏摇杆) 6-10-1(Socket协议) 6-10-2(Http协议) 6-11(本地化与国际化) 6-2(多触点缩放位图) 6-3(触屏手势识别) 6-4(加速度传感器) 6-5(9patch工具)] 6-6(截屏) 6-8(游戏视图与系统组件) 6-9(蓝牙对战游戏) 7-10-1(遍历Body) 7-10-2(Body的m_userData) 7-11(为Body施加力) 7-12(Body碰撞监听) 7-13-1(距离关节) 7-13-2(旋转关节) 7-13-3(齿轮关节) 7-13-4(滑轮关节) 7-13-5-1(通过移动关节移动Body) 7-13-5-2(通过移动关节绑定两个Body动作) 7-13-6(鼠标关节-拖拽Body) 7-14(AABB获取Body) 7-4(Box2d物理世界) 7-5在物理世界中添加矩形) 7-7(添加自定义多边形) 7-9(在物理世界中添加圆形) 8-1(迷宫小球) 8-2(堆房子)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三江831

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值