android Bitmap

来源:互联网 发布:ubuntu 实时聊天工具 编辑:程序博客网 时间:2024/05/16 09:48

 1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap

main.xml文件如下:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="256px"  
  4.     android:layout_height="256px">  
  5.     <TextView android:layout_width="wrap_content" android:id="@+id/city"  
  6.         android:layout_height="wrap_content" android:textSize="20px"  
  7.         android:textColor="#ffffff" android:shadowColor="#0000AA"  
  8.         android:shadowDx="0" android:shadowDy="-2" android:shadowRadius="0.1"  
  9.         android:layout_gravity="right" android:layout_marginRight="5px" />  
  10.     </LinearLayout>  

 

java代码中的处理,方法一:

[java] view plaincopyprint?
  1. //加载xml布局文件  
  2.         LayoutInflater factory = LayoutInflater.from(context);  
  3.         View view = factory.inflate(R.layout.main, null);  
  4.         //获得布局文件中的TextView  
  5.         TextView city = (TextView) view.findViewById(R.id.city);  
  6.         //设置city的文本信息  
  7.         city.setText("xml中的textview");    
  8.         //启用绘图缓存  
  9.         view.setDrawingCacheEnabled(true);        
  10.         //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null  
  11.         view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),  
  12.                 MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));  
  13.         //这个方法也非常重要,设置布局的尺寸和位置  
  14.         view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  15.         //获得绘图缓存中的Bitmap  
  16.         view.buildDrawingCache();  
  17.         Bitmap bitmap = view.getDrawingCache();  

 

java代码中的处理,方法二:

 

[java] view plaincopyprint?
  1. //加载xml布局文件  
  2.         LayoutInflater factory = LayoutInflater.from(context);  
  3.         View view = factory.inflate(R.layout.main, null);  
  4.         //获得布局文件中的TextView  
  5.         TextView city = (TextView) view.findViewById(R.id.city);  
  6.         //设置city的文本信息  
  7.         city.setText("xml中的textview");    
  8.         //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null  
  9.         view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),  
  10.                 MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));  
  11.         //这个方法也非常重要,设置布局的尺寸和位置  
  12.         view.layout(00, view.getMeasuredWidth(), view.getMeasuredHeight());  
  13.         //生成bitmap  
  14.         Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),  
  15.                 Bitmap.Config.RGB_565);  
  16.         //利用bitmap生成画布  
  17.         Canvas canvas = new Canvas(bitmap);  
  18.         //把view中的内容绘制在画布上  
  19.         view.draw(canvas);  



2)Bitmap转换为byte[]数组

方法一:

[java] view plaincopyprint?
  1. private byte[] Bitmap_To_Bytes(Bitmap bitmap){     
  2.     ByteArrayOutputStream baos = new ByteArrayOutputStream();       
  3.     bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);       
  4.     return baos.toByteArray();     
  5.    }   


 

方法二:

[java] view plaincopyprint?
  1. public static byte[] readStream(InputStream inStream) throws Exception {     
  2.         byte[] buffer = new byte[1024];    
  3.         int len = -1;    
  4.         ByteArrayOutputStream baos  = new ByteArrayOutputStream();    
  5.         while ((len = inStream.read(buffer)) != -1) {    
  6.             baos .write(buffer, 0, len);    
  7.         }    
  8.         byte[] data = baos .toByteArray();    
  9.         baos .close();    
  10.         inStream.close();    
  11.         return data;    
  12.     
  13.     }    


3)设置在应用中支持32位的图像:

在onCreate()函数中加入以下两行:

[java] view plaincopyprint?
  1. getWindow().setFormat(PixelFormat.RGBX_8888);  
  2. BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);  


以使之 支持32bit的图像。

 

4)把drawable文件夹下的文件转成Bitmap

[java] view plaincopyprint?
  1. Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(),  
  2.         R.drawable.down);  

原创粉丝点击