android Bitmap用法总结

Bitmap用法总结
1、Drawable → Bitmap
public static Bitmap
drawableToBitmap(Drawable drawable) {
Bitmap bitmap =
Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity()
!= PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
:
Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
//
canvas.setBitmap(bitmap);
drawable.setBounds(0, 0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return
bitmap;
}
2、从资源中获取Bitmap
Resources res=getResources();
Bitmap
bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap →
byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos
= new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100,
baos);
return baos.toByteArray();
}
4、byte[] → Bitmap
private Bitmap
Bytes2Bimap(byte[] b){
if(b.length!=0){
return
BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return
null;
}
}
5、保存bitmap
static boolean saveBitmap2file(Bitmap
bmp,String filename){
CompressFormat format=
Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream =
null;
try {
stream = new FileOutputStream("/sdcard/" + filename);
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch
block
Generated by Foxit PDF Creator ? Foxit
Software
http://www.foxitsoftware.com For evaluation
only.
e.printStackTrace();
}
return bmp.compress(format, quality,
stream);
}
6、将图片按自己的要求缩放
// 图片源
Bitmap bm =
BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.dog));
//
获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
//
设置想要的大小
int newWidth = 320;
int newHeight = 480;
// 计算缩放比例
float
scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float)
newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new
Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap
newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
//
放在画布上
canvas.drawBitmap(newbm, 0, 0,
paint);
相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
7、bitmap的用法小结
BitmapFactory.Options
option = new BitmapFactory.Options();
option.inSampleSize = 2;
//将图片设为原来宽高的1/2,防止内存溢出
Bitmap bm =
BitmapFactory.decodeFile("",option);//文件流
URL url = new
URL("");
InputStream is = url.openStream();
Bitmap bm =
BitmapFactory.decodeStream(is);
android:scaleType:
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType
/
android:scaleType值的意义区别:
CENTER /center
按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
显示
CENTER_CROP / centerCrop
按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
(宽)
CENTER_INSIDE / centerInside
将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
长/宽等于或小于View的长/宽
Generated by Foxit PDF
Creator ? Foxit Software
http://www.foxitsoftware.com For evaluation
only.
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd
把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart
把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片 不按比例
扩大/缩小到View的大小显示
MATRIX / matrix
用矩阵来绘制,动态缩小放大图片来显示。

