您的位置:首页 > 移动开发 > Android开发

Android 常用知识总结

2016-06-15 08:22 603 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android mediaplayer