Android实现将控件等View转化为Bitmap对象,通过ImageView显示的功能
2016-03-17 12:15
633 查看
Android实现将控件等View转化为Bitmap对象,通过ImageView显示的功能
例子:布局Layout代码<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.dateselector.MainActivity" > <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" /> <LinearLayout android:id="@+id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我是好人" android:layout_centerHorizontal="true"/> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerHorizontal="true" android:src="@drawable/ic_launcher" /> </LinearLayout> </LinearLayout>
对应视图
MainActivity代码
package com.example.dateselector; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends Activity { private ImageView imageView; private LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); layout = (LinearLayout) findViewById(R.id.layout); // 转化图片 Bitmap bitmap = getViewBitmap(layout, 300, 300); imageView.setImageBitmap(bitmap); } /** * 把View绘制到Bitmap上 * * @param view * 需要绘制的View * @param width * 该View的宽度 * @param height * 该View的高度 * @return 返回Bitmap对象 add by csj 13-11-6 */ public Bitmap getViewBitmap(View comBitmap, int width, int height) { Bitmap bitmap = null; if (comBitmap != null) { comBitmap.clearFocus(); comBitmap.setPressed(false); boolean willNotCache = comBitmap.willNotCacheDrawing(); comBitmap.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = comBitmap.getDrawingCacheBackgroundColor(); comBitmap.setDrawingCacheBackgroundColor(0); float alpha = comBitmap.getAlpha(); comBitmap.setAlpha(1.0f); if (color != 0) { comBitmap.destroyDrawingCache(); } int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); comBitmap.measure(widthSpec, heightSpec); comBitmap.layout(0, 0, width, height); comBitmap.buildDrawingCache(); Bitmap cacheBitmap = comBitmap.getDrawingCache(); if (cacheBitmap == null) { Log.e("view.ProcessImageToBlur", "failed getViewBitmap(" + comBitmap + ")", new RuntimeException()); return null; } bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view comBitmap.setAlpha(alpha); comBitmap.destroyDrawingCache(); comBitmap.setWillNotCacheDrawing(willNotCache); comBitmap.setDrawingCacheBackgroundColor(color); } return bitmap; } }
下面的方法就是实现绘制View的方法
/** * 把View绘制到Bitmap上 * * @param view * 需要绘制的View * @param width * 该View的宽度 * @param height * 该View的高度 * @return 返回Bitmap对象 add by csj 13-11-6 */ public Bitmap getViewBitmap(View comBitmap, int width, int height) { Bitmap bitmap = null; if (comBitmap != null) { comBitmap.clearFocus(); comBitmap.setPressed(false); boolean willNotCache = comBitmap.willNotCacheDrawing(); comBitmap.setWillNotCacheDrawing(false); // Reset the drawing cache background color to fully transparent // for the duration of this operation int color = comBitmap.getDrawingCacheBackgroundColor(); comBitmap.setDrawingCacheBackgroundColor(0); float alpha = comBitmap.getAlpha(); comBitmap.setAlpha(1.0f); if (color != 0) { comBitmap.destroyDrawingCache(); } int widthSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int heightSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); comBitmap.measure(widthSpec, heightSpec); comBitmap.layout(0, 0, width, height); comBitmap.buildDrawingCache(); Bitmap cacheBitmap = comBitmap.getDrawingCache(); if (cacheBitmap == null) { Log.e("view.ProcessImageToBlur", "failed getViewBitmap(" + comBitmap + ")", new RuntimeException()); return null; } bitmap = Bitmap.createBitmap(cacheBitmap); // Restore the view comBitmap.setAlpha(alpha); comBitmap.destroyDrawingCache(); comBitmap.setWillNotCacheDrawing(willNotCache); comBitmap.setDrawingCacheBackgroundColor(color); } return bitmap; }
效果就是将下面的View转化为bitmap图片,显示在上面的ImageView上面了
初来乍到,请各位客官多多提建议,写的不好的,勿喷!
相关文章推荐
- Android 检查设备是否存在 导航栏 NavigationBar
- Android开发技巧——大图裁剪
- Android开发技巧——大图裁剪
- Android开发技巧——大图裁剪
- Android 之常用工具类(后续)
- Android之framework修改底部导航栏NavigationBar动态显示和隐藏
- android 过场动画如何加载
- android 开发技巧
- 《Android框架揭秘》读书笔记——JNI
- android开发 装饰者模式
- Android Studio 插件推荐(合集)
- Android 常用小代码
- Android项目EditText焦点问题
- android圆角矩形图片的实现进而讨论view自定义的要点
- android studio快捷键大全
- android sdk manager 代理设置
- Android悬浮窗进阶版-上篇(思路篇)
- Android四大基本组件介绍与生命周期
- android GridView android:stretchMode="columnWidth"
- Android Activity内镶嵌Activity