//放大缩小图片
public static Bitmap
zoomBitmap(Bitmap bitmap,int w,int h){
int width = bitmap.getWidth();
int
height = bitmap.getHeight();
Matrix matrix = new Matrix();
float
scaleWidht = ((float)w / width);
float scaleHeight = ((float)h /
height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp =
Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
return
newbmp;
}
//将Drawable转化为Bitmap
public static Bitmap
drawableToBitmap(Drawable drawable){
int width =
drawable.getIntrinsicWidth();
int height =
drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width,
height,
drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new
Canvas(bitmap);
drawable.setBounds(0,0,width,height);
drawable.draw(canvas);
return
bitmap;
Generated by Foxit PDF Creator ? Foxit
Software
http://www.foxitsoftware.com For evaluation
only.
}
//获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap
bitmap,float roundPx){
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new
Canvas(output);
final int color = 0xff424242;
final Paint paint = new
Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new
RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0,
0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx,
paint);
paint.setXfermode(new
PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect,
paint);
return output;
}
//获得带倒影的图片方法
public static Bitmap
createReflectionImageWithOrigin(Bitmap bitmap){
final int reflectionGap =
4;
int width = bitmap.getWidth();
int height =
bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1,
-1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
0, height/2,
width, height/2, matrix, false);
Bitmap bitmapWithReflection =
Bitmap.createBitmap(width, (height + height/2),
Config.ARGB_8888);
Canvas
canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0,
null);
Paint deafalutPaint = new Paint();
Generated by Foxit PDF Creator ?
Foxit Software
http://www.foxitsoftware.com For evaluation
only.
canvas.drawRect(0, height,width,height +
reflectionGap,
deafalutPaint);
canvas.drawBitmap(reflectionImage, 0,
height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient
shader = new LinearGradient(0,
bitmap.getHeight(), 0,
bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff,
TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be
porter duff and destination in
paint.setXfermode(new
PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with
our linear gradient
canvas.drawRect(0, height, width,
bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return
bitmapWithReflection;
}
}

android Bitmap用法总结,布布扣,bubuko.com

时间: 2024-08-02 15:10:08

android Bitmap用法总结的相关文章

Android Bitmap 用法总结

android Bitmap用法总结 Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 1.Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsi

android Bitmap用法总结(转载)

Bitmap用法总结1.Drawable → Bitmappublic static Bitmap drawableToBitmap(Drawable drawable) {Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_888

Android Bitmap用法大全,以后再也不担心了

1.Drawable → Bitmap public static Bitmap drawableToBitmap(Drawable drawable) { Bitmap bitmap = Bitmap .createBitmap( drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 :

Android Bitmap 加载多张图片的缓存处理(精华二)

一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~不用重新下载图片但是手机内存是很有限的~当缓存的图片越来越多,即使单张图片不是很大,不过数量太多时仍然会出现OOM的情况了~本篇则是讨论多张图片的处理问题 -----------------------------------------------------------------------

Android Bitmap 全面解析(二)加载多张图片的缓存处理

一般少量图片是很少出现OOM异常的,除非单张图片过~大~ 那么就可以用教程一里面的方法了通常应用场景是listview列表加载多张图片,为了提高效率一般要缓存一部分图片,这样方便再次查看时能快速显示~不用重新下载图片但是手机内存是很有限的~当缓存的图片越来越多,即使单张图片不是很大,不过数量太多时仍然会出现OOM的情况了~本篇则是讨论多张图片的处理问题-----------------------------------------------------------------------图片

Android Bitmap 常见的几个操作:缩放,裁剪,旋转,偏移

Android Bitmap 相关操作 常见的几个操作:缩放,裁剪,旋转,偏移      很多操作需要 Matrix 来支持:Matrix 通过矩阵来处理位图,计算出各个像素点的位置,从而把bitmap显示出来. matrix里有一个3x3的矩阵,用于图像处理: MSCALE_X MSKEW_X MTRANS_X MSKEW_Y MSCALE_Y MTRANS_Y MPERSP_0 MPERSP_1 MPERSP_2 根据变量名能猜出具体的用途:缩放X 偏移X 平移X偏移Y 缩放Y 平移Y透视0

Android Bitmap 开源图片框架分析(精华三)

主要介绍这三个框架,都挺有名的,其他的框架估计也差不多了 Android-Universal-Image-Loaderhttps://github.com/nostra13/Android-Universal-Image-Loader ImageLoaderhttps://github.com/novoda/ImageLoader Volley(综合框架,包含图片部分)https://github.com/mcxiaoke/android-volley 扯淡时间,可以跳过这段这些开源框架的源码还

Android Bitmap 开源图片框架分析(精华四)

disk缓存主要难点在于内存缓存,disk缓存其实比较简单,就是图片加载完成后把图片文件存到本地方便下次使用 同样,先贴一下官方主页的介绍(主页地址见文章最开始处)和内存缓存差不多,根据算法不同提供了几种类别,可以自行通过ImageLoaderConfiguration.discCache(..)设置<ignore_js_op> 硬盘缓存,保存是以文件的形式框架提供了4种类型,具体算法规则不同,看名字我们大概也能知道对应意思 UnlimitedDiscCache                

Android Bitmap 全面解析(四)图片处理效果对比 ...

对比对象: UIL Volley 官方教程中的方法(此系列教程一里介绍的,ImageLoader的处理方法和官方的差不多) ------------------------------------------------------------------------ 首先单张图片的压缩处理,也是分析重点 专门撸了一个小demo(结尾会放出下载连接)将对应计算方法copy了出来,然后计算了几十组数据,进行了对比 原图宽高都是一个10000以内的随机整数,限定大小是400 200,然后进行压缩处理