'안드로이드 FingerPaint 배경'에 해당되는 글 1건

  1. 2012.01.26 안드로이드 FingerPaint(그림판) 예제 따라하기 (1)
JAVA/Android2012.01.26 20:04
- 안드로이드로 터치를 이용한 그림을 그릴 수 있는 그림판을 만들어보고 그림판 배경 이미지도 바꾸어 보자.
- 이 예제를 통해서 그림판을 만들 수 있게 된다.

-Java 소스
public class FingerPaint extends Activity {
private Paint mPaint;
private Path mPath;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);

}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setContentView(new MyView(this));
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(8);

}

public class drawPoint {
private float x;
private float y;
private boolean draw;

public drawPoint(float x, float y, boolean d) {
this.x = x;
this.y = y;
draw = d;
}

public boolean getDraw() {
return draw;
}

public void setDraw(boolean _draw) {
draw = _draw;
}

public float getX() {
return x;
}

public float getY() {
return y;
}
}

public class MyView extends View {

private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;

private Bitmap mBitmap;
Bitmap bm;
private Canvas mCanvas;
// private Path mPath;
private Paint mBitmapPaint;

public MyView(Context context) {
super(context);

bm = BitmapFactory.decodeResource(getResources(), // 배경 비트맵
R.drawable.ic_launcher);

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

mBitmap = Bitmap.createBitmap(metrics.widthPixels,
metrics.heightPixels, Bitmap.Config.ARGB_8888);

mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
// Matrix matrix;
// mCanvas.setBitmap(bm);
// mCanvas.drawBitmap(bm, matrix, 0xFFFFFFFF);
mCanvas.drawColor(0xFFFFFFFF); // backgroundcolor
}

protected void onDraw(Canvas canvas) {
// canvas.drawColor(0x00000000);

canvas.drawBitmap(bm, 0, 0, mBitmapPaint); // 배경바꾸기
canvas.drawPath(mPath, mPaint);
}

/**  
         */
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;
}
}

/**  
         */
private void touch_up() {
mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
// mPath.reset();
}

public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
drawPoint p = new drawPoint(x, y, false);
switch (action) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
break;
case MotionEvent.ACTION_UP:
touch_up();
break;
}
invalidate();
return true;
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuItem item = menu.add(0, 1, 0, "다시쓰기");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);

switch (item.getItemId()) {
case 1:
mPath.reset();
onResume();
}

return true;
}
}
 
- 실행 화면 

 
신고
Posted by Namseungil

티스토리 툴바