1. /** 获取 drawable 的图片 可以循环 1.图名 2.drawable 3.包名 **/
  2.  
  3. int imgid = getResources().getIdentifier("ic_launcher", "drawable", "com.example.anywight");
  4. text.setBackgroundResource(imgid);
  5.  
  6. /** 通过图片id获得Bitmap **/
  7. Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
  8.  
  9. /** 通过 assest 获取 获得Drawable bitmap **/
  10. InputStream in = this.getAssets().open("ic_launcher");
  11. Drawable da = Drawable.createFromStream(in, null);
  12. Bitmap mm = BitmapFactory.decodeStream(in);
  13.  
  14. /** 通过 sdcard 获得 bitmap **/
  15. Bitmap bit = BitmapFactory.decodeFile("/sdcard/android.jpg");
  16.  
  17. /** view转Bitmap **/
  18. public static Bitmap convertViewToBitmap(View view, int bitmapWidth, int bitmapHeight){
  19.  
  20. Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
  21. view.draw(new Canvas(bitmap));
  22. return bitmap;
  23. }
  24.  
  25. /** 将控件转换为bitmap **/
  26. public static Bitmap convertViewToBitMap(View view){
  27. // 打开图像缓存
  28. view.setDrawingCacheEnabled(true);
  29. // 必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件
  30. // 测量View大小
  31. view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  32. // 发送位置和尺寸到View及其所有的子View
  33. view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
  34. // 获得可视组件的截图
  35. Bitmap bitmap = view.getDrawingCache();
  36. return bitmap;
  37. }
  38.  
  39. public static Bitmap getBitmapFromView(View view){
  40. Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
  41. Canvas canvas = new Canvas(returnedBitmap);
  42. Drawable bgDrawable = view.getBackground();
  43. if (bgDrawable != null)
  44. bgDrawable.draw(canvas);
  45. else
  46. canvas.drawColor(Color.WHITE);
  47. view.draw(canvas);
  48. return returnedBitmap;
  49. }
  50.  
  51. /** 获取屏幕截图的bitmap对象的代码如下 **/
  52. public Bitmap getScreenPic(View view){
  53.  
  54. View rootView = view.getRootView();
  55. rootView.setDrawingCacheEnabled(true);
  56. rootView.buildDrawingCache();
  57. // 不明白为什么这里返回一个空,有帖子说不能在oncreat方法中调用
  58. // 测量View大小
  59. rootView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
  60. // 发送位置和尺寸到View及其所有的子View
  61. rootView.layout(0, 0, rootView.getMeasuredWidth(), rootView.getMeasuredHeight());
  62. // 解决措施,调用上面的measure和layout方法之后,返回值就不再为空
  63. // 如果想要创建的是固定长度和宽度的呢?
  64. Bitmap bitmap = rootView.getDrawingCache();
  65. rootView.destroyDrawingCache();
  66. return bitmap;
  67. }
  68.  
  69. /** Drawable → Bitmap **/
  70. public static Bitmap drawableToBitmap(Drawable drawable){
  71.  
  72. Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
  73. Canvas canvas = new Canvas(bitmap);
  74. // canvas.setBitmap(bitmap);
  75. drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
  76. drawable.draw(canvas);
  77. return bitmap;
  78.  
  79. }
  80.  
  81. /** bitmap → drawable **/
  82. public static Drawable bitmapToDrawable(Context context,String filename){
  83.  
  84. Bitmap image = null;
  85. BitmapDrawable ddd = null;
  86. try {
  87. AssetManager am = context.getAssets();
  88. InputStream is = am.open(filename);
  89. image = BitmapFactory.decodeStream(is);
  90. ddd = new BitmapDrawable(context.getResources(), image);
  91. is.close();
  92. } catch (Exception e) {
  93. }
  94. return ddd;
  95.  
  96. }
  97.  
  98. /** byte[] → Bitmap **/
  99. public static Bitmap byteToDrawable(Context context,byte[] bb){
  100. Bitmap pp = BitmapFactory.decodeByteArray(bb, 0, bb.length);
  101. return pp;
  102. }
  103.  
  104. /** Bitmap → byte[]**/
  105. public static byte[] bitmapToByte(Bitmap bitmap){
  106.  
  107. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  108. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  109. byte[] yy = baos.toByteArray();
  110. return yy;
  111. }
  112.  
  113. /** 将text 转换成 bitmap **/
  114. public static Bitmap createTxtImage(String txt, int txtSize) {
  115. Bitmap mbmpTest = Bitmap.createBitmap(txt.length() * txtSize + 4,
  116. txtSize + 4, Config.ARGB_8888);
  117. Canvas canvasTemp = new Canvas(mbmpTest);
  118. Paint p = new Paint();
  119. p.setAntiAlias(true);
  120. p.setColor(Color.WHITE);
  121. p.setTextSize(txtSize);
  122. canvasTemp.drawText(txt, 2, txtSize - 2, p);
  123. return mbmpTest;
  124.  
  125. }
  126.  
  127. /** 显示将bitmap进行缩放 **/
  128. public Bitmap bitmapScanel(Context context){
  129. //通过openRawResource获取一个inputStream对象
  130. InputStream inputStream = context.getResources().openRawResource(R.id.backageground);
  131. //通过一个InputStream创建一个BitmapDrawable对象
  132. BitmapDrawable drawable = new BitmapDrawable(inputStream);
  133. //通过BitmapDrawable对象获得Bitmap对象
  134. Bitmap bitmap = drawable.getBitmap();
  135. //利用Bitmap对象创建缩略图
  136. bitmap = ThumbnailUtils.extractThumbnail(bitmap, 40, 40);
  137. return bitmap;
  138.  
  139. }
  140.  
  141. /** 放大缩小图片 **/
  142. public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){
  143. int width = bitmap.getWidth();
  144. int height = bitmap.getHeight();
  145. Matrix matrix = new Matrix();
  146. float scaleWidht = ((float)w / width);
  147. float scaleHeight = ((float)h / height);
  148. matrix.postScale(scaleWidht, scaleHeight);
  149. Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
  150. return newbmp;
  151. }
  152.  
  153. /** 获得圆角图片的方法 **/
  154. public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
  155.  
  156. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
  157. .getHeight(), Config.ARGB_8888);
  158. Canvas canvas = new Canvas(output);
  159.  
  160. final int color = 0xff424242;
  161. final Paint paint = new Paint();
  162. final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
  163. final RectF rectF = new RectF(rect);
  164.  
  165. paint.setAntiAlias(true);
  166. canvas.drawARGB(0, 0, 0, 0);
  167. paint.setColor(color);
  168. canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
  169.  
  170. paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
  171. canvas.drawBitmap(bitmap, rect, rect, paint);
  172.  
  173. return output;
  174. }
  175.  
  176. /** 对 bitmap 进行裁剪 **/
  177. public Bitmap bitmapClip(Context context , int id , int x , int y){
  178. Bitmap map = BitmapFactory.decodeResource(context.getResources(), id);
  179. map = Bitmap.createBitmap(map, x, y, 120, 120);
  180. return map;
  181. }
  182.  
  183. /**
  184. * 图片的倒影效果
  185. */
  186. public static Bitmap createReflectedImage(Bitmap originalImage) {
  187. final int reflectionGap = 4;
  188.  
  189. int width = originalImage.getWidth();
  190. int height = originalImage.getHeight();
  191.  
  192. Matrix matrix = new Matrix();
  193. matrix.preScale(1, -1);
  194.  
  195. // Create a Bitmap with the flip matrix applied to it.
  196. // We only want the bottom half of the image
  197. Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
  198. height / 2, width, height / 2, matrix, false);
  199.  
  200. // Create a new bitmap with same width but taller to fit reflection
  201. Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
  202. (height + height / 2), Config.ARGB_8888);
  203.  
  204. // Create a new Canvas with the bitmap that's big enough for
  205. // the image plus gap plus reflection
  206. Canvas canvas = new Canvas(bitmapWithReflection);
  207. // Draw in the original image
  208. canvas.drawBitmap(originalImage, 0, 0, null);
  209. // Draw in the gap
  210. Paint defaultPaint = new Paint();
  211. canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
  212. // Draw in the reflection
  213. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
  214.  
  215. // Create a shader that is a linear gradient that covers the reflection
  216. Paint paint = new Paint();
  217. LinearGradient shader = new LinearGradient(0,
  218. originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
  219. + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
  220. // Set the paint to use this shader (linear gradient)
  221. paint.setShader(shader);
  222. // Set the Transfer mode to be porter duff and destination in
  223. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  224. // Draw a rectangle using the paint with our linear gradient
  225. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
  226.  
  227. return bitmapWithReflection;
  228. }

