1、图片压缩
从目录中加载图片:
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/xxx.bmp"); ----原大小加载
iv.setImageBitmap(bm); ----速度慢
压缩加载:
----------压缩的时候,常常要计算压缩倍数
获取屏幕宽高:
WindowManager widow=getWindowManager();
int screenWidth=widow.getDefultDisplay().getWidth();
int screenHeight=widow.getDefultDisplay.getHeight();
获取图片宽高:
Options ops=new Options();
ops.inJustDecodeBounds=true; 只加载图片的属性。
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/xxx.bmp",ops);
int bmpWidth=ops.outWidth();
int bmpHeight=ops.outHeight();
计算压缩比例:
int times=1;
int wtims=bmpWidth/screenWidth;
int htimes=bmpHeight/screenHeight;
if(wtims>htimes){times=wtims;}
else{times=htimes;}
ops.inSampleSize=times; --------压缩比例
ops.inJustDecodeBounds=flase;
Bitmap bm=BitmapFactory.decodeFile("/mnt/sdcard/xxx.bmp",ops);
iv.setImageBitmap(bm);
2、图片copy
public void copyImage(View view){
Bitmap bm;
1.创建一张空白图片
Bitmap copybm=Bitmap.createBitmap(bm.getWidth(),bm.getHeight,bm.getconfig());
2.将旧图片bm的内容 画 到copybm中
----调用 画() 的类
Canvas canvas=new Canvas(copybm);
-----matrix 矩形对象
Matrix matrix=new Matrix();
-----paint 画笔
Paint paint=new Paint();
canvas.drawBitmap(bm,matrix,paint); -----matrix,paint
iv.setImageBitmap(copybm);
}
`3、撕衣服
设置透明度:
bm.setPixel(x,y,Color.TRANSPARENT);
设置两个ImageView,对上层IV:
iv.setOnTouchListener(new View.OnTouchListener(){ --监听点击事件
public boolean onTouch(View v,MotionEvent event){
int action=event.getAction();
switch(action){
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.MOVE:
int x=(int ) event.getX();
int y=(int) event.getY();
for(int xi=x-2;xi<x+2;xi++){
for(int yi=y-2;yi<y+2;yi++){
copyImage,setPixel(xi,yi,Color.TRANSPARENT);
}
}
break;
}
return(ture);
}
});
4、播放音乐
MediaPlayer player=new MediaPlayer();
1.播放
player.setDataSource("/.../../xxx.mp3");
player.setAudioStreamType(AudioManager.STREAM_MUSIC); --设置类型
player.prepare(); ------- 播放器准备
player.start(); -------开始
上方的方式,从网络加载可能会很慢。所以使用一步加载:
player.prepareAsync();
player.setOnpreparedListener(new OnpreparedListener(){
pulic void onPrepared(MediaPlayer mp){
player.start(); -------设置监听器,加载完成后播放
}
});
playerButton.setEnabled(flase); ----- 禁用播放按钮
--设置监听器,歌曲播放完成后,按钮启用
player.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer mp){
playerButton.setEnabled(true);
}
});
2.暂停
player.pause();
3.重播
player.seekTo(0); -----跳到0毫秒。 单位 毫秒。
4.停止
player.stop();
---停止之后,释放,设空
player.release();
player=null;
5、播放视频
方法:
MediaPlayer对象,配合控件 <SurfaceView /> -----可用于视频、游戏界面等等
public class MainActivity extends Activity {
private MediaPlayer player;
private SurfaceView surface;
private SurfaceHolder sh;
private Button pauseBt;
private int jindu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surface=(SurfaceView)this.findViewById(R.id.surface);
pauseBt=(Button)this.findViewById(R.id.pause);
}
public void player(View view){
player=new MediaPlayer();
try {
player.setDataSource("http://192.168.0.129:8080/fuwuqi/123.mp4");
} catch (Exception e)
{e.printStackTrace();}
sh=surface.getHolder();
sh.addCallback(new Callback(){
@Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
// TODO Auto-generated method stub
System.out.println("bm");
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
sh=surface.getHolder();
player.setDisplay(sh);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
player.seekTo(jindu);
player.start();
}
});
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
jindu=player.getCurrentPosition();
player.stop();
}
});
player.setDisplay(sh);
player.prepareAsync();
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
player.start();
}
});
}
public void pause(View view){
if(pauseBt.getText().toString().equals("暂停")){
if(player!=null&&player.isPlaying()){
player.pause();
pauseBt.setText("继续");}
}else{
player.start();
pauseBt.setText("暂停");
}
}
public void stop(View view){
if(player!=null)
{player.stop();
player.release();}
}
}