如何在Android中将画布转换为位图?

时间:2014-12-06 09:04:20

标签: android bitmap android-canvas

我正在android i中创建一个应用程序,用户在画布上绘制一些内容并将其保存为用户目录,即sdcard。

但我的问题是我保存的位图始终显示黑色图像,我的意思是图像永远不会像在画布上绘制那样只保存黑色图像。

这是我的代码

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class DrawWrite extends View {
    float TouchXD = 0, TouchYD = 0, TouchXU = 0, TouchYU = 0, TouchXM = 0,
            TouchYM = 0; // Define touch co-ordinates
    float x1 = 0, y1 = 0, x2 = 0, y2 = 0; // Define drawing path co-ordinates
    float stroke = 2; // Define the message structure width
    int i=0;
boolean Move = false, moveD = false, moveU = false; // Define whether the
                                                    // touch has occurred or
                                                    // not
boolean exp = false;
Paint paint = new Paint(); // Paint object
Path mPath = new Path(); // Define the drawing message path
Context context;

public DrawWrite(Context context) {
    super(context);
    this.context=context;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    invalidate();
    paint.setAntiAlias(true);
    if (DrawWriteActivity.clearScreen){
        mPath.reset();
        DrawWriteActivity.clearScreen = false;
    }
    if(DrawWriteActivity.saveImage){
        try {
            getDrawnMessage(canvas);
            DrawWriteActivity.saveImage = false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    paint.setColor(Color.parseColor(DrawWriteActivity.colorProvider));
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(DrawWriteActivity.strokeProvider);
    if (moveD == true) {
        x1 = TouchXD;
        y1 = TouchYD;
        moveD = false; 
    } else if (Move == true) {
        x2 = TouchXD;
        y2 = TouchYD;
        mPath.moveTo(x1, y1);
        mPath.lineTo(x2, y2);
        canvas.drawPath(mPath, paint);
        x1 = x2;
        y1 = y2;
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        TouchXD = (float) event.getX();
        TouchYD = (float) event.getY();
        moveD = true;
        break;
    case MotionEvent.ACTION_UP:
        TouchXU = (float) event.getX();
        TouchYU = (float) event.getY();
        moveU = true;
        break;
    case MotionEvent.ACTION_MOVE:
        TouchXD = (float) event.getX();
        TouchYD = (float) event.getY();
        Move = true;
        break;
    }
    return true;
}

public void getDrawnMessage(Canvas canvas) throws FileNotFoundException{
    Bitmap bitmap;
    setDrawingCacheEnabled(true);
    Toast.makeText(getContext(), "Toasting", Toast.LENGTH_SHORT).show();
    String root = Environment.getExternalStorageDirectory().toString();
    File imgDir = new File(root+"/ChitBak/");
    String imgName;
    bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
    imgDir.mkdirs();
    imgName = "img"+i+".jpg";
    i++;
    File file = new File(imgDir,imgName);
    if(file.exists()) file.delete();
    FileOutputStream outImg = new FileOutputStream(file);
    bitmap.compress(CompressFormat.JPEG, 100, outImg);
    setDrawingCacheEnabled(false);
}
}

我还想澄清一下,当我使用

bitmap = Bitmap.createBitmap(getDrawingCache());

方法然后抛出stackoverflow异常。

请朋友帮助我,因为我被这种情况从一周后感到生气。

2 个答案:

答案 0 :(得分:2)

您的位图是黑色的,因为您没有在其上绘制任何内容。

要在位图中绘制一些图形,您必须使用此调用创建它:

bitmap = Bitmap.createBitmap(getDrawingCache());

然后你有一个stackoverflow异常只是因为:

  • getDrawingCache()致电onDraw(Canvas)
  • onDraw(Canvas)致电getDrawnMessage(Canvas canvas)
  • getDrawnMessage(Canvas canvas)致电getDrawingCache()
  • getDrawingCache()致电onDraw(Canvas)
  • ...

所以你真正的问题是StackOverflow异常。

解决方案:请勿在{{1​​}}方法中调用getDrawnMessage(Canvas canvas)

答案 1 :(得分:0)

可能存在逻辑错误: 当你在移动事件中设置移动=真时,你忘记将它重置为假,但是经常会调用onDraw(),然后x1将等于x2,y1到y2,你什么也看不见只有一个点。

建议解决方案:

  1. 改变你的逻辑,最好在阵列中记录触控轨迹,记得在使用后清除它
  2. 作为第一个答案,不在onDraw()中生成位图,更好地在另一个事件处理程序中点击按钮
  3. 祝你好运