我正在Android中創建一個應用程序,用戶在畫布上畫一些東西,並保存它的用戶目錄,即SD卡。如何在Android中將畫布轉換爲位圖?
但我的問題是,我保存的位圖總是顯示一個黑色的圖像,我的意思是圖像從不保存在畫布上繪製只有黑色圖像保存。
這裏是我的代碼
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());
方法則拋出一個計算器例外。
請朋友們幫助我,因爲我從一週的這種情況中生氣。
男人你是誰明白我的問題只有一個。 – 2014-12-08 13:43:02