안드로이드 개발 질문/답변
(글 수 45,052)
import java.io.OutputStream; import java.util.Stack;
import jin.oh.academy.R; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView;
public class BestPaintBoard extends View { ImageView imageview1; Bitmap bm = null; String d_Qfile = "NODATA"; /** * Undo data */ Stack undos = new Stack();
/** * Maximum Undos */ public static int maxUndos = 10;
/** * Changed flag */ public boolean changed = false; /** * Canvas instance */ Canvas mCanvas; ImageView imageview; /** * Bitmap for double buffering */ Bitmap mBitmap; /** * Paint instance */ final Paint mPaint; /** * X coordinate */ float lastX; /** * Y coordinate */ float lastY; //최대치 public int FILL_PARENT = getWidth()|getHeight(); private final Path mPath = new Path();
private float mCurveEndX; private float mCurveEndY;
private int mInvalidateExtraBorder = 10; static final float TOUCH_TOLERANCE = 8;
private static final boolean RENDERING_ANTIALIAS = true; private static final boolean DITHER_FLAG = true;
private int mCertainColor = 0xFF000000; private float mStrokeWidth = 2.0f; private String imagFilePath; /** * Initialize paint object and coordinates * * @param c */ public BestPaintBoard(Context context, String filePath) { super(context); // create a new paint object mPaint = new Paint(); mPaint.setAntiAlias(RENDERING_ANTIALIAS); mPaint.setColor(mCertainColor); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(mStrokeWidth); mPaint.setDither(DITHER_FLAG);
lastX = -1; lastY = -1; imagFilePath = filePath; Log.i("GoodPaintBoard", "initialized."); }
/** * Clear undo */ public void clearUndo() { while(true) { Bitmap prev = (Bitmap)undos.pop(); if (prev == null) return; prev.recycle(); } } /** * Save undo */ public void saveUndo() { if (mBitmap == null) return; while (undos.size() >= maxUndos){ Bitmap i = (Bitmap)undos.get(undos.size()-1); i.recycle(); undos.remove(i); } Bitmap img = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(img); canvas.drawBitmap(mBitmap, 0, 0, mPaint); undos.push(img); Log.i("GoodPaintBoard", "saveUndo() called."); } /** * Undo */ public void undo() { Bitmap prev = null; try { prev = (Bitmap)undos.pop(); } catch(Exception ex) { Log.e("GoodPaintBoard", "Exception : " + ex.getMessage()); } if (prev != null){ drawBackground(mCanvas); mCanvas.drawBitmap(prev, 0, 0, mPaint); invalidate(); prev.recycle(); } Log.i("GoodPaintBoard", "undo() called."); } /** * Paint background * * @param g * @param w * @param h */ Drawable drawable = getResources().getDrawable(R.drawable.book); public void drawBackground(Canvas canvas) { //Intent data = (Intent) getParent(); //d_Qfile = data.getStringExtra(MakeActivity.FILE_PATH); // d_Qfile = imagFilePath; // Log.d("AA", d_Qfile); // if(d_Qfile.equals("NODATA") || d_Qfile.equals("")){ // imageview1.setBackgroundResource(R.drawable.notemakepica); // }else{ // imageview1.setImageBitmap(makeBitmap(imagFilePath)); // // } // drawable.setBounds(0,0, getWidth(), getHeight()); if (canvas != null) { // canvas.drawColor(Color.WHITE); drawable.draw(canvas); Log.e("",""+getWidth()+","+getHeight()); } } // private Bitmap makeBitmap(String url) { // // BitmapFactory.Options opt = new BitmapFactory.Options(); // opt.inSampleSize = 4; // bm = BitmapFactory.decodeFile(url, opt); // // return bm; //} /** * Update paint properties * * @param canvas */ public void updatePaintProperty(int color, int size) { mPaint.setColor(color); mPaint.setStrokeWidth(size); } /** * Create a new image */ public void newImage(int width, int height) { Bitmap img = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.setBitmap(img); mBitmap = img; mCanvas = canvas;
drawBackground(mCanvas); changed = false; invalidate(); } /** * Set image * * @param newImage */ public void setImage(Bitmap newImage) { changed = false; setImageSize(newImage.getWidth(),newImage.getHeight(),newImage); invalidate(); } /** * Set image size * * @param width * @param height * @param newImage */ public void setImageSize(int width, int height, Bitmap newImage) { if (mBitmap != null){ if (width < mBitmap.getWidth()) width = mBitmap.getWidth(); if (height < mBitmap.getHeight()) height = mBitmap.getHeight(); } if (width < 1 || height < 1) return; Bitmap img = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); drawBackground(canvas); if (newImage != null) { canvas.setBitmap(newImage); } if (mBitmap != null) { mBitmap.recycle(); mCanvas.restore(); }
mBitmap = img; mCanvas = canvas; clearUndo(); } /** * onSizeChanged */ protected void onSizeChanged(int w, int h, int oldw, int oldh) { if (w > 0 && h > 0) { newImage(w, h); } }
/** * Draw the bitmap */ protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mBitmap != null) { canvas.drawBitmap(mBitmap, 0, 0, null); }
}
/** * Handles touch event, UP, DOWN and MOVE */ public boolean onTouchEvent(MotionEvent event) { int action = event.getAction();
switch (action) { case MotionEvent.ACTION_UP: changed = true; Rect rect = touchUp(event, false); if (rect != null) { invalidate(rect); }
mPath.rewind(); return true; case MotionEvent.ACTION_DOWN: saveUndo(); rect = touchDown(event); if (rect != null) { invalidate(rect); } return true; case MotionEvent.ACTION_MOVE: rect = touchMove(event); if (rect != null) { invalidate(rect); }
return true; }
return false; }
/** * Process event for touch down * * @param event * @return */ private Rect touchDown(MotionEvent event) {
float x = event.getX(); float y = event.getY();
lastX = x; lastY = y;
Rect mInvalidRect = new Rect(); mPath.moveTo(x, y);
final int border = mInvalidateExtraBorder; mInvalidRect.set((int) x - border, (int) y - border, (int) x + border, (int) y + border);
mCurveEndX = x; mCurveEndY = y;
mCanvas.drawPath(mPath, mPaint); return mInvalidRect; } /** * Process event for touch move * * @param event * @return */ private Rect touchMove(MotionEvent event) { Rect rect = processMove(event); return rect; }
private Rect touchUp(MotionEvent event, boolean cancel) { Rect rect = processMove(event);
return rect; }
/** * Process Move Coordinates * * @param x * @param y * @param dx * @param dy * @return */ private Rect processMove(MotionEvent event) {
final float x = event.getX(); final float y = event.getY();
final float dx = Math.abs(x - lastX); final float dy = Math.abs(y - lastY);
Rect mInvalidRect = new Rect(); if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { final int border = mInvalidateExtraBorder; mInvalidRect.set((int) mCurveEndX - border, (int) mCurveEndY - border, (int) mCurveEndX + border, (int) mCurveEndY + border);
float cX = mCurveEndX = (x + lastX) / 2; float cY = mCurveEndY = (y + lastY) / 2;
mPath.quadTo(lastX, lastY, cX, cY);
// union with the control point of the new curve mInvalidRect.union((int) lastX - border, (int) lastY - border, (int) lastX + border, (int) lastY + border);
// union with the end point of the new curve mInvalidRect.union((int) cX - border, (int) cY - border, (int) cX + border, (int) cY + border);
lastX = x; lastY = y;
mCanvas.drawPath(mPath, mPaint); }
return mInvalidRect; } /** * Save this contents into a Jpeg image * * @param outstream * @return */ public boolean Save(OutputStream outstream) { try { mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream); invalidate(); return true; } catch (Exception e) { return false; } } }
소스코드입니다..
Drawable drawable = getResources().getDrawable(R.drawable.book); public void drawBackground(Canvas canvas) { //Intent data = (Intent) getParent(); //d_Qfile = data.getStringExtra(MakeActivity.FILE_PATH); // d_Qfile = imagFilePath; // Log.d("AA", d_Qfile); // if(d_Qfile.equals("NODATA") || d_Qfile.equals("")){ // imageview1.setBackgroundResource(R.drawable.notemakepica); // }else{ // imageview1.setImageBitmap(makeBitmap(imagFilePath)); // // } // drawable.setBounds(0,0, getWidth(), getHeight()); if (canvas != null) { // canvas.drawColor(Color.WHITE); drawable.draw(canvas); Log.e("",""+getWidth()+","+getHeight()); } } // private Bitmap makeBitmap(String url) { // // BitmapFactory.Options opt = new BitmapFactory.Options(); // opt.inSampleSize = 4; // bm = BitmapFactory.decodeFile(url, opt); // // return bm; //}
이부분에서
imagFilePath 이부분이 카메라를찍고 저장을하면 그 주소값이 들어있는 스트링값입니다.
그리고 지금 보시는 부분이 캔버스에서 백그라운드를 정해주는 부분인대
백그라운드에 저 저장한 주소값이 들어있는 스트링값을 출력하기위해선 무엇을사용해야할지 전혀 감이안잡히네요 ㅠㅠ