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

Android开发总结笔记 Btimap (上) 3-4

2015-12-28 11:21 134 查看
关于Bitmap的几个类

Bitmap

BitmapFactory:用于创建Bitmap的接口类,里面有各种decode方法.





BitmapFactory.Options:BitmapFactory的一个静态内部类,用于设置decode时的一些参数

Bitmap的一些常用方法

public boolean compress(Bitmap.CompressFormat format,int quality,OutputStream stream)
将位图压缩到指定OutputStream,可以理解将Bitmap保存到文件中
format表示格式,PNG,JPG等
quality表示压缩质量,0-100
stream输出流
返回值表示是否成功压缩void recycle 回收位图占用的空间,把位图标记为Dead

boolean isRecycled 判断位图是否已经释放

int getWidth 获取位图的宽度

int getHeight 获取位图的高度

boolean isMutable 图片是否可修改

int getScaledWidth(Canvas canvas) 获取指定密度转换后的图像的宽度

int getScaledHeight(Canvas canvas) 获取指定密度转换后的图像的高度

Bitmap createBitmap 这个方法有多个重载,可以通过设置一些参数生成指定的位图





BItmapFactory.Options





看几个比较常用的

boolean inJustDecodeBounds如果设置为true,不获取图片,不分配内存,但会返回图片的高宽度信息

int inSampleSize 图片缩放的倍数,如果设置为4,则宽高为原本的四分之一

int outWidth获取图片的宽度值

int outHeight 获取图片的高度值

int Density 用于位图的像素压缩比

int inTargetDensity 用于目标位图的像素压缩比

boolean inScaled设置为true时进行图片压缩,从inDensity到inTargetDensity

从资源中获取Bitmap位图

BitmapDrawable drawable = new BitmapDrawable(getAssets().open("test.jpg"));

Bitmap mBitmap = drawable.getBitmap();

[/code]

//通过资源ID

private Bitmap getBitmapFromResource(Resources res, int resId) {

return BitmapFactory.decodeResource(res, resId);

}


//文件

private Bitmap getBitmapFromFile(String pathName) {

return BitmapFactory.decodeFile(pathName);

}


//字节数组

public Bitmap Bytes2Bimap(byte[] b) {

if (b.length != 0) {

return BitmapFactory.decodeByteArray(b, 0, b.length);

} else {

return null;

}

}


//输入流

private Bitmap getBitmapFromStream(InputStream inputStream) {

return BitmapFactory.decodeStream(inputStream);

}

[/code]

简单实现截图功能

private void getScreenHot(View v, String filePath)

{

try

{

Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Config.ARGB_8888);

Canvas canvas = new Canvas();

canvas.setBitmap(bitmap);

v.draw(canvas);


try

{

FileOutputStream fos = new FileOutputStream(filePath);

bitmap.compress(CompressFormat.PNG, 100, fos);

}

catch (FileNotFoundException e)

{

throw new InvalidParameterException();

}


}

catch (Exception e)

{

e.printStackTrace();

}

}

[/code]

只是截取了decorView的内容,状态栏的并没有截取

getScreenHot((View) getWindow().getDecorView(), "/sdcard/test1.png");

[/code]


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: