Android编程之图片相关代码集锦
本文实例总结了Android编程之图片相关代码。分享给大家供大家参考,具体如下:
1.Bitmap转化为字符串:
/**
*@param位图
*@return转化成的字符串
*/
publicstaticStringbitmapToString(Bitmapbitmap){
//将Bitmap转换成字符串
Stringstring=null;
ByteArrayOutputStreambStream=newByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG,100,bStream);
byte[]bytes=bStream.toByteArray();
string=Base64.encodeToString(bytes,Base64.DEFAULT);
returnstring;
}
2.字符串转化为Bitmap:
/**
*@paramstring字符串
*@return转化成的位图
*/
publicstaticBitmapstringToBitmap(Stringstring){
//将字符串转换成Bitmap类型
Bitmapbitmap=null;
try{
byte[]bitmapArray;
bitmapArray=Base64.decode(string,Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray,0,bitmapArray.length);
}catch(Exceptione){
e.printStackTrace();
}
returnbitmap;
}
3.Bitmap转化为Drawable:
/**
*@parambitmapBitmap位图图像
*@returnDrawable转换后的Drawable对象
*/
publicstaticDrawablebitmapToDrawable(Bitmapbitmap){
if(bitmap==null)
returnnull;
if(160!=bitmap.getDensity()){
bitmap.setDensity(160);
}
returnnewBitmapDrawable(bitmap);
}
根据图片资源ID获取Drawable对象:
/**
*@paramcontext上下文
*@paramid图片的资源ID
*@returnDrawable对象
*/
publicstaticDrawableresourceToDrawable(Contextcontext,intid){
returnnull==context?null:bitmapToDrawable(BitmapFactory.decodeResource(context.getResources(),id));
}
byte数组转换Drawble对象:
/**
*@parambytesbyte数组
*@returndrawble对象
*/
publicstaticDrawablebyteArrayToDrawable(byte[]bytes){
returnnull==bytes?null:bitmapToDrawable(BitmapFactory.decodeByteArray(bytes,0,bytes.length));
}
4.Drawable转化为bitmap:
/**
*Drawble对象转Bitmap对象
*@paramdrawabledrawble对象
*@returnbitmap对象
*/
publicstaticBitmapdrawableToBitmap(Drawabledrawable){
returnnull==drawable?null:((BitmapDrawable)drawable).getBitmap();
}
5.byte数组转换Bitmap对象:
/**
*@parambytesbyte数组
*@returnbitmap对象
*/
publicstaticBitmapbyteArrayToBitmap(byte[]bytes){
returnnull==bytes?null:BitmapFactory.decodeByteArray(bytes,0,bytes.length);
}
6.图片去色,返回灰度图片(老式图片):
/**
*@parambitmap传入的bitmap
*@return去色后的图片Bitmap对象
*/
publicstaticBitmaptoGrayscale(Bitmapbitmap){
intwidth,height;
height=bitmap.getHeight();
width=bitmap.getWidth();
BitmapbmpGrayscale=Bitmap.createBitmap(width,height,Bitmap.Config.RGB_565);
Canvasc=newCanvas(bmpGrayscale);
Paintpaint=newPaint();
ColorMatrixcm=newColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilterf=newColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bitmap,0,0,paint);
returnbmpGrayscale;
}
7.对图片进行缩放:
/**
*@paramurl图片的路径
*@paramrequireSize缩放的尺寸
*@return缩放后的图片Bitmap对象
*/
publicstaticBitmapgetScaleImage(Stringurl,intrequireSize){
BitmapFactory.Optionso=newBitmapFactory.Options();
//此属性表示图片不加载到内存,只是读取图片的属性,包括图片的高宽
o.inJustDecodeBounds=true;
BitmapFactory.decodeFile(url,o);
intwidth_tmp=o.outWidth,height_tmp=o.outHeight;
intscale=1;
while(true){
if(width_tmp/2<requireSize||height_tmp/2<requireSize)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Optionso2=newBitmapFactory.Options();
o2.inSampleSize=scale;
Bitmapbmp=BitmapFactory.decodeFile(url,o2);
returnbmp;
}
8.获得图片的倒影,同时倒影渐变效果:
/**
*@parambitmap图片源
*@return处理后的图片Bitmap对象
*/
publicstaticBitmapcreateMirro(Bitmapbitmap){
intwidth=bitmap.getWidth();
intheight=bitmap.getHeight();
intshadow_height=15;
int[]pixels=newint[width*height];
bitmap.getPixels(pixels,0,width,0,0,width,height);
//shadoweffect
intalpha=0x00000000;
for(inty=0;y<height;y++){
for(intx=0;x<width;x++){
intindex=y*width+x;
intr=(pixels[index]>>16)&0xff;
intg=(pixels[index]>>8)&0xff;
intb=pixels[index]&0xff;
pixels[index]=alpha|(r<<16)|(g<<8)|b;
}
if(y>=(height-shadow_height)){
alpha=alpha+0x1F000000;
}
}
//inverteffect
Bitmapbm=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
for(inty=0;y<height;y++){
bm.setPixels(pixels,y*width,width,0,height-y-1,width,1);
}
returnBitmap.createBitmap(bm,0,0,width,shadow_height);
}
9.保存图片到SDCard:
/**
*@paramimagePath图片保存路径
*@parambm被保存的bitmap对象
*/
publicstaticvoidsaveImgToLocal(StringimagePath,Bitmapbm){
if(bm==null||imagePath==null||"".equals(imagePath)){
return;
}
Filef=newFile(imagePath);
if(f.exists()){
return;
}else{
try{
FileparentFile=f.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
f.createNewFile();
FileOutputStreamfos;
fos=newFileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG,100,fos);
fos.close();
}catch(FileNotFoundExceptione){
f.delete();
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
f.delete();
}
}
}
10.从SDCard中获取图片:
/**
*@paramimagePath图片在SDCard中保存的路径
*@return返回保存的bitmap对象
*/
publicstaticBitmapgetImageFromLocal(StringimagePath){
Filefile=newFile(imagePath);
if(file.exists()){
Bitmapbitmap=BitmapFactory.decodeFile(imagePath);
file.setLastModified(System.currentTimeMillis());
returnbitmap;
}
returnnull;
}
11.图片压缩处理:
/**
*对图片进行压缩,主要是为了解决控件显示过大图片占用内存造成OOM问题。
*一般压缩后的图片大小应该和用来展示它的控件大小相近。
*@paramcontext上下文
*@paramresId图片资源Id
*@paramreqWidth期望压缩的宽度
*@paramreqHeight期望压缩的高度
*@return压缩后的图片
*/
publicstaticBitmapcompressBitmapFromResourse(Contextcontext,intresId,intreqWidth,intreqHeight){
finalBitmapFactory.Optionsoptions=newBitmapFactory.Options();
/*
*第一次解析时,inJustDecodeBounds设置为true,
*禁止为bitmap分配内存,虽然bitmap返回值为空,但可以获取图片大小
*/
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(context.getResources(),resId,options);
finalintheight=options.outHeight;
finalintwidth=options.outWidth;
intinSampleSize=1;
if(height>reqHeight||width>reqWidth){
finalintheightRatio=Math.round((float)height/(float)reqHeight);
finalintwidthRatio=Math.round((float)width/(float)reqWidth);
inSampleSize=heightRatio<widthRatio?heightRatio:widthRatio;
}
options.inSampleSize=inSampleSize;
//使用计算得到的inSampleSize值再次解析图片
options.inJustDecodeBounds=false;
returnBitmapFactory.decodeResource(context.getResources(),resId,options);
}
12.获取可用内存的最大值(App使用内存超出这个值会引起OutOfMemory异常):
privateintgetMaxMemoryForApp(){
intmaxMemory=(int)(Runtime.getRuntime().maxMemory()/1024);
returnmaxMemory;
}
13.将图片裁剪成圆圈:
/**
*将Bitmap处理为圆形的图片
*@parambitmap处理之前的位图
*@return处理之后的位图
*/
publicstaticBitmapcirclePic(Bitmapbitmap){
intwidth=bitmap.getWidth();
intheight=bitmap.getHeight();
intr=width<height?width/2:height/2;//圆的半径,取宽和高中较小的,以便于显示没有空白
BitmapoutBitmap=Bitmap.createBitmap(r*2,r*2,Bitmap.Config.ARGB_8888);//创建一个刚好2r大小的Bitmap
Canvascanvas=newCanvas(outBitmap);
finalintcolor=0xff424242;
finalPaintpaint=newPaint();
/**
*截取图像的中心的一个正方形,用于在原图中截取
*坐标如下:
*1.如果w<h,左上坐标(0,(h-w)/2),右上坐标(w,(h+w)/2)偏移10
*2.如果w>h,左上坐标((w-h)/2,0),右上坐标((w+h)/2,h)偏移10
*/
finalRectrect=newRect(width<height?0:(width-height)/2,width<height?(height-width)/2-10:-10,
width<height?width:(width+height)/2,(width<height?(height+width)/2-10:height-10));
//创建一个直径大小的正方形,用于设置canvas的显示与设置画布截取
finalRectrect2=newRect(0,0,r*2,r*2);
//提高精度,用于消除锯齿
finalRectFrectF=newRectF(rect2);
//下面是设置画笔和canvas
paint.setAntiAlias(true);
canvas.drawARGB(0,0,0,0);
paint.setColor(color);
//设置圆角,半径都为r,大小为rect2
canvas.drawRoundRect(rectF,r,r,paint);
//设置图像重叠时的显示方式
paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//绘制图像到canvas
canvas.drawBitmap(bitmap,rect,rect2,paint);
returnoutBitmap;
}
}
希望本文所述对大家Android程序设计有所帮助。