您的位置:首页 > 编程语言

安卓开发实例代码

2016-02-05 16:39 253 查看
绘图实例:

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;

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: