Bitmap用法总结
1、Drawable → Bitmap

Java代码 [java]view plaincopy
  1. 1、Drawable→Bitmap
  2. publicstaticBitmapdrawableToBitmap(Drawabledrawable){
  3. Bitmapbitmap=Bitmap
  4. .createBitmap(
  5. drawable.getIntrinsicWidth(),
  6. drawable.getIntrinsicHeight(),
  7. drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
  8. :Bitmap.Config.RGB_565);
  9. Canvascanvas=newCanvas(bitmap);
  10. //canvas.setBitmap(bitmap);
  11. drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
  12. drawable.getIntrinsicHeight());
  13. drawable.draw(canvas);
  14. returnbitmap;
  15. }
2、从资源中获取Bitmap
Java代码
  1. <span style="font-size: medium;">Resources res=getResources();
  2. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);</span>


3、Bitmap → byte[]

Java代码
  1. privatebyte[] Bitmap2Bytes(Bitmap bm){
  2. ByteArrayOutputStream baos =newByteArrayOutputStream();
  3. bm.compress(Bitmap.CompressFormat.PNG,100, baos);
  4. returnbaos.toByteArray();</span>
4、byte[] → Bitmap
Java代码
  1. privateBitmap Bytes2Bimap(byte[] b){
  2. if(b.length!=0){
  3. returnBitmapFactory.decodeByteArray(b,0, b.length);
  4. }
  5. else{
  6. returnnull;
  7. }
  8. }</span>


5、保存bitmap

Java代码
  1. staticbooleansaveBitmap2file(Bitmap bmp,String filename){
  2. CompressFormat format= Bitmap.CompressFormat.JPEG;
  3. intquality =100;
  4. OutputStream stream =null;
  5. try{
  6. stream =newFileOutputStream("/sdcard/"+ filename);
  7. }catch(FileNotFoundException e) {
  8. // TODO Auto-generated catch block
  9. Generated by Foxit PDF Creator © Foxit Software
  10. http://www.foxitsoftware.com For evaluation only.
  11. e.printStackTrace();
  12. }
  13. returnbmp.compress(format, quality, stream);
  14. }</span>


6、将图片按自己的要求缩放

Java代码
  1. // 图片源
  2. Bitmap bm = BitmapFactory.decodeStream(getResources()
  3. .openRawResource(R.drawable.dog));
  4. // 获得图片的宽高
  5. intwidth = bm.getWidth();
  6. intheight = bm.getHeight();
  7. // 设置想要的大小
  8. intnewWidth =320;
  9. intnewHeight =480;
  10. // 计算缩放比例
  11. floatscaleWidth = ((float) newWidth) / width;
  12. floatscaleHeight = ((float) newHeight) / height;
  13. // 取得想要缩放的matrix参数
  14. Matrix matrix =newMatrix();
  15. matrix.postScale(scaleWidth, scaleHeight);
  16. // 得到新的图片
  17. Bitmap newbm = Bitmap.createBitmap(bm,0,0, width, height, matrix,
  18. true);
  19. // 放在画布上
  20. canvas.drawBitmap(newbm,0,0, paint);</span>
相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
7、bitmap的用法小结
Java代码
  1. BitmapFactory.Options option =newBitmapFactory.Options();
  2. option.inSampleSize =2;//将图片设为原来宽高的1/2,防止内存溢出
  3. Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
  4. URL url =newURL("");
  5. InputStream is = url.openStream();
  6. Bitmap bm = BitmapFactory.decodeStream(is);
  7. android:scaleType:
  8. android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType /
  9. android:scaleType值的意义区别:
  10. CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分
  11. 显示
  12. CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长
  13. (宽)
  14. CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片
  15. 长/宽等于或小于View的长/宽
  16. Generated by Foxit PDF Creator © Foxit Software
  17. http://www.foxitsoftware.com For evaluation only.
  18. FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示
  19. FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
  20. FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
  21. FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示
  22. MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。</span>

8. 放大缩小图片
Java代码
  1. publicstaticBitmap zoomBitmap(Bitmap bitmap,intw,inth){
  2. intwidth = bitmap.getWidth();
  3. intheight = bitmap.getHeight();
  4. Matrix matrix =newMatrix();
  5. floatscaleWidht = ((float)w / width);
  6. floatscaleHeight = ((float)h / height);
  7. matrix.postScale(scaleWidht, scaleHeight);
  8. Bitmap newbmp = Bitmap.createBitmap(bitmap,0,0, width, height, matrix,
  9. true);
  10. returnnewbmp;
  11. }</span>
9. 将Drawable转化为Bitmap
Java代码
  1. publicstaticBitmap drawableToBitmap(Drawable drawable){
  2. intwidth = drawable.getIntrinsicWidth();
  3. intheight = drawable.getIntrinsicHeight();
  4. Bitmap bitmap = Bitmap.createBitmap(width, height,
  5. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  6. : Bitmap.Config.RGB_565);
  7. Canvas canvas =newCanvas(bitmap);
  8. drawable.setBounds(0,0,width,height);
  9. drawable.draw(canvas);
  10. returnbitmap;
  11. Generated by Foxit PDF Creator © Foxit Software
  12. http://www.foxitsoftware.com For evaluation only.
  13. }</span>


10. 获得圆角图片的方法

Java代码
  1. publicstaticBitmap getRoundedCornerBitmap(Bitmap bitmap,floatroundPx){
  2. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
  3. .getHeight(), Config.ARGB_8888);
  4. Canvas canvas =newCanvas(output);
  5. finalintcolor =0xff424242;
  6. finalPaint paint =newPaint();
  7. finalRect rect =newRect(0,0, bitmap.getWidth(), bitmap.getHeight());
  8. finalRectF rectF =newRectF(rect);
  9. paint.setAntiAlias(true);
  10. canvas.drawARGB(0,0,0,0);
  11. paint.setColor(color);
  12. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  13. paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
  14. canvas.drawBitmap(bitmap, rect, rect, paint);
  15. returnoutput;
  16. }</span>
