在画板中定义一个方法public Bitmap getbit(){
return mybitmap;
}
然后在你的activity中取得你的组件getbit();
把读出来的bitmap传到服务器那就你自己写了,直接发二进制流过去就ok了。
加载html页面时,会在/data/data/应用package目录下生成database与cache两个文件夹。
cache里面会保存webview中的文件,读的话需要bitmap解析流就可以了。
解释:
1、首先创建一个Bitmap,并指定大小;
2、在该上创建一个新的画布Canvas,然后在画布上绘制,并保存即可;
3、需要保存的目录File,注意如果写的目录如“/sdcard/akai/”如果不存在的话,要先创建(filemkdirs()),否则FileOutputStream会报错No found;
4、需要添加权限:<uses-permission android:name="androidpermissionWRITE_EXTERNAL_STORAGE"/>
什么是画布呢 其实通过字面意思的理解就是用来绘画的地方,那么android里的画布是神马样子的呢?
在自定义画布中常用到下面3个类
Canvas
这些绘图方法中的每一个都需要指定一个Paint对象来渲染它
Paint
Paint也称为"刷子",Paint可以指定如何将基本图形绘制到位图上。
Paint类相当于一个笔刷和调色板。它可以选择如何使用上面描述的draw方法来渲染绘 制在画布上的基本图形。通过修改Paint对象,可以在绘图的时候控制颜色、样式、字体和特殊效果。最简单地,setColor可以让你选择一个Paint的颜色,而Paint对象的样式(使用setStyle控制)则可以决定是绘制绘图对象的轮廓(STROKE),还是只填充每一部 分(FILL),或者是两者都做(STROKE_AND_FILL)除了这些简单的控制之外,Paint类还支持透明度,另外,它也可以通过使用各种各样的阴影、过滤器和效果进行修改,从而提供由更丰富的、复杂的画笔和颜料组成的调色板。
从继承View类(或其子类)开始,并定义onDraw()回调方法。系统会调用该方法来完 成View对象自己的绘制请求。这也是通过Canvas对象来执行所有的图形绘制调用的地方,这个Canvas对象是由onDraw()回调方法传入的。
Android框架只在必要的时候才会调用onDraw()方法,每次请求应用程序准备完成图形 绘制任务时,必须通过调用invalidate()方法让该View对象失效。这表明可以在该View 对象上进行图形绘制处理了,然后Android系统会调用该View对象的onDraw()方(尽 管不保证该回调方法会立即被调用)。
在定制的View组件的onDraw()方法内部,使用给定的Canvas对象来完成所有的图形绘制处理(如Canvasdraw…()方法或把该Canvas对象作为参数传递给其他类的draw() 方法)。一旦onDraw()方法被执行完成,Android框架就会使用这个Canvas对象来绘制一个有系统处理的Bitmap对象。
下面是Paint一些常用方法:
Bitmap
Bitmap绘图的表面也称位图(这里详细说哈位图的功能)。
从资源中获取位图:
通过Resource的函数:InputStream openRawResource(int id)获取得到资源文件的数据流后,可以通过2种方式获得bitmap
使用BitmapDrawable :
使用BitmapDrawable(InputStream is)构造一个BitmapDrawable;
使用BitmapDrawable类的getBitmap()获取得到位图;
使用BitmapFactory使用BitmapFactory类decodeStream(InputStream is)解码位 图资源,获取位图BitmapFactory的所有函数都是static,这个辅助类可以通过资 源ID、路径、文件、数据流等方式来获取位图。
获取位图的信息
一般获取位图信息包括:位图大小、透明度、颜色格式等等,这些信息呢可以通过 三-一方法获取得到Bitmap就迎刃而解了,Android SDK中对Bitmap有详细说明,大家可以去详细了解哈。
显示位图
显示位图需要使用核心类Canvas,可以直接通过Canvas类的drawBirmap()显示位图,或者借助于BitmapDrawable来将Bitmap绘制到Canvas,下面的实例中会详细列举到
位图的缩放
位图的缩放,在Android SDK中提供了2种方法:
1:将一个位图按照需求重画一遍,画后的位图就是我们需要的了,与位图的显示几乎 一样:
drawBitmap(Bitmap bitmap, Rect src, Rectdst, Paint paint)
2:在原有位图的基础上,缩放原位图,创建一个新的位图:
createBitmap(Bitmap source, int x, int y,int width, int height, Matrix m, boolean filter)
位图旋转
位图的旋转,离不开Matrix。Android SDK提供了Matrix类,可以通过各种接口来设置 矩阵
android 处理工具
截取视频帧并转化为Bitmap
Author:FounderWatts
context:调用方上下文,Activitythis
urlStr:指定的网址,如:“>
一、相关概念
1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
二、Bitmap
1、从资源中获取Bitmap
Java代码
Resources res = getResources();
Bitmap bmp = BitmapFactorydecodeResource(res, Rdrawableicon);
2、Bitmap → byte[]
Java代码
public byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmcompress(BitmapCompressFormatPNG, 100, baos);
return baostoByteArray();
}
3、byte[] → Bitmap
Java代码
public Bitmap Bytes2Bimap(byte[] b) {
if (blength != 0) {
return BitmapFactorydecodeByteArray(b, 0, blength);
} else {
return null;
}
}
4、Bitmap缩放
Java代码
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmapgetWidth();
int h = bitmapgetHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrixpostScale(scaleWidth, scaleHeight);
Bitmap newbmp = BitmapcreateBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
5、将Drawable转化为Bitmap
Java代码
public static Bitmap drawableToBitmap(Drawable drawable) {
// 取 drawable 的长宽
int w = drawablegetIntrinsicWidth();
int h = drawablegetIntrinsicHeight();
// 取 drawable 的颜色格式
BitmapConfig config = drawablegetOpacity() != PixelFormatOPAQUE BitmapConfigARGB_8888
: BitmapConfigRGB_565;
// 建立对应 bitmap
Bitmap bitmap = BitmapcreateBitmap(w, h, config);
// 建立对应 bitmap 的画布
Canvas canvas = new Canvas(bitmap);
drawablesetBounds(0, 0, w, h);
// 把 drawable 内容画到画布中
drawabledraw(canvas);
return bitmap;
}
6、获得圆角
Java代码
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
int w = bitmapgetWidth();
int h = bitmapgetHeight();
Bitmap output = BitmapcreateBitmap(w, h, ConfigARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
paintsetAntiAlias(true);
canvasdrawARGB(0, 0, 0, 0);
paintsetColor(color);
canvasdrawRoundRect(rectF, roundPx, roundPx, paint);
paintsetXfermode(new PorterDuffXfermode(ModeSRC_IN));
canvasdrawBitmap(bitmap, rect, rect, paint);
return output;
}
7、获得带倒影的
Java代码
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int w = bitmapgetWidth();
int h = bitmapgetHeight();
Matrix matrix = new Matrix();
matrixpreScale(1, -1);
Bitmap reflectionImage = BitmapcreateBitmap(bitmap, 0, h / 2, w,
h / 2, matrix, false);
Bitmap bitmapWithReflection = BitmapcreateBitmap(w, (h + h / 2),
ConfigARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvasdrawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvasdrawRect(0, h, w, h + reflectionGap, deafalutPaint);
canvasdrawBitmap(reflectionImage, 0, h + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmapgetHeight(), 0,
bitmapWithReflectiongetHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileModeCLAMP);
paintsetShader(shader);
// Set the Transfer mode to be porter duff and destination in
paintsetXfermode(new PorterDuffXfermode(ModeDST_IN));
// Draw a rectangle using the paint with our linear gradient
canvasdrawRect(0, h, w, bitmapWithReflectiongetHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
三、Drawable
1、Bitmap转换成Drawable
Java代码
Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd= new BitmapDrawable(getResource(), bm);
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
2、Drawable缩放
Java代码
public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
int width = drawablegetIntrinsicWidth();
int height = drawablegetIntrinsicHeight();
// drawable转换成bitmap
Bitmap oldbmp = drawableToBitmap(drawable);
// 创建 *** 作用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrixpostScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = BitmapcreateBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
在 Java中,JavaVM拥有自动管理内存的功能,Java的GC能够进行垃圾回收,但是如果ImageView使用过多的Bitmap的话,经常会报OOM(内存溢出)。
造成内存溢出及解决方案:
使用BitmapFactorydecodeStream替代createBitmap方法
原因是该方法直读取字节,调用JNI>>nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap。
使用压缩读取技术
BitmapFactoryOptions options = new BitmapFactoryOptions();
optionsinJustDecodeBounds = true;
BitmapFactorydecodeFile(imageSdUri, options);
final int height = optionsoutHeight;
final int width = optionsoutWidth;
optionsinSampleSize = 1;
int w = 320;
int h = 480;
h = wheight/width;//计算出宽高等比率
int a = optionsoutWidth/ w;
int b = optionsoutHeight / h;
optionsinSampleSize = Mathmax(a, b);
optionsinJustDecodeBounds = false;
Bitmap bitmap = BitmapFactorydecodeFile(imageSdUri, options);
3及时释放Bitamp
Bitmap对象在不使用时,我们应该先调用recycle()释放内存,然后才它设置为null虽然recycle()从源码上看,调用它应该能立即释放Bitmap的主要内存,但是测试结果显示它并没能立即释放内存。但是我它应该还是能大大的加速Bitmap的主要内存的释放。
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
返回一个不可改变的位图,该位图来自源位图的子集,并根据可选的矩阵进行转换。它被初始化为跟源位图有同样的密度。
参数
source 产生子位图的源位图;
x 子位图第一个像素在源位图的X坐标
y 子位图第一个像素在源位图的y坐标
width 子位图每一行的像素个数
height 子位图的行数
m 对像素值进行变换的可选矩阵
filter 如果为true,源图要被过滤。该参数仅在matrix包含了超过一个翻转才有效
返回值
一个描述了源图指定子集的位图。
异常
IllegalArgumentException 如果x,y,width,height的值超出了源图的维度,该异常会被抛出。
以上就是关于android画板上bitmap的图怎么提取出来传到服务器全部的内容,包括:android画板上bitmap的图怎么提取出来传到服务器、android 怎么获取webview中的图片资源bitmap、android图像绘制——画布保存为图片等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)