I have problem with my first Android application, I am trying to do hand-signature. Code that display and draw line. Line is draw fine, but when I try clear canvas I get into error. Second problem is saving canvas image, I would like to just save it to ftp, I dont need full code, just some help how to store image to variable and than how to save it. Thank everyone for help.
Code failure is: "Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference" My activity code is:
public class AndroidCanvasSignature extends Activity { private CanvasView customCanvas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_android_canvas_signature); customCanvas = (CanvasView) findViewById(R.id.signature_canvas); } public void clearCanvas(View v) { customCanvas.clearCanvas(); } public void saveCanvas(View v){ View content = v; content.setDrawingCacheEnabled(true); content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); Bitmap bitmap = content.getDrawingCache(); String path = Environment.getExternalStorageDirectory().getAbsolutePath(); File file = new File("../img/image.png"); FileOutputStream ostream; try { file.createNewFile(); ostream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, ostream); ostream.flush(); ostream.close(); Toast.makeText(getApplicationContext(), "image saved",Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show(); } } }
and my view class: public class CanvasView extends View {
public int width; public int height; private Bitmap mBitmap; private Canvas mCanvas; private Path mPath; Context context; private Paint mPaint; private float mX, mY; private static final float TOLERANCE = 5; public CanvasView(Context c, AttributeSet attrs) { super(c, attrs); context = c; // we set a new Path mPath = new Path(); // and we set a new Paint with the desired attributes mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(Color.BLACK); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeWidth(4f); } // override onSizeChanged @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // your Canvas will draw onto the defined Bitmap mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); mCanvas = new Canvas(mBitmap); } // override onDraw @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // draw the mPath with the mPaint on the canvas when onDraw canvas.drawPath(mPath, mPaint); } // when ACTION_DOWN start touch according to the x,y values private void startTouch(float x, float y) { mPath.moveTo(x, y); mX = x; mY = y; } // when ACTION_MOVE move touch according to the x,y values private void moveTouch(float x, float y) { float dx = Math.abs(x - mX); float dy = Math.abs(y - mY); if (dx >= TOLERANCE || dy >= TOLERANCE) { mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2); mX = x; mY = y; } } public void clearCanvas() { mPath.reset(); invalidate(); } // when ACTION_UP stop touch private void upTouch() { mPath.lineTo(mX, mY); } //override the onTouchEvent @Override public boolean onTouchEvent(MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: startTouch(x, y); invalidate(); break; case MotionEvent.ACTION_MOVE: moveTouch(x, y); invalidate(); break; case MotionEvent.ACTION_UP: upTouch(); invalidate(); break; } return true; } }
No comments:
Post a Comment