- 浏览: 302153 次
- 性别:
- 来自: 南京
文章分类
最新评论
-
ggwang:
谢谢分享!
如何释放Ubuntu多余的空间?如何给Ubuntu扩容(install inside windows)? -
allenshao:
只有放枪咯~~~~~
Google Group Android Developers 无法打开的问题 -
malong26:
也打不开~~~
Google Group Android Developers 无法打开的问题 -
songshuang:
一直喂它会一直吃是不?
用你的鼠标逗逗它 -
allenshao:
现在基本上用免费的VPN,缺点是限流量。
如何访问appspot网站?
Sometimes, in our applications, we would like to do something like this: Create a view and then, convert it to a Bitmap to work with it as a Image. In this “How-to” we will learn how to do this.
Introduction
In some applications, we would like to create a View with texts, images, and more view-elements and convert it to a Bitmap. Why? Well, you don't know until you need it, but imagine that you are working with OpenGL, you want to add textures to a OpenGL object, and you want this textures view-like. This is just an example, but I'm positive that more functionality will come to your mind while you are developing in Android.
Short introduction to Bitmap and Canvas
First of all, I would like to write about the Bitmap and the Canvas classes. I think its important to know about this before start talking about “converting a view in a Bitmap using a Canvas”
In Android, the very known word “bitmap”, is a class that help us to work with images as a map of bits or an array of bits. This Bitmap class is very useful when we are working with graphics interfaces like Canvas or OpenGL.
Canvas, is other known class in Java, is used to draw things on it. In Canvas we have the control of all the pixels.
Converting View to Bitmap
We need to have some variables initialized,
This is the Canvas we are going to draw in.
This is the layout we are going to use to create our view.
This is the background we are going to set in our view, we get it from a resource file (R.drawable.blackBgrnd). The BitmapFactory.decodeResource method is used to get a resource image and convert it in a Bitmap (we will use the Bitmap class). The object mContext (context) must be passed from the Activity we are working on.
we need another bitmap, to draw it in the canvas. We set the width and the height of this bitmap related with the width and height we have created our layout. Now this Bitmap is empty, but we are going to associate it to the canvas, so every time we draw in the canvas, it will be drawn in this bitmap object.
First of all, we had the canvas = null, now we create a Canvas object using the auxiliary bitmap we had created before.
Now its time to create our view.
We can add Images, for example:
we can set the imageView position in the view using “layout” method:
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
and finally adding it to our layout
or we can add text
adding it to the layout in the same way:
Once we have added all elements we want to our layout, we have to create a paint object
just to define default values of painting.
We use the “drawBitmap” method from the canvas:
and finally we call dispatchDraw in the layout to draw the children views (imageView, textView) in the canvas.
The returnedBitmap is the bitmap that contains the drawing of the views in the canvas, on it, we have the layout and its childrens as a Bitmap, after painting them in the Canvas.
Conclusion
This is really tricky and maybe difficult to understand. It took me time to understand how it worked. I will try to summarize it:
1.We need to create a empty bitmap. This bitmap will be the final bitmap with the views on it.
2.We create the canvas using that bitmap
3.We create a layout and we add as many elements as we want
4.We attach the layout to the canvas.
5.Because we have created the canvas using a bitmap object, all that is drawn in the canvas, will be drawn in the bitmap.
How-to Develop Applications on Android: Drawing
In this serie, we will learn how to use Canvas, Bitmaps and other images resources in our Applicacions.
1. How-to convert a View to a Bitmap Image in our Android Application
2. Google Programming Environment: How to get a Bitmap from a URL
Read more: http://www.brighthub.com/mobile/google-android/articles/30676.aspx#ixzz0eg77Kd7E
Introduction
In some applications, we would like to create a View with texts, images, and more view-elements and convert it to a Bitmap. Why? Well, you don't know until you need it, but imagine that you are working with OpenGL, you want to add textures to a OpenGL object, and you want this textures view-like. This is just an example, but I'm positive that more functionality will come to your mind while you are developing in Android.
Short introduction to Bitmap and Canvas
First of all, I would like to write about the Bitmap and the Canvas classes. I think its important to know about this before start talking about “converting a view in a Bitmap using a Canvas”
In Android, the very known word “bitmap”, is a class that help us to work with images as a map of bits or an array of bits. This Bitmap class is very useful when we are working with graphics interfaces like Canvas or OpenGL.
Canvas, is other known class in Java, is used to draw things on it. In Canvas we have the control of all the pixels.
Converting View to Bitmap
We need to have some variables initialized,
This is the Canvas we are going to draw in.
Canvas canvas = null
This is the layout we are going to use to create our view.
RelativeLayout relativeView ;
This is the background we are going to set in our view, we get it from a resource file (R.drawable.blackBgrnd). The BitmapFactory.decodeResource method is used to get a resource image and convert it in a Bitmap (we will use the Bitmap class). The object mContext (context) must be passed from the Activity we are working on.
Bitmap viewBgrnd = BitmapFactory.decodeResource(mContext.getResources(),R.drawable.blackBgrnd);
we need another bitmap, to draw it in the canvas. We set the width and the height of this bitmap related with the width and height we have created our layout. Now this Bitmap is empty, but we are going to associate it to the canvas, so every time we draw in the canvas, it will be drawn in this bitmap object.
Bitmap returnedBitmap = Bitmap.createBitmap(relativeView .width, relativeView.height,Bitmap.Config.ARGB_8888);
First of all, we had the canvas = null, now we create a Canvas object using the auxiliary bitmap we had created before.
canvas = new Canvas(auxBitmap);
Now its time to create our view.
We can add Images, for example:
ImageView newImage = new ImageView(mContext); newImage.setImageBitmap(bitmapWithImage)
we can set the imageView position in the view using “layout” method:
newImage.layout(l,t,r,b);
l Left position, relative to parent
t Top position, relative to parent
r Right position, relative to parent
b Bottom position, relative to parent
and finally adding it to our layout
relativeView.addView(newImage);
or we can add text
TextView newText = new TextView(mContext); newText.setText(“This is the text that its going to appear”);
adding it to the layout in the same way:
relativeView.addView(newText);
Once we have added all elements we want to our layout, we have to create a paint object
Paint paint = new Paint();
just to define default values of painting.
We use the “drawBitmap” method from the canvas:
canvas.drawBitmap(ViewBgrnd, 0, 0, paint);
and finally we call dispatchDraw in the layout to draw the children views (imageView, textView) in the canvas.
relativeView.dispatchDraw(canvas);
The returnedBitmap is the bitmap that contains the drawing of the views in the canvas, on it, we have the layout and its childrens as a Bitmap, after painting them in the Canvas.
Conclusion
This is really tricky and maybe difficult to understand. It took me time to understand how it worked. I will try to summarize it:
1.We need to create a empty bitmap. This bitmap will be the final bitmap with the views on it.
2.We create the canvas using that bitmap
3.We create a layout and we add as many elements as we want
4.We attach the layout to the canvas.
5.Because we have created the canvas using a bitmap object, all that is drawn in the canvas, will be drawn in the bitmap.
How-to Develop Applications on Android: Drawing
In this serie, we will learn how to use Canvas, Bitmaps and other images resources in our Applicacions.
1. How-to convert a View to a Bitmap Image in our Android Application
2. Google Programming Environment: How to get a Bitmap from a URL
Read more: http://www.brighthub.com/mobile/google-android/articles/30676.aspx#ixzz0eg77Kd7E
发表评论
-
AOSP source code build error: Virtual memory exhausted: Cannot allocate memory
2014-01-02 15:47 1383Sometimes compiling certain thi ... -
What is the purpose of different Android partitions
2014-01-02 09:57 744-- Boot partition stores the An ... -
Android Kitkat ART vs. Dalvik & Impacts for end-users
2013-12-08 19:00 834What's ART? ART is Google's 2- ... -
error: gnutls_handshake() falied when you sync chip code in ubuntu
2013-11-30 19:47 927gnutls package is broken, worka ... -
unix2dos dos2unix
2013-03-04 20:12 798sudo aptitude install tofrodos ... -
How to make resources added in frameworks/base/core/res/res
2013-02-23 10:19 10881) add the new id to your xml 2 ... -
JDK6 installed in Ubuntu
2012-11-10 15:23 856按照网上的方法apt-get并不成功,我这里采用的方法是手工安 ... -
Android source sync问题汇总(since 2012)
2012-11-04 16:00 18381. [repo init] fetch address is ... -
Android Partitions Explained: boot, system, recovery, data, cache & misc
2012-09-06 16:17 1108Unless you have been using your ... -
Android IPC AudioFlinger binder实例
2012-06-20 13:32 1020一篇 android 的 IPC 机制 binder ... -
Eclipse Android project name有错误, source tree无红叉解决办法
2012-06-07 13:22 1318linux: Window -> Preference ... -
Android内核开发的几个常用命令
2012-02-23 15:29 1020在android源码的根目录下执行: . build/env ... -
MTP (Media Transfer Protocol) Introduction
2012-02-07 14:46 2321微軟制訂了一套名 ... -
Why is Android laggy, while iOS, Windows Phone 7, QNX, and WebOS are fluid?
2011-12-12 17:55 1042The Root Cause It’s not GC p ... -
Writing Native Code for Android Systems
2011-09-26 17:53 809Writing Native Code for Android ... -
Android JNI 使用的数据结构JNINativeMethod详解
2011-09-13 10:26 896Andoird 中使用了一种不同传统Java JNI的方 ... -
Android property system
2011-08-25 15:11 1342属性系统是 android 的一个重 ... -
Android boot process stub
2011-07-20 10:08 0Android's boot up process is su ... -
Android boot process stub
2011-07-20 10:07 964Android's boot up process is su ... -
OpenFrameworks + kinect + Android
2011-06-20 15:29 1448How to make: 1.Setup ofxAndr ...
相关推荐
在Android开发中,有时我们需要对应用的某个特定View进行截图并保存或分享,例如用户希望分享当前活动的状态或者游戏得分。这个过程涉及到的关键知识点包括View的层级结构、Bitmap的生成与处理以及图片的保存和合成...
在Android开发中,有时我们需要将View的显示内容截图并保存为Bitmap,以便进行分享或者其他图形处理操作。这个过程涉及到Android的视图系统、图形处理以及文件存储等多个知识点。以下将详细讲解如何实现这一功能。 ...
在Android开发中,将View转换为Bitmap是一种常见的需求,尤其在实现屏幕截图、保存或分享View内容、创建自定义控件或动态生成图片等场景下。以下是对如何将Android View转换为Bitmap的深入解析,包括代码逻辑分析、...
Android开发者学习笔记——View、Canvas、bitmap Android 开发者学习笔记——View、Canvas、bitmap 是 Android 开发中常用的类,本文将通过实例讲解 View、Canvas 等相关知识点。 从资源中获取位图 在 Android ...
在Android开发中,Bitmap是用于表示图像数据的基本对象,它是一种内存中的图片表示形式。而当我们需要在应用程序中展示带有圆角的图片时,通常会用到Bitmap的处理技巧。本篇文章将深入探讨如何在Android中对Bitmap...
一个比较好用的Bitmap Image Reader Writer Library 。该库只有一个hpp文件,跟CImg有几分相似(但功能没有CImg丰富,可满足日常开发的基本需求足够)。该库不仅可以很方面的进行bmp文件的读写操作,而且还可以进行...
在Android平台上,Bitmap是用于表示图像数据的基本类,它提供了对像素颜色的直接访问。24位深度的Bitmap文件,通常指的是每个像素包含红、绿、蓝三个通道,每个通道用8位表示,总共24位。这种格式的Bitmap色彩丰富,...
在Android应用开发中,将View或Drawable转换为Bitmap是一项常见的需求。这主要涉及到视图的渲染和图像处理,常用于截图、自定义视图动画、数据记录等多种场景。以下是关于如何进行这种转换以及解决相关问题的详细...
在Android开发中,Bitmap是处理图像的基本类,用于在内存中表示位图图像。当我们需要对图片进行裁剪、缩放或进行其他操作时,Bitmap提供了丰富的功能。本篇文章将详细探讨如何在Android环境下利用Bitmap来切割图片。...
在Android开发中,Canvas是用于在屏幕上绘制图形和图像的核心组件。`drawBitmap()`方法是Canvas的一个关键函数,用于在Canvas上绘制Bitmap图像。本文将深入解析`drawBitmap()`方法的参数及其用法,并通过实例来说明...
Bitmap是Android平台中用于处理图像的核心类,它用于表示位图图像数据。下面是对Bitmap用法的详细总结: 1. **Drawable转换为Bitmap**: 当我们需要将一个Drawable对象(如从XML布局文件中加载的图像)转换为...
android图片处理(压缩,保存,截屏,view转化为bitmap)相关函数 ImageUtils
Android-BitmapCache ... The sample app shows you how to use the library by creating a ViewPager of images downloaded from the web. These are cached in the LruCache and/or Disk Cache. Summary A cache
在Android开发中,处理图像数据是一项常见的任务,而Bitmap和String是两个核心的数据类型,分别代表位图图像和文本字符串。Bitmap对象用于存储和显示图像,而String则常用于保存和传输文本信息。本篇文章将深入探讨...
将pdf文件转换成图片并显示在界面, 先要引用AndroidPDFViewerLibrary-master ... Bitmap image = page.getImage((int)(page.getWidth() * scale), (int)(page.getHeight() * scale), null, true, true);
ImageView imageView = (ImageView) findViewById(R.id.image_view); imageView.setImageBitmap(bmp); ``` 这里的 bmp 是一个 Bitmap 对象,setImageBitmap 方法将其显示在 ImageView 中。 Bitmap 的 compress ...
Bitmap是Android系统中用于处理图像的核心类,它在Android应用开发中扮演着至关重要的角色。在深入探讨Bitmap之前,我们先来理解一下它的基本概念。Bitmap代表的是位图,即像素数组,它存储了图像的颜色信息。在...
在Winform中,我们通常使用`System.Drawing.Bitmap`类来处理图像,而在WPF中,图像数据则被表示为`System.Windows.Media.ImageSource`。当你需要在WPF应用中显示一个在Winform中创建或处理过的`Bitmap`对象时,就...
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!