Bitmap用法总结
1、Drawable → Bitmap

Java代码 复制代码  [java] view plaincopy  
  1. 1、Drawable → Bitmap  
  2. public static Bitmap drawableToBitmap(Drawable drawable) {  
  3. Bitmap bitmap = Bitmap  
  4. .createBitmap(  
  5. drawable.getIntrinsicWidth(),  
  6. drawable.getIntrinsicHeight(),  
  7. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
  8. : Bitmap.Config.RGB_565);  
  9. Canvas canvas = new Canvas(bitmap);  
  10. // canvas.setBitmap(bitmap);  
  11. drawable.setBounds(0,0, drawable.getIntrinsicWidth(),  
  12. drawable.getIntrinsicHeight());  
  13. drawable.draw(canvas);  
  14. return bitmap;  
  15. }  
2、从资源中获取Bitmap
Java代码 复制代码 收藏代码
  1. "font-size: medium;">Resources res=getResources();
  2. Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);


3、Bitmap → byte[]

Java代码 复制代码 收藏代码
  1. private byte[] Bitmap2Bytes(Bitmap bm){
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  4. return baos.toByteArray();
4、byte[] → Bitmap
Java代码 复制代码 收藏代码
  1. private Bitmap Bytes2Bimap(byte[] b){
  2. if(b.length!=0){
  3. return BitmapFactory.decodeByteArray(b,0, b.length);
  4. }
  5. else {
  6. return null;
  7. }
  8. }


5、保存bitmap

Java代码 复制代码 收藏代码
  1. static boolean saveBitmap2file(Bitmap bmp,String filename){
  2. CompressFormat format= Bitmap.CompressFormat.JPEG;
  3. int quality = 100;
  4. OutputStream stream = null;
  5. try {
  6. stream = new FileOutputStream("/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. return bmp.compress(format, quality, stream);
  14. }


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

Java代码 复制代码 收藏代码
  1. // 图片源
  2. Bitmap bm = BitmapFactory.decodeStream(getResources()
  3. .openRawResource(R.drawable.dog));
  4. // 获得图片的宽高
  5. int width = bm.getWidth();
  6. int height = bm.getHeight();
  7. // 设置想要的大小
  8. int newWidth = 320;
  9. int newHeight = 480;
  10. // 计算缩放比例
  11. float scaleWidth = ((float) newWidth) / width;
  12. float scaleHeight = ((float) newHeight) / height;
  13. // 取得想要缩放的matrix参数
  14. Matrix matrix = new Matrix();
  15. matrix.postScale(scaleWidth, scaleHeight);
  16. // 得到新的图片
  17. Bitmap newbm = Bitmap.createBitmap(bm, 00, width, height, matrix,
  18. true);
  19. // 放在画布上
  20. canvas.drawBitmap(newbm, 0,0, paint);
相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html
7、bitmap的用法小结
Java代码 复制代码 收藏代码
  1. BitmapFactory.Options option =new BitmapFactory.Options();
  2. option.inSampleSize = 2;//将图片设为原来宽高的1/2,防止内存溢出
  3. Bitmap bm = BitmapFactory.decodeFile("",option);//文件流
  4. URL url = new URL("");
  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 用矩阵来绘制,动态缩小放大图片来显示。

8. 放大缩小图片
Java代码 复制代码 收藏代码
  1. public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
  2. int width = bitmap.getWidth();
  3. int height = bitmap.getHeight();
  4. Matrix matrix = new Matrix();
  5. float scaleWidht = ((float)w / width);
  6. float scaleHeight = ((float)h / height);
  7. matrix.postScale(scaleWidht, scaleHeight);
  8. Bitmap newbmp = Bitmap.createBitmap(bitmap, 00, width, height, matrix,
  9. true);
  10. return newbmp;
  11. }
9. 将Drawable转化为Bitmap
Java代码 复制代码 收藏代码
  1. public static Bitmap drawableToBitmap(Drawable drawable){
  2. int width = drawable.getIntrinsicWidth();
  3. int height = 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 = new Canvas(bitmap);
  8. drawable.setBounds(0,0,width,height);
  9. drawable.draw(canvas);
  10. return bitmap;
  11. Generated by Foxit PDF Creator © Foxit Software
  12. http://www.foxitsoftware.com For evaluation only.
  13. }


10. 获得圆角图片的方法

Java代码 复制代码 收藏代码
  1. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
  2. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
  3. .getHeight(), Config.ARGB_8888);
  4. Canvas canvas = new Canvas(output);
  5. final int color = 0xff424242;
  6. final Paint paint =new Paint();
  7. final Rect rect = new Rect(0,0, bitmap.getWidth(), bitmap.getHeight());
  8. final RectF rectF =new RectF(rect);
  9. paint.setAntiAlias(true);
  10. canvas.drawARGB(00,0,0);
  11. paint.setColor(color);
  12. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  13. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  14. canvas.drawBitmap(bitmap, rect, rect, paint);
  15. return output;
  16. }
11. 获得带倒影的图片方法
Java代码 复制代码 收藏代码
  1. public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap){
  2. final int reflectionGap =4;
  3. int width = bitmap.getWidth();
  4. int height = bitmap.getHeight();
  5. Matrix matrix = new Matrix();
  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 = new Canvas(bitmapWithReflection);
  12. canvas.drawBitmap(bitmap, 0,0,null);
  13. Paint deafalutPaint = new Paint();
  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 = new Paint();
  20. LinearGradient shader = new LinearGradient(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(new PorterDuffXfermode(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. return bitmapWithReflection;
  30. }
  31. }<

更多相关文章

  1. android > 获取图片 从 本地 /相机
  2. Android实用代码片段(一)
  3. Android ListView异步加载网络图片
  4. Android使用HttpURLConnection显示网络图片
  5. fanfou(饭否) android客户端 代码学习1
  6. android短信发送器源代码
  7. Android Media Recorder录音播放源代码
  8. android 加载 网络图片

随机推荐

  1. android启动之init进程详解
  2. android 线程 synchronized关键字
  3. android4.4调整音量调节速度
  4. Android下面的MD5加密
  5. android rle格式开机logo制作
  6. android从sdcard加载.9.png图片
  7. android打电话
  8. Android开发如何正确使用WebView
  9. android sha1和签名证书的学习
  10. android客户端和struts框架之间的通信