- 论坛徽章:
- 0
|
1.获取窗口大小方法 /* 定义DisplayMetrics对象 */ DisplayMetrics dm = new DisplayMetrics(); /* 取得窗口属性 */ getWindowManager().getDefaultDisplay().getMetrics(dm); /* 窗口的宽度 */ int screenWidth = dm.widthPixels; /* 窗口的高度 */ int screenHeight = dm.heightPixels; 2.使用动画: (1) /* 创建Alpha动画 */ mAnimationAlpha = new AlphaAnimation(0.1f, 1.0f); /* 设置动画的时间 */ mAnimationAlpha.setDuration(3000); /* 开始播放动画 */ this.startAnimation(mAnimationAlpha);
(2) /* 创建Scale动画 */ mAnimationScale =new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); /* 设置动画的时间 */ mAnimationScale.setDuration(500); /* 开始播放动画 */ this.startAnimation(mAnimationScale);
(3) /* 创建Translate动画 */ mAnimationTranslate = new TranslateAnimation(10, 100,10, 100); /* 设置动画的时间 */ mAnimationTranslate.setDuration(1000); /* 开始播放动画 */ this.startAnimation(mAnimationTranslate);
(4) /* 创建Rotate动画 */ mAnimationRotate=new RotateAnimation(0.0f, +360.0f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF, 0.5f); /* 设置动画的时间 */ mAnimationRotate.setDuration(1000); /* 开始播放动画 */ this.startAnimation(mAnimationRotate); 3. 使用动画布局 /* 装载动画布局 */ mAnimationAlpha = AnimationUtils.loadAnimation(mContext,R.anim.alpha_animation); /* 装载动画布局 */ mAnimationScale = AnimationUtils.loadAnimation(mContext,R.anim.scale_animation); /* 装载动画布局 */ mAnimationTranslate = AnimationUtils.loadAnimation(mContext,R.anim.translate_animation); /* 装载动画布局 */ mAnimationRotate = AnimationUtils.loadAnimation(mContext,R.anim.rotate_animation); /* 开始播放动画 */ this.startAnimation(mAnimationRotate); 4.位图处理
(1)位图缩放 /* 重置mMatrix */ mMatrix.reset(); /* 设置缩放 */ mMatrix.postScale(Scale,Scale); /* 按mMatrix得旋转构建新的Bitmap */ Bitmap mBitQQ2 = Bitmap.createBitmap(mBitQQ, 0, 0, BitQQwidth,BitQQheight, mMatrix, true);
/* 绘制旋转之后的图片 */ GameView.drawImage(canvas, mBitQQ2, (320-BitQQwidth)/2, 10); (2)位图旋转 /* 重置mMatrix */ mMatrix.reset(); /* 设置旋转 */ mMatrix.setRotate(Angle); /* 按mMatrix得旋转构建新的Bitmap */ Bitmap mBitQQ2 = Bitmap.createBitmap(mBitQQ, 0, 0, BitQQwidth,BitQQheight, mMatrix, true);
/* 绘制旋转之后的图片 */ GameView.drawImage(canvas, mBitQQ2, (320-BitQQwidth)/2, 10); 5.按桢播放动画 (1)转载位图资源到对象 /* 实例化AnimationDrawable对象 */ frameAnimation = new AnimationDrawable(); /* 装载资源 */ //这里用一个循环了装载所有名字类似的资源 //如“a1.......15.png”的图片 //这个方法用处非常大 for (int i = 1; i <= 15; i++) { int id = getResources().getIdentifier("a" + i, "drawable", mContext.getPackageName()); mBitAnimation = getResources().getDrawable(id); /* 为动画添加一帧 */ //参数mBitAnimation是该帧的图片 //参数500是该帧显示的时间,按毫秒计算 frameAnimation.addFrame(mBitAnimation, 500); } /* 设置播放模式是否循环false表示循环而true表示不循环 */ frameAnimation.setOneShot( false ); /* 设置本类将要显示这个动画 */ this.setBackgroundDrawable(frameAnimation);、 (2)使用动画布局 /* 定义一个ImageView用来显示动画 */ ImageView img = new ImageView(mContext); /* 装载动画布局文件 */ img.setBackgroundResource(R.anim.frameanimation); /* 构建动画 */ frameAnimation = (AnimationDrawable) img.getBackground(); /* 设置是否循环 */ frameAnimation.setOneShot( false ); /* 设置该类显示的动画 */ this.setBackgroundDrawable(frameAnimation); 6. SD相关 /* 检测是否存在SD卡 */ if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) ;
/* 得到SD卡得路径 */ File sdPath = Environment.getExternalStorageDirectory(); 7.录制声音 * 创建录音文件 */ mRecAudioFile = File.createTempFile(strTempFile, ".amr", mRecAudioPath); /* 实例化MediaRecorder对象 */ mMediaRecorder = new MediaRecorder(); /* 设置麦克风 */ mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); /* 设置输出文件的格式 */ mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); /* 设置音频文件的编码 */ mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); /* 设置输出文件的路径 */ mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath()); /* 准备 */ mMediaRecorder.prepare(); /* 开始 */ mMediaRecorder.start(); /* 停止录音 */ mMediaRecorder.stop(); /* 释放MediaRecorder */ mMediaRecorder.release(); 8.设置和取消闹钟
创建闹钟: calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY,hourOfDay); calendar.set(Calendar.MINUTE,minute); calendar.set(Calendar.SECOND,0); calendar.set(Calendar.MILLISECOND,0); /* 建立Intent和PendingIntent,来调用目标组件 */ Intent intent = new Intent(Activity01.this, AlarmReceiver.class); PendingIntent pendingIntent=PendingIntent.getBroadcast(Activity01.this,0, intent, 0); AlarmManager am; /* 获取闹钟管理的实例 */ am = (AlarmManager)getSystemService(ALARM_SERVICE); /* 设置闹钟 */ am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); /* 设置周期闹钟,24小时周期 */ am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (10*1000), (24*60*60*1000), pendingIntent); 取消闹钟: Intent intent = new Intent(Activity01.this, AlarmReceiver.class); PendingIntent pendingIntent=PendingIntent.getBroadcast(Activity01.this,0, intent, 0); AlarmManager am; /* 获取闹钟管理的实例 */ am =(AlarmManager)getSystemService(ALARM_SERVICE); /* 取消 */ am.cancel(pendingIntent); 9.使用MediaPlayer播放音乐 (1)播放 try { /* 重置MediaPlayer */ mMediaPlayer.reset(); /* 设置要播放的文件的路径 */ mMediaPlayer.setDataSource(path); /* 准备播放 */ mMediaPlayer.prepare(); /* 开始播放 */ mMediaPlayer.start(); mMediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer arg0) { //播放完成一首之后进行下一首 nextMusic(); } }); }catch (IOException e){} (2)暂停和继续 if (mMediaPlayer.isPlaying()) { /* 暂停 */ mMediaPlayer.pause(); } else { /* 开始播放 */ mMediaPlayer.start(); } (3)停止播放 mMediaPlayer.stop(); mMediaPlayer.release();
10.设置来电铃声
//打开系统铃声设置 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); //类型为来电RINGTONE intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE); //设置显示的title intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置来电铃声"); //当设置完成之后返回到当前的Activity startActivityForResult(intent, ButtonRingtone); 11.设置闹钟铃声 //打开系统铃声设置 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); //设置铃声类型和title intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置闹铃铃声"); //当设置完成之后返回到当前的Activity startActivityForResult(intent, ButtonAlarm);
12.设置通知铃声 //打开系统铃声设置 Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); //设置铃声类型和title intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "设置通知铃声"); //当设置完成之后返回到当前的Activity startActivityForResult(intent, ButtonNotification); 13.重力感应 //通过服务得到传感器管理对象 sm = (SensorManager) MainActivity.ma.getSystemService(Service.SENSOR_SERVICE); sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);//得到一个重力传感器实例 //TYPE_ACCELEROMETER 加速度传感器(重力传感器)类型。 //TYPE_ALL 描述所有类型的传感器。 //TYPE_GYROSCOPE 陀螺仪传感器类型 //TYPE_LIGHT 光传感器类型 //TYPE_MAGNETIC_FIELD 恒定磁场传感器类型。 //TYPE_ORIENTATION 方向传感器类型。 //TYPE_PRESSURE 描述一个恒定的压力传感器类型 //TYPE_PROXIMITY 常量描述型接近传感器 //TYPE_TEMPERATURE 温度传感器类型描述 mySensorListener = new SensorEventListener() { @Override //传感器获取值发生改变时在响应此函数 public void onSensorChanged(SensorEvent event) {//备注1 //传感器获取值发生改变,在此处理 x = event.values[0]; //手机横向翻滚 //x>0 说明当前手机左翻 x<0右翻 y = event.values[1]; //手机纵向翻滚 //y>0 说明当前手机下翻 y<0上翻 z = event.values[2]; //屏幕的朝向 //z>0 手机屏幕朝上 z<0 手机屏幕朝下 arc_x -= x;//备注2 arc_y += y; } @Override //传感器的精度发生改变时响应此函数 public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub
} }; sm.registerListener(mySensorListener, sensor, SensorManager.SENSOR_DELAY_GAME); //第一个参数是传感器监听器,第二个是需要监听的传感实例 //最后一个参数是监听的传感器速率类型: 一共一下四种形式 //SENSOR_DELAY_NORMAL 正常 //SENSOR_DELAY_UI 适合界面 //SENSOR_DELAY_GAME 适合游戏 (我们必须选这个呀 哇哈哈~) //SENSOR_DELAY_FASTEST 最快
14. Error: Unable to execute dex: wrapper was not properly loaded first 解决方法
运行时 报错 Your project contains error(s),please fix them before running your application.
控制台显示: Unable to execute dex: wrapper was not properly loaded first Conversion to Dalvik format failed: Unable to execute dex: wrapper was not properly loaded first
解决方案: 找到Eclipse目录下eclipse.ini文件,将最后两句改为 -Xms128m -Xmx512m 重启eclipse 然后project->clean一下就OK了
15.下载文件
URL url = new URL(mUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(20 * 1000); connection.setRequestMethod("GET"); File file = new File("/mnt/sdcard/test.jpeg"); if (!file.exists()) { file.createNewFile(); } OutputStream out = new FileOutputStream(file); int len = 0; InputStream instream = connection.getInputStream(); while ((len = instream.read()) != -1) { out.write(len); } out.flush(); instream.close(); out.close();
|
|