1.大图片的缩放
由于图片尺寸过大,不仅不能直接在屏幕放下,导致查看不方便,而且可能会导致OOM异常(内存溢出)。
注意,bmp图像是rgb形式的位图,安卓下是argb的位图,而jpeg和png则是压缩过的图像,当我们想把一张图片显式到屏幕上时,需要解码为rgb的形式,这样压缩过的图像在显示时就会消耗很多内存。
解决这个问题的办法就是就是对图片的尺寸压缩,然后采样显示图片。
API
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); //用于获取窗体信息
例程
缩放图片加载到内存
步骤:
1、获得设备屏幕的分辨率;
2、得到原图的分辨率;
3、通过比较得到一个合适的比例值;
4、按照比例值缩放图片
5、把图片显示在imageview
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.WindowManager;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
// 1、获得设备屏幕的分辨率;
//通过服务获得窗口管理器
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
//获得设备分辨率对象
Display display = wm.getDefaultDisplay();
//api13之前使用这个两个方法,13之后置为过期
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
// 2、得到原图的分辨率;
Options opts = new Options();
opts.inJustDecodeBounds = true;
//opts为null 可以返回一个图片对象,如果不为null ,就会返回null
BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/lp.jpg", opts);
//得到原图的宽和高
int srcWidth = opts.outWidth;
int srcHeight = opts.outHeight;
// 3、通过比较得到一个合适的比例值;
//3000/320 = 9 2262/480 =5
int sx = srcWidth/screenWidth;
int sy = srcHeight/screenHeight;
int scale = 0;
if(sx >= 1 && sx > sy){
scale = sx;
}else if(sy >= 1 && sy > sx){
scale = sy;
}
// 4、按照比例值缩放图片
opts.inJustDecodeBounds = false;
opts.inSampleSize = scale; // 1/scale * 1/scale
Bitmap bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/lp.jpg", opts);
// 5、把图片显示在imageview
iv.setImageBitmap(bm);
}
}
2.创建原图的副本
由于直接读取本地图片我们没有办法修改,所以如果想对一张图片进行绘图操作,必须读入一幅图像。
例程
package com.itheima.copypic;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
// 1、得到原图;
Bitmap srcPic = BitmapFactory.decodeResource(getResources(), R.drawable.meinv);
//config 包括位深度
// 2、创建一个空白的纸张,参考原图;
Bitmap copyPic = Bitmap.createBitmap(srcPic.getWidth(), srcPic.getHeight(), srcPic.getConfig());
// 3、创建一个画板,参考空白纸张;
Canvas canvas = new Canvas(copyPic);
// 4、创建一个画笔;
Paint paint = new Paint();
paint.setColor(Color.BLACK);//默认是使用黑色,后面会根据原图的颜色画画
// 5、在画板上画画;
canvas.drawBitmap(srcPic, new Matrix(), paint);
iv.setImageBitmap(copyPic);
}
}
3.图形处理
* 旋转
Matrix matrix = new Matrix();
matrix.setRotate(30); //旋转多少度
matrix.setRotate(30, copyBitmap.getWidth()/2, copyBitmap.getHeight()/2);
canvas.drawBitmap(srcBitmap, matrix, paint);
* 缩放
Matrix matrix = new Matrix();
//对图片进行缩放
matrix.setScale(0.5f, 0.5f);
canvas.drawBitmap(srcBitmap, matrix, paint);
* 平移
Matrix matrix = new Matrix();
matrix.setTranslate(20, 0); //对图片进行平移
canvas.drawBitmap(srcBitmap, matrix, paint);
* 镜面、倒影
* 修改颜色
ColorMatrix cm = new ColorMatrix();
cm.set(new float[] {
1, 0, 0, 0, 0, //red
0, 1, 0, 0, 0, //green
0, 0, 1, 0, 0, //blue
0, 0, 0, 1, 0 //透明度
});
paint.setColorFilter(new ColorMatrixColorFilter(cm));
4.绘图
public class MainActivity extends Activity {
private ImageView iv;
private Bitmap srcPic;
private Bitmap copyPic;
private Canvas canvas;
private Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
srcPic = BitmapFactory.decodeResource(getResources(), R.drawable.bg);
copyPic = Bitmap.createBitmap(srcPic.getWidth(), srcPic.getHeight(), srcPic.getConfig());
canvas = new Canvas(copyPic);
paint = new Paint();
paint.setColor(Color.BLACK);//默认是使用黑色,后面会根据原图的颜色画画
// 5、在画板上画画;
Matrix matrix = new Matrix();
//
canvas.drawBitmap(srcPic, matrix, paint);
iv.setImageBitmap(copyPic);
//为ImageView控件创建一个事件监听
iv.setOnTouchListener(new OnTouchListener(){
int startX ;
int startY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int type = event.getAction();
switch (type) {
case MotionEvent.ACTION_DOWN: //发生按下
//开始坐标点
startX = (int) event.getX();
startY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE : //发生移动
//得到结束点的坐标
int newX = (int) event.getX();
int newY = (int) event.getY();
canvas.drawLine(startX, startY, newX, newY, paint);
iv.setImageBitmap(copyPic);
//得到新的开始坐标点
startX = (int) event.getX();
startY = (int) event.getY();
break;
}
//消费掉当前的监听器
return true;
}
});
}
}
5.播放音频
API
MediaPlayer 用于播放音频和部分视频
//使用阻塞式准备方法,等待数据资源准备完成后才开始播放,用于播放本地的音视频文件
// mediaPlay.prepare();
//使用异步式准备方法,使用子线程等待数据资源准备完成后才开始播放,用于播放网络上的音视频文件
mediaPlay.prepareAsync();
例程
player = new MediaPlayer(); //创建一个MidaPlayer
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
player.setDataSource("/mnt/sdcard/xpg.mp3");
player.prepare();
//等待底层操作完毕后开始调用start方法
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
barMax = player.getDuration(); //获取当前播放的总时长
registTimer();
player.seekTo(barProgress);
//开始播放
player.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
6.视频播放器videoview
API
VideoView 播放的类
例程
package com.itheima.videoview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;
public class MainActivity extends Activity {
private VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vv = (VideoView) findViewById(R.id.vv);
vv.setVideoPath("http://192.168.22.136:8080/11.mp4");
vv.start();
}
}
7.SurfaceView 播放视频
API
SurfaceView 显示
例程
public void click(View v)
{
new Thread(){public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//设置数据源
final MediaPlayer player = new MediaPlayer();
try {
player.setDataSource("mnt/sdcard/he.3gp");
//准备播放
player.prepareAsync();
//获取一个holder
SurfaceHolder holder = sfv.getHolder();
player.setDisplay(holder);
//等待底层工作完毕后 开始播放
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
player.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}}.start();
}
8.Vitamio播放视频
需要导入Vitamio类库
例程
在布局中定义VideoView
<io.vov.vitamio.widget.VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
一定要在清单文件初始化InitActivity
<activity android:name="io.vov.vitamio.activity.InitActivity"></activity>
//***********************************************************************
//做一下vitamio 初始化的操作
if (!LibsChecker.checkVitamioLibs(this)) {
return;
}
final VideoView vv = (VideoView) findViewById(R.id.vv);
vv.setVideoPath("mnt/sdcard/he.3gp");
vv.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vv.start();
System.out.println("***********************");
}
});
9.摄像机和照相机
使用意图开启相机
照相机应用
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory()+"/123.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 0);
录像机应用
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
file = new File(Environment.getExternalStorageDirectory()+"/123.3gp");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 0);