我创建了一个用于绘制符号的活动,将其保存到内部存储中,对其进行自动加载并继续从保存的位图开始绘制。
首次启动时,文件未加载,因为它不存在。所以我绘制,当我点击返回previos Activity时,我将Bitmap保存在文件中。从第二次启动文件存在并正确加载(我从android studio中的debug查看图像)。问题是画布显示为空白。我没有例外。有什么想法吗?
public class PhysicalSignatureActivity extends AppCompatActivity {
DrawingView dv ;
private Paint mPaint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Physical sign");
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
if (!(getIntent().getStringExtra("filename") == null)){
try {
InputStream inputStream = getApplicationContext().openFileInput(getIntent().getStringExtra("filename"));
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Bitmap bitmap_mutable = bitmap.copy(Bitmap.Config.ARGB_8888, true);
dv = new DrawingView(this,bitmap_mutable);
} catch (FileNotFoundException ex) {
}
} else {
dv = new DrawingView(this);
}
setContentView(dv);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Save sign?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String filename = UUID.randomUUID().toString()+".png";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
dv.buildDrawingCache();
Bitmap bitmap = dv.getDrawingCache();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
outputStream.close();
} catch (FileNotFoundException ex) {
} catch (IOException e) {
e.printStackTrace();
} finally {
Intent i = new Intent(PhysicalSignatureActivity.this, PerformanceActivity.class);
i.putExtra("performance_id",getIntent().getStringExtra("performance_id"));
i.putExtra("authentication_token",getIntent().getStringExtra("authentication_token"));
i.putExtra("filename",filename);
startActivity(i);
finish();
}
}
});
builder.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alert=builder.create();
alert.show();
}
public class DrawingView extends View {
public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
Context context;
private Paint circlePaint;
private Path circlePath;
public DrawingView(Context c) {
super(c);
context=c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
circlePaint = new Paint();
circlePath = new Path();
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
}
public DrawingView(Context c, Bitmap b) {
super(c);
context=c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
circlePaint = new Paint();
circlePath = new Path();
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);
mBitmap = b;
mCanvas = new Canvas();
mCanvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath( mPath, mPaint);
canvas.drawPath( circlePath, circlePaint);
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
circlePath.reset();
circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
circlePath.reset();
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
}