问题描述
我不得不放置一个画布的内容到一个位图一些困难与问候。当我尝试这样做时,文件被写入具有约5.80KB一个文件大小,但它似乎是完全空(每一个像素是#000)。
画布绘制一系列的由手写形成的相互连接的线。下面是我为View的onDraw。 (我知道,它的阻止UI线程/陋习/等,但是我只需要得到它的工作)
感谢您。
@覆盖
保护无效的onDraw(帆布油画){
// TODO自动生成方法存根
super.onDraw(画布); 如果(IsTouchDown){ //计算点
路径currentPath =新路径();
布尔IsFirst =真;
对于(点对点:currentPoints){
如果(IsFirst){
IsFirst = FALSE;
currentPath.moveTo(point.x,point.y);
}其他{
currentPath.lineTo(point.x,point.y);
}
} //绘制的点的路径
canvas.drawPath(currentPath,笔); //企图使该位图,并将其写入文件。
位图toDisk = NULL;
尝试{ // TODO:获取画布的大小,更换640,480
toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);
toDisk.com preSS(Bitmap.Com pressFormat.JPEG,100,新的FileOutputStream(新文件(arun.jpg))); }赶上(例外前){
} }其他{ //清除点
currentPoints.clear(); }
}
我有类似的问题,我已经得到了解决。在这里,一个任务的全面code /不要忘记 android.permission.WRITE_EXTERNAL_STORAGE
在清单许可/
公共位图saveSignature(){ 位图的位图= Bitmap.createBitmap(this.getWidth(),this.getHeight(),Bitmap.Config.ARGB_8888);
帆布帆布=新的Canvas(位图);
this.draw(画布); 档案文件=新的文件(Environment.getExternalStorageDirectory()+/sign.png); 尝试{
bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,新的FileOutputStream(文件));
}赶上(例外五){
e.printStackTrace();
} 返回位图;
}
I'm having some difficulty with regards to placing the contents of a Canvas into a Bitmap. When I attempt to do this, the file gets written with a file size of around 5.80KB but it appears to be completely empty (every pixel is '#000').
The canvas draws a series of interconnected lines that are formed by handwriting. Below is my onDraw for the View. (I'm aware that it's blocking the UI thread / bad practices/ etc.., however I just need to get it working)
Thank you.
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (IsTouchDown) {
// Calculate the points
Path currentPath = new Path();
boolean IsFirst = true;
for(Point point : currentPoints){
if(IsFirst){
IsFirst = false;
currentPath.moveTo(point.x, point.y);
} else {
currentPath.lineTo(point.x, point.y);
}
}
// Draw the path of points
canvas.drawPath(currentPath, pen);
// Attempt to make the bitmap and write it to a file.
Bitmap toDisk = null;
try {
// TODO: Get the size of the canvas, replace the 640, 480
toDisk = Bitmap.createBitmap(640,480,Bitmap.Config.ARGB_8888);
canvas.setBitmap(toDisk);
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("arun.jpg")));
} catch (Exception ex) {
}
} else {
// Clear the points
currentPoints.clear();
}
}
I had similar problem and i've got solution. Here full code of a task /don't forget about android.permission.WRITE_EXTERNAL_STORAGE
permission in manifest/
public Bitmap saveSignature(){
Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
this.draw(canvas);
File file = new File(Environment.getExternalStorageDirectory() + "/sign.png");
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
这篇关于保存帆布为位图在Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!