Android——Bitmap及其BitmapFactory的常用方法

来源:互联网 发布:ios8录屏软件 编辑:程序博客网 时间:2024/06/01 12:18

1.BitMap类

public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,

boolean filter)——以src为原图,创建新的图像,指定新图像的高宽以及是否可变。

int pieceWidth = (int) (lineHeight * radioOflineHeight);

例如:whitePiece 是一张BitMap图片

whitePiece = Bitmap.createScaledBitmap(whitePiece, pieceWidth, pieceWidth,

false);

public final int getWidth()——获取位图的宽度

public final int getHeight()——获取位图的高度

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工厂类:
public int inSampleSize——图片缩放的倍数。如果设为4,则宽和高都为原来的1/4,

则图是原来的1/16。

public int outWidth——获取图片的宽度值

public int outHeight——获取图片的高度值

//第一种方式:从资源文件中得到图片

public static Bitmap decodeResource(Resources res, int id)

//第二种方式:从SD卡中得到图片

String SDCarePath=Environment.getExternalStorageDirectory().toString();

String filePath=SDCarePath+”/”+”haha.jpg”;

Bitmap rawBitmap1 = BitmapFactory.decodeFile(filePath, null);

从输入流中解码位图
public static Bitmap decodeStream(InputStream is)

public BitmapDrawable(Resources res, Bitmap bitmap)——在资源中从bitmap变

BitMapdrawable

通过BitmapDrawable显示位图

1 // 获取位图

Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);

2 // 转换为BitmapDrawable对象

BitmapDrawable bmpDraw=new BitmapDrawable(bmp);

3 // 显示位图

ImageView iv2 = (ImageView)findViewById(R.id.ImageView02);

iv2.setImageDrawable(bmpDraw);

将Bitmap保存到SDCard中,方便于原图片的比较

this.compressAndSaveBitmapToSDCard(newBitmap, “xx100.jpg”, 80);

根据资源res转换成BitMap大图:

// 获得大图
1.Bitmap bitmap = ((BitmapDrawable)

getResources().getDrawable(R.drawable.xxx)).getBitmap();

2.Bitmap bitmap = BitmapFactory.decodeResource(getResources(),

R.drawable.xxx);

将大图分割成小图:

for (int i = 0; i < row; i++) {            for (int j = 0; j < column; j++) {            //bitmap是大图                Bitmap bm = Bitmap.createBitmap(bitmap, j * mWidthAndHeight, i * mWidthAndHeight, mWidthAndHeight,                        mWidthAndHeight);                ivs[i][j] = new ImageView(act);                ivs[i][j].setImageBitmap(bm);                }}

更详细方法http://www.cnblogs.com/Free-Thinker/p/6394818.html

阅读全文
0 0
原创粉丝点击