11. 获得带倒影的图片方法
Java代码
  1. publicstaticBitmap createReflectionImageWithOrigin(Bitmap bitmap){
  2. finalintreflectionGap =4;
  3. intwidth = bitmap.getWidth();
  4. intheight = bitmap.getHeight();
  5. Matrix matrix =newMatrix();
  6. matrix.preScale(1, -1);
  7. Bitmap reflectionImage = Bitmap.createBitmap(bitmap,
  8. 0, height/2, width, height/2, matrix,false);
  9. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2),
  10. Config.ARGB_8888);
  11. Canvas canvas =newCanvas(bitmapWithReflection);
  12. canvas.drawBitmap(bitmap,0,0,null);
  13. Paint deafalutPaint =newPaint();
  14. Generated by Foxit PDF Creator © Foxit Software
  15. http://www.foxitsoftware.com For evaluation only.
  16. canvas.drawRect(0, height,width,height + reflectionGap,
  17. deafalutPaint);
  18. canvas.drawBitmap(reflectionImage,0, height + reflectionGap,null);
  19. Paint paint =newPaint();
  20. LinearGradient shader =newLinearGradient(0,
  21. bitmap.getHeight(),0, bitmapWithReflection.getHeight()
  22. + reflectionGap,0x70ffffff,0x00ffffff, TileMode.CLAMP);
  23. paint.setShader(shader);
  24. // Set the Transfer mode to be porter duff and destination in
  25. paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
  26. // Draw a rectangle using the paint with our linear gradient
  27. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
  28. + reflectionGap, paint);
  29. returnbitmapWithReflection;
  30. }
  31. }<

更多相关文章

  1. Android(安卓)webview上传图片(相册/相机)
  2. Android(安卓)主动 try-catch 主线程的 Exception
  3. Android(安卓)活动的最佳实践
  4. android context调用startActivity时报错:Calling startActivity(
  5. Android(安卓)JNI和NDK学习(2)--编程入门
  6. android 系统定制的小技巧
  7. Android判断是Wifi还是4G网络代码
  8. Android(安卓)Bitmap保存为.bmp格式,图像转化为黑白图片

随机推荐

  1. Android巴士
  2. 如何解决App无法收到android开机广播
  3. 在Android中根据联系人查询电话号码
  4. windows下载android源代码
  5. Android中显示照片的Exif信息
  6. 高仿Android 点心桌面皮肤实现方式
  7. 【Android车载系统 News | Tech 3】News
  8. 博客园app for xamarin android一款简洁
  9. Android中的sqlite简单示例
  10. android studio 3.6.0 绑定视图新特性的