android Bitmap类方法属性 详细说明
(转:http://blog.csdn.net/ymangu666/article/details/37729109)
1. BitMap类
public void recycle()——回收位图占用的内存空间,把位图标记为Dead
public final boolean isRecycled() ——判断位图内存是否已释放
public final int getWidth()——获取位图的宽度
public final int getHeight()——获取位图的高度
public final boolean isMutable()——图片是否可修改
public int getScaledWidth(Canvas canvas)——获取指定密度转换后的图像的宽度
public int getScaledHeight(Canvas canvas)——获取指定密度转换后的图像的高度
public boolean compress(CompressFormat format, int quality, OutputStream stream)——按指定的图片格式以及画质,将图片转换为输出流。
format:Bitmap.CompressFormat.PNG或Bitmap.CompressFormat.JPEG
quality:画质,0-100.0表示最低画质压缩,100以最高画质压缩。对于PNG等无损格式的图片,会忽略此项设置。
常用的静态方法:
public static Bitmap createBitmap(Bitmap src) ——以src为原图生成不可变得新图像
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
int dstHeight, boolean filter)——以src为原图,创建新的图像,指定新图像的高宽以及是否可变。
public static Bitmap createBitmap(int width, int height, Config config)——创建指定格式、大小的位图
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)以source为原图,创建新的图片,指定起始坐标以及新图像的高宽。
2. BitmapFactory工厂类:
Option 参数类:
public boolean inJustDecodeBounds——如果设置为true,不获取图片,不分配内存,但会返回图片的高度宽度信息。
public int inSampleSize——图片缩放的倍数。如果设为4,则宽和高都为原来的1/4,则图是原来的1/16。
public int outWidth——获取图片的宽度值
public int outHeight——获取图片的高度值
public int inDensity——用于位图的像素压缩比
public int inTargetDensity——用于目标位图的像素压缩比(要生成的位图)
public boolean inScaled——设置为true时进行图片压缩,从inDensity到inTargetDensity。
使用BitmapFactory 可从资源files, streams, and byte-arrays中解码生成Bitmap对象。
读取一个文件路径得到一个位图。如果指定文件为空或者不能解码成文件,则返回NULL。
public static Bitmap decodeFile(String pathName, Options opts)
public static Bitmap decodeFile(String pathName)
读取一个资源文件得到一个位图。如果位图数据不能被解码,或者opts参数只请求大小信息时,则返回NuLL。
(即当Options.inJustDecodeBounds=true,只请求图片的大小信息。)
public static Bitmap decodeResource(Resources res, int id)
public static Bitmap decodeResource(Resources res, int id, Options opts)
从输入流中解码位图
public static Bitmap decodeStream(InputStream is)
从字节数组中解码生成不可变的位图
public static Bitmap decodeByteArray(byte[] data, int offset, int length)
BitmapDrawable类:继承于Drawable,你可以从文件路径、输入流、XML文件以及Bitmap中创建。
常用的构造函数:
Resources res=getResources();//获取资源
public BitmapDrawable(Resources res)——创建一个空的drawable。(Response用来指定初始时所用的像素密度)替代public BitmapDrawable()方法(此方法不处理像素密度)
public BitmapDrawable(Resources res, Bitmap bitmap)——Create drawable from a bitmap
public BitmapDrawable(Resources res, String filepath)——Create a drawable by opening a given file path and decoding the bitmap.
public BitmapDrawable(Resources res, java.io.InputStream is)——Create a drawable by decoding a bitmap from the given input stream.
2).显示Bitmap
2.1) 使用Canvas类显示位图
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Panel(this));
}
class Panel extends View{
public Panel(Context context) {
super(context);
}
public void onDraw(Canvas canvas){
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
}
2.2) 通过BitmapDrawable显示位图
// 获取位图
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
// 转换为BitmapDrawable对象
BitmapDrawable bmpDraw=new BitmapDrawable(bmp);
// 显示位图
ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);
iv2.setImageDrawable(bmpDraw);
摘自:http://www.jb51.net/article/32366.htm
//方法:
//1 生成圆角Bitmap图片
//2 生成Bitmap缩量图
//3 压缩图片场长宽以及kB
//注意:
//以上代码,测试其中一个方法时最好注释掉其余的代码
public class MainActivity extends Activity {
private ImageView imageView;
private Bitmap copyRawBitmap1;
private Bitmap copyRawBitmap2;
private Bitmap copyRawBitmap3;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
//第一种方式:从资源文件中得到图片
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.haha);
copyRawBitmap1=rawBitmap;
copyRawBitmap2=rawBitmap;
copyRawBitmap3=rawBitmap;
//第二种方式:从SD卡中得到图片(方法1)
String SDCarePath=Environment.getExternalStorageDirectory().toString();
String filePath=SDCarePath+"/"+"haha.jpg";
Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);
//第二种方式:从SD卡中得到图片(方法2)
InputStream inputStream=getBitmapInputStreamFromSDCard("haha.jpg");
Bitmap rawBitmap2 = BitmapFactory.decodeStream(inputStream); //————>以下为将设置图片的圆角
Bitmap roundCornerBitmap=this.toRoundCorner(rawBitmap, 40);
imageView.setImageBitmap(roundCornerBitmap);
//————>以上为将设置图片的圆角 //————>以下为将图片高宽和的大小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);
// 将Bitmap转换为Drawable
Drawable newBitmapDrawable = new BitmapDrawable(newBitmap);
imageView.setImageDrawable(newBitmapDrawable);
//然后将Bitmap保存到SDCard中,方便于原图片的比较
this.compressAndSaveBitmapToSDCard(newBitmap, "xx100.jpg", 80);
//问题:
//原图大小为625x690 90.2kB
//如果设置图片500x500 压缩后大小为171kB.即压缩后kB反而变大了.
//原因是将:compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
//第二个参数quality设置得有些大了(比如100).
//常用的是80,刚设100太大了造成的.
//————>以上为将图片高宽和的大小kB压缩 //————>以下为将图片的kB压缩,宽高不变
this.compressAndSaveBitmapToSDCard(copyRawBitmap1,"0011fa.jpg",80);
//————>以上为将图片的kB压缩,宽高不变 //————>以下为获取SD卡图片的缩略图方法1
String SDCarePath1=Environment.getExternalStorageDirectory().toString();
String filePath1=SDCarePath1+"/"+"haha.jpg";
Bitmap bitmapThumbnail1=this.getBitmapThumbnail(filePath1);
imageView.setImageBitmap(bitmapThumbnail1);
//————>以上为获取SD卡图片的缩略图方法1 //————>以下为获取SD卡图片的缩略图方法2
String SDCarePath2=Environment.getExternalStorageDirectory().toString();
String filePath2=SDCarePath2+"/"+"haha.jpg";
Bitmap tempBitmap=BitmapFactory.decodeFile(filePath2);
Bitmap bitmapThumbnail2=ThumbnailUtils.extractThumbnail(tempBitmap, 100, 100);
imageView.setImageBitmap(bitmapThumbnail2);
//————>以上为获取SD卡图片的缩略图方法2 }
//读取SD卡下的图片
private InputStream getBitmapInputStreamFromSDCard(String fileName){
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String SDCarePath=Environment.getExternalStorageDirectory().toString();
String filePath=SDCarePath+File.separator+fileName;
File file=new File(filePath);
try {
FileInputStream fileInputStream=new FileInputStream(file);
return fileInputStream;
} catch (Exception e) {
e.printStackTrace();
} }
return null;
} //获取SDCard的目录路径功能
private String getSDCardPath() {
String SDCardPath = null;
// 判断SDCard是否存在
boolean IsSDcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if (IsSDcardExist) {
SDCardPath = Environment.getExternalStorageDirectory().toString();
}
return SDCardPath;
}
//压缩且保存图片到SDCard
private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap,String fileName,int quality){
String saveFilePaht=this.getSDCardPath()+File.separator+fileName;
File saveFile=new File(saveFilePaht);
if (!saveFile.exists()) {
try {
saveFile.createNewFile();
FileOutputStream fileOutputStream=new FileOutputStream(saveFile);
if (fileOutputStream!=null) {
//imageBitmap.compress(format, quality, stream);
//把位图的压缩信息写入到一个指定的输出流中
//第一个参数format为压缩的格式
//第二个参数quality为图像压缩比的值,0-100.0 意味着小尺寸压缩,100意味着高质量压缩
//第三个参数stream为输出流
rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
}
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace(); }
}
} //获取图片的缩略图
private Bitmap getBitmapThumbnail(String filePath){
BitmapFactory.Options options=new BitmapFactory.Options();
//true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息
options.inJustDecodeBounds=true;
//此时rawBitmap为null
Bitmap rawBitmap = BitmapFactory.decodeFile(filePath, options);
if (rawBitmap==null) {
System.out.println("此时rawBitmap为null");
}
//inSampleSize表示缩略图大小为原始图片大小的几分之一,若该值为3
//则取出的缩略图的宽和高都是原始图片的1/3,图片大小就为原始大小的1/9
//计算sampleSize
int sampleSize=computeSampleSize(options, 150, 200*200);
//为了读到图片,必须把options.inJustDecodeBounds设回false
options.inJustDecodeBounds = false;
options.inSampleSize = sampleSize;
//原图大小为625x690 90.2kB
//测试调用computeSampleSize(options, 100, 200*100);
//得到sampleSize=8
//得到宽和高位79和87
//79*8=632 87*8=696
Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options);
//保存到SD卡方便比较
this.compressAndSaveBitmapToSDCard(thumbnailBitmap, "15.jpg", 80);
return thumbnailBitmap;
} //参考资料:
//http://my.csdn.net/zljk000/code/detail/18212
//第一个参数:原本Bitmap的options
//第二个参数:希望生成的缩略图的宽高中的较小的值
//第三个参数:希望生成的缩量图的总像素
public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
} private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
//原始图片的宽
double w = options.outWidth;
//原始图片的高
double h = options.outHeight;
System.out.println("========== w="+w+",h="+h);
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
// return the larger one when there is no overlapping zone.
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
} /**
* @param bitmap 需要修改的图片
* @param pixels 圆角的弧度
* @return 圆角图片
*/
//参考资料:
//http://blog.csdn.net/c8822882/article/details/6906768
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;
} }
android Bitmap类方法属性 详细说明的更多相关文章
- android AndroidManifest.xml 属性详细解析
一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...
- Android中控件属性详细总结(转载)
转载地址:https://www.cnblogs.com/nanguojs/p/5950510.html 1.LinearLayout(线性布局): 可以分为水平线性:android:orientat ...
- Android中RelativeLayout属性详细说明
android:layout_above="@id/xxx" --将控件置于给定ID控件之上android:layout_below="@id/xxx" - ...
- android ListView 九大重要属性详细分析、
android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...
- [翻译]开发文档:android Bitmap的高效使用
内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...
- Windows7 64位系统搭建Cocos2d-x-2.2.1最新版以及Android交叉编译环境(详细教程)
Windows7 64位系统搭建Cocos2d-x-2.2.1最新版以及Android交叉编译环境(详细教程) 声明:本教程在参考了以下博文,并经过自己的摸索后实际操作得出,本教程系本人原创,由于升级 ...
- Android控件属性大全(转)
http://blog.csdn.net/pku_android/article/details/7365685 LinearLayout 线性布局 子元素任意: Tab ...
- Android开发EditText属性
Android开发EditText属性 EditText继承关系:View-->TextView-->EditText EditText的属性很多,这里介绍几个:android:hint= ...
- Android Bitmap 全面解析(四)图片处理效果对比 ...
对比对象: UIL Volley 官方教程中的方法(此系列教程一里介绍的,ImageLoader的处理方法和官方的差不多) -------------------------------------- ...
随机推荐
- Spark集群 + Akka + Kafka + Scala 开发(3) : 开发一个Akka + Spark的应用
前言 在Spark集群 + Akka + Kafka + Scala 开发(1) : 配置开发环境中,我们已经部署好了一个Spark的开发环境. 在Spark集群 + Akka + Kafka + S ...
- 利用FileStream实现多媒体文件复制
利用FileStream实现多媒体文件复制的主要思路在于利用两个FileStream对象,一个读取字节,另一个对象写入字节既可. 涉及知识点: 1.通常我们操作的File类,FileS ...
- windows下命令行打jar包方法
注意:系统必须装了java并且配置好了java环境变量. 事先必须编译好需要打jar的class.(eclipse一般在bin,maven构建的在target/classes) 进入cmd,输入ja ...
- Unsupported configuration attributes: [FILE_UPLOAD]
Caused by: java.lang.IllegalArgumentException: Unsupported configuration attributes: [FILE_UPLOAD] 情 ...
- Skippr – 轻量、快速的 jQuery 幻灯片插件
Skippr 是一个超级简单的 jQuery 幻灯片插件.只是包括你的网页中引入 jquery.skippr.css 和 jquery.skippr.js 文件就能使用了.Skippr 能够自适应窗口 ...
- GifShot - 创建动态 GIF 的 JavaScript 库
GifShot 是一个可以创建流媒体,视频或图像的 GIF 动画的 JavaScript 库.该库的客户端特性使其非常便携,易于集成到几乎任何网站.利用最先进的浏览器 API ,包括 WebRTC , ...
- 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService... 匹配的导出 VS2012报错
刚安装完VS2012,打开VS2012新建项目,但是并没有像之前那样顺利的创建页面,而是弹出了一个错误窗口. 我的系统是win7旗舰版 64位 ,同时安装了VS2010和VS2012.然后我又试了一下 ...
- 规划SharePoint2010的管理员密码更改
规划自动密码更改 (SharePoint Server 2010) 为了简化密码管理,自动密码更改功能允许您更新和部署密码,而不必在多个帐户.服务和 Web 应用程序之间执行手动密码更新任务.您可以配 ...
- JavaScript学习11 数组排序实例
JavaScript学习11 数组排序实例 数组声明 关于数组对象的声明,以前说过:http://www.cnblogs.com/mengdd/p/3680649.html 数组声明的一种方式: va ...
- Fragment与FragmentActivity的关系
前阵用viewpaper+fragment做滑动引导,查阅了下网上的资料,发现在有两种做法,一个是自建类直接继承Activity另一种是继承FragmentActivity,很是迷惑,在查了些goog ...