Android 常用知识总结

来源:互联网 发布:手机淘宝用银行卡支付 编辑:程序博客网 时间:2024/05/22 01:48

1、打开raw资源文件

AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);

2、打开assert里的文件

InputStream inputStream = getAssets().open("tangyan.jpg");

3、mediaPlayer的初始化

 // 设置声道流格式为音乐setVolumeControlStream(AudioManager.STREAM_MUSIC);MediaPlayer mediaPlayer = new MediaPlayer();mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置声音完成后监听mediaPlayer.setOnCompletionListener(beepListener);AssetFileDescriptor file = getResources().openRawResourceFd(        R.raw.beep);try {    mediaPlayer.setDataSource(file.getFileDescriptor(),            file.getStartOffset(), file.getLength());    file.close();    mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);//设置音量    mediaPlayer.prepare();} catch (IOException e) {    mediaPlayer = null;}/** * 当声音播放完成后,播放点回到原点 */private final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() {    public void onCompletion(MediaPlayer mediaPlayer) {        mediaPlayer.seekTo(0);    }};

4、activity动画切换

overridePendingTransition(R.anim.activity_in_from_rigth, R.anim.activity_out_to_scale);

5、压缩图片

ByteArrayOutputStream baos = new ByteArrayOutputStream();bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);byte[] bitmapByte = baos.toByteArray();intent.putExtra("bitmap", bitmapByte);

6、显示图片的一段区域

ImageView mImageView = (ImageView) findViewById(R.id.id_imageview);try {    InputStream inputStream = getAssets().open("tangyan.jpg");    //获得图片的宽、高    BitmapFactory.Options tmpOptions = new BitmapFactory.Options();    tmpOptions.inJustDecodeBounds = true;    BitmapFactory.decodeStream(inputStream, null, tmpOptions);    int width = tmpOptions.outWidth;    int height = tmpOptions.outHeight;    //设置显示图片的中心区域    BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);    BitmapFactory.Options options = new BitmapFactory.Options();    options.inPreferredConfig = Bitmap.Config.RGB_565;    Bitmap bitmap = bitmapRegionDecoder.decodeRegion(new Rect(width / 2 - 100, height / 2 - 100, width / 2 + 100, height / 2 + 100), options);    mImageView.setImageBitmap(bitmap);} catch (IOException e){    e.printStackTrace();}

7、onTouchEvent里的
getX与getY指的是当前前控件所在的坐标
getX > 0 and getX< getWidth
getY > 0 and getY< getHeight

0 0
原创粉丝点击