android bitmap的 一些简单操作的更多相关文章

  1. Android Bitmap 载入与像素操作

    Android Bitmap 载入与像素操作 一:载入与像素读写 在Android SDK中,图像的像素读写能够通过getPixel与setPixel两个Bitmap的API实现. Bitmap AP ...

  2. [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888

    [Android]通过JNI访问并操作Bitmap的元素,支持RGB565和ARGB8888 标签: androidbitmapjni 2014-05-09 20:35 2985人阅读 评论(1) 收 ...

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

    Android Bitmap 相关操作 常见的几个操作:缩放,裁剪,旋转,偏移      很多操作需要 Matrix 来支持:Matrix 通过矩阵来处理位图,计算出各个像素点的位置,从而把bitma ...

  4. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  5. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

  6. Android Bitmap 和 ByteArray的互相转换

    Android Bitmap 和 ByteArray的互相转换 移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接处理图像 ...

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

    对比对象: UIL Volley 官方教程中的方法(此系列教程一里介绍的,ImageLoader的处理方法和官方的差不多) -------------------------------------- ...

  8. 36、Android Bitmap 全面解析

    Android Bitmap 全面解析(一)加载大尺寸图片 http://www.eoeandroid.com/thread-331669-1-1.html Android Bitmap 全面解析(二 ...

  9. Android bitmap图片处理

    一.View转换为Bitmap         在Android中所有的控件都是View的直接子类或者间接子类,通过它们可以组成丰富的UI界面.在窗口显示的时候Android会把这些控件都加载到内存中 ...

随机推荐

  1. 用Python高亮org-mode代码块

    文章同时可在我的github blog上阅读:http://cheukyin.github.io/python/2014-08/pygments-highlight-src-export-html.h ...

  2. Android JSON 解析库的使用 - Gson 和 fast-json

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...

  3. Day10 网络编程(续)

    作用域 Python的作用域是函数,没有块级作用域 if 1 == 1: name = 'wang' print(name) #wang   for i in range(10): name = i ...

  4. QLabel

    The QLabel widget provides a text or image display. Content  Setting Plain text  Pass a QString to s ...

  5. MAC 开发工具

    web开发编辑器 Espresso下载地址   密码: i9hr

  6. redis运行状态图形化监控工具 — RedisLive

    在Centos中部署redis运行状态图形化监控工具 — RedisLive   写在前面 前两天看到张善友老师的一篇文章<先定个小目标, 使用C# 开发的千万级应用>,里面给出了一张腾讯 ...

  7. MySQL 执行计划explain详解

    MySQL 执行计划explain详解 2015-08-10 13:56:27 分类: MySQL explain命令是查看查询优化器如何决定执行查询的主要方法.这个功能有局限性,并不总会说出真相,但 ...

  8. IOS--UISwitch的使用方法

    IOS--UISwitch的使用方法详细 (2013-08-24 11:09:38) 转载▼ 标签: uiswitch switch 选择控件 ios it 分类: iOS--UI // UISwit ...

  9. 【HDOJ】1813 Escape from Tetris

    bfs预处理一点到边界的最小距离,IDA*求出可行方案.注意按字典序初始化dir数组.并且存在中间点全为1,边界含0的可能性(wa了很多次).此时不输出任何命令. /* 1813 */ #includ ...

  10. java面向对象值类属语句块

    在我们之前学习语句的时候,我们讲过一种比较特殊的语句块,那就是局部代码块.局部代码块的作用是什么呢,就是把临时使用的变量放在里面,之后执行完之后,局部代码块中定义的变量会直接被释放,这样就避免了那些我 ...