转载自http://www.cnblogs.com/summers/
BitMap代表一张位图,扩展名可以是.bmp或者.dib。位图是Windows标准格式图形文件,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示,包括2、4、8、16、24和32位色彩。例如,一幅1024×768分辨率的32位真彩图片,其所占存储字节数为:1024×768×32/8=3072KB
位图文件图像效果好,但是非压缩格式的,需要占用较大存储空间,不利于在网络上传送。jpg格式则恰好弥补了位图文件这个缺点。
在android系统当中,bitmap是图像处理最重要的类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。
下面主要介绍BitMap的用法:
1.从资源文件中获取
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img1);
2.从SD卡中得到图片
(方法1)
String SDCarePath=Environment.getExternalStorageDirectory().toString();
String filePath=SDCarePath+”/”+”haha.jpg”;
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);
(方法2)
InputStream inputStream=getBitmapInputStreamFromSDCard(“haha.jpg”);
Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream);
3.设置图片的圆角,返回设置后的BitMap
public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
Bitmap roundCornerBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(roundCornerBitmap);
int color = 0xff424242;// int color = 0xff424242;
Paint paint = new Paint();
paint.setColor(color);
// 防止锯齿
paint.setAntiAlias(true);
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
RectF rectF = new RectF(rect);
float roundPx = pixels;
// 相当于清屏
canvas.drawARGB(0, 0, 0, 0);
// 先画了一个带圆角的矩形
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
// 再把原来的bitmap画到现在的bitmap!!!注意这个理解
canvas.drawBitmap(bitmap, rect, rect, paint);
return roundCornerBitmap;
}
4.将图片高宽和的大小kB压缩
//得到图片原始的高宽
int rawHeight = rawBitmap.getHeight();
int rawWidth = rawBitmap.getWidth();
// 设定图片新的高宽
int newHeight = 500;
int newWidth = 500;
// 计算缩放因子
float heightScale = ((float) newHeight) / rawHeight;
float widthScale = ((float) newWidth) / rawWidth;
// 新建立矩阵
Matrix matrix = new Matrix();
matrix.postScale(heightScale, widthScale);
// 设置图片的旋转角度
// matrix.postRotate(-30);
// 设置图片的倾斜
// matrix.postSkew(0.1f, 0.1f);
// 将图片大小压缩
// 压缩后图片的宽和高以及kB大小均会变化
Bitmap newBitmap = Bitmap.createBitmap(rawBitmap, 0, 0, rawWidth,
rawWidth, matrix, true);
5.将Bitmap转换为Drawable Drawable转Bitmap
Drawable newBitmapDrawable = new BitmapDrawable(Bitmap);
//如果要获取BitMapDrawable中所包装的BitMap对象,可以用getBitMap()方法;
Bitmap bitmap = newBitmapDrawable.getBitmap();
6.由于前面创建的Bitmap所占用的内存还没有回收,而导致引发OutOfMemory错误,所以用下面方法判断是否回收。
if(!bitmap.isRecycled())
{
bitmap.recycle()
}
从服务器下载一张图片,显示在ImageView控件上,并将该图片保存在移动设备的SD上。
// 根据网络URL获取输入流
public InputStream getUrlInputStream(String strUrl) throws IOException {
URL url = new URL(strUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inputStream = conn.getInputStream();
if (inputStream != null) {
return inputStream;
} else {
Log.i("inputStream", "输入流对象为空");
return null;
}
}
// 将输入流转化为Bitmap流
public Bitmap getBitmap(InputStream inputStream) {
Bitmap bitmap = null;
if (inputStream != null) {
bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} else {
Log.i("test", "输入流对象in为空");
return null;
}
}
// 给ImageView对象赋值
public void setWidgetImage(Bitmap bitmap) {
ImageView img = new ImageView(this);
if (bitmap != null) {
img.setImageBitmap(bitmap);
}
}
// 获取SD卡上的文件存储路径
public void createSDFile() {
File sdroot = Environment.getExternalStorageDirectory();
File file = new File(sdroot + "/Android/date/包名/文件名");
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
// 相关操作
}
}
// 将图片保存到SD卡上
public boolean readToSDCard(File file, Bitmap bitmap)
throws FileNotFoundException {
FileOutputStream os = new FileOutputStream(file);
return bitmap.compress(Bitmap.CompressFormat.PNG, 90, os);
// true:表示操作成功,false:表示操作失败
}