android Bitmap(将视图转为bitmap对象)
来源:互联网 发布:最搞笑的网络歪歌 编辑:程序博客网 时间:2024/06/08 05:43
1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap
main.xml文件如下:
- <?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>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="256px"android:layout_height="256px"><TextView android: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布局文件
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.main, null);
- //获得布局文件中的TextView
- TextView city = (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();
- Bitmap bitmap = view.getDrawingCache();
//加载xml布局文件LayoutInflater factory = LayoutInflater.from(context);View view = factory.inflate(R.layout.main, null);//获得布局文件中的TextViewTextView city = (TextView) view.findViewById(R.id.city);//设置city的文本信息city.setText("xml中的textview");//启用绘图缓存view.setDrawingCacheEnabled(true);//调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为nullview.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));//这个方法也非常重要,设置布局的尺寸和位置view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());//获得绘图缓存中的Bitmapview.buildDrawingCache();Bitmap bitmap = view.getDrawingCache();
java代码中的处理,方法二:
- //加载xml布局文件
- LayoutInflater factory = LayoutInflater.from(context);
- View view = factory.inflate(R.layout.main, null);
- //获得布局文件中的TextView
- TextView city = (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
- Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
- Bitmap.Config.RGB_565);
- //利用bitmap生成画布
- Canvas canvas = new Canvas(bitmap);
- //把view中的内容绘制在画布上
- view.draw(canvas);
//加载xml布局文件LayoutInflater factory = LayoutInflater.from(context);View view = factory.inflate(R.layout.main, null);//获得布局文件中的TextViewTextView city = (TextView) view.findViewById(R.id.city);//设置city的文本信息city.setText("xml中的textview");//调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为nullview.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY));//这个方法也非常重要,设置布局的尺寸和位置view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());//生成bitmapBitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.RGB_565);//利用bitmap生成画布Canvas canvas = new Canvas(bitmap);//把view中的内容绘制在画布上view.draw(canvas);
2)Bitmap转换为byte[]数组
方法一:
- privatebyte[] Bitmap_To_Bytes(Bitmap bitmap){
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
- return baos.toByteArray();
- }
private byte[] Bitmap_To_Bytes(Bitmap bitmap){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
方法二:
- publicstaticbyte[] readStream(InputStream inStream) throws Exception {
- byte[] buffer = newbyte[1024];
- int len = -1;
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- while ((len = inStream.read(buffer)) != -1) {
- baos .write(buffer, 0, len);
- }
- byte[] data = baos .toByteArray();
- baos .close();
- inStream.close();
- return data;
- }
public static byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { baos .write(buffer, 0, len); } byte[] data = baos .toByteArray(); baos .close(); inStream.close(); return data; }
3)设置在应用中支持32位的图像:
在onCreate()函数中加入以下两行:
- getWindow().setFormat(PixelFormat.RGBX_8888);
- BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
getWindow().setFormat(PixelFormat.RGBX_8888); BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
以使之 支持32bit的图像。
4)把drawable文件夹下的文件转成Bitmap
- Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(),
- R.drawable.down);
0 0
- android Bitmap(将视图转为bitmap对象)
- android 将View转为Bitmap
- Android将View转为Bitmap
- 将Drawable转为Bitmap
- android将 View转为Bitmap图片
- drawable转为bitmap对象
- Android中将资源文件转为Bitmap对象
- Android 文字转为bitmap
- 将Richviewedit内容转为bitmap
- 如何将Drawable转为Bitmap?
- 将bitmap转为File文件
- 把Bitmap对象转为文件
- URL转为bitmap对象(xutils3超简单)
- 如何将xml中的VectorDrawable转为Bitmap
- Android中图片处理(压缩 保存 显示 截屏 视图View转为图片Bitmap)函数工具类
- Android 怎么把imageview 转为Bitmap
- Android 怎么把imageview 转为Bitmap
- Android 怎么把imageview 转为Bitmap
- windows Bat 批处理脚本 教程
- IOS获取当前经纬度 有时不调用代理方法
- HDU 5128
- J2EE --- Statement VS PreparedStatement
- netty:protobuf的编码器ProtobufVarint32LengthFieldPrepender()、解码器ProtobufVarint32FrameDecoder()
- android Bitmap(将视图转为bitmap对象)
- CGLIB介绍与原理
- ByteBuffer常用方法详解
- GIT 的使用
- NumberPicker实现时间选择器,并且可以选择一定范围内的时间
- SMBIOS规范学习
- 第三章 垃圾收集器与内存分配策略
- C语言学习第一天
- Java --- 并发库concurrent包学习总结