Android____View-->Bitmap
16lz
2021-01-24
1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap
main.xml文件如下:
view plain print ?- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"android:layout_width="256px"
- android:layout_height="256px">
- <TextViewandroid:layout_width="wrap_content"android:id="@+id/city"
- android:layout_height="wrap_content"android:textSize="20px"
- android:textColor="#ffffff"android:shadowColor="#0000AA"
- android:shadowDx="0"android:shadowDy="-2"android:shadowRadius="0.1"
- android:layout_gravity="right"android:layout_marginRight="5px"/>
- </LinearLayout>
java代码中的处理,方法一:
- //加载xml布局文件
- LayoutInflaterfactory=LayoutInflater.from(context);
- Viewview=factory.inflate(R.layout.main,null);
- //获得布局文件中的TextView
- TextViewcity=(TextView)view.findViewById(R.id.city);
- //设置city的文本信息
- city.setText("xml中的textview");
- //启用绘图缓存
- view.setDrawingCacheEnabled(true);
- //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null
- view.measure(MeasureSpec.makeMeasureSpec(256,MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(256,MeasureSpec.EXACTLY));
- //这个方法也非常重要,设置布局的尺寸和位置
- view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
- //获得绘图缓存中的Bitmap
- view.buildDrawingCache();
- Bitmapbitmap=view.getDrawingCache();
java代码中的处理,方法二:
view plain print ?
- //加载xml布局文件
- LayoutInflaterfactory=LayoutInflater.from(context);
- Viewview=factory.inflate(R.layout.main,null);
- //获得布局文件中的TextView
- TextViewcity=(TextView)view.findViewById(R.id.city);
- //设置city的文本信息
- city.setText("xml中的textview");
- //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null
- view.measure(MeasureSpec.makeMeasureSpec(256,MeasureSpec.EXACTLY),
- MeasureSpec.makeMeasureSpec(256,MeasureSpec.EXACTLY));
- //这个方法也非常重要,设置布局的尺寸和位置
- view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());
- //生成bitmap
- Bitmapbitmap=Bitmap.createBitmap(view.getWidth(),view.getHeight(),
- Bitmap.Config.RGB_565);
- //利用bitmap生成画布
- Canvascanvas=newCanvas(bitmap);
- //把view中的内容绘制在画布上
- view.draw(canvas);
2)Bitmap转换为byte[]数组
方法一:
view plain print ?- privatebyte[]Bitmap_To_Bytes(Bitmapbitmap){
- ByteArrayOutputStreambaos=newByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG,100,baos);
- returnbaos.toByteArray();
- }
方法二:
view plain print ?- publicstaticbyte[]readStream(InputStreaminStream)throwsException{
- byte[]buffer=newbyte[1024];
- intlen=-1;
- ByteArrayOutputStreambaos=newByteArrayOutputStream();
- while((len=inStream.read(buffer))!=-1){
- baos.write(buffer,0,len);
- }
- byte[]data=baos.toByteArray();
- baos.close();
- inStream.close();
- returndata;
- }
3)设置在应用中支持32位的图像:
在onCreate()函数中加入以下两行:
getWindow().setFormat(PixelFormat.RGBX_8888);
BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
以使之 支持32bit的图像。
更多相关文章
- android注入代码之注入类方法
- Android(安卓)库文件的编译(静态和动态库 Android.mk)
- android使用activitygroup和Scrollview的方法
- Android递归拷贝assets资源到指定目录
- android studio中gradle更新办法
- Android中获取文件路径的方法总结及对照
- Android(安卓)命令行打包和签名
- Android(安卓)ScrollView的具体使用
- Android调试程序技巧