마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 그림판 질문입니다

0 추천

그림판 만들고 있습니다. 어디서 퍼와서 기능 추가하고있지만
색깔 바꾸는게 잘 안되네요..
버튼 클릭했을때 mPaint.setColor(Color.RED) 로 색깔을 바꿀려고했는데
기존에 그렸던거 까지 전부 바껴 버리네요..
어떻게 하면좋을까요?? ㅠㅠ


 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  mCtx = this;
  
  setContentView(R.layout.crash_create_map);

                .........
                .........

  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mPaint.setDither(true);
  //mPaint.setColor(0xFFFFFF00);
  mPaint.setColor(Color.BLACK);
  mPaint.setStyle(Paint.Style.STROKE);
  mPaint.setStrokeJoin(Paint.Join.ROUND);
  mPaint.setStrokeCap(Paint.Cap.ROUND);
  mPaint.setStrokeWidth(8);


}


 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.device4_hit_17);
   
   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
   mCanvas.drawColor(Color.WHITE);

  }

  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;

  }
 }



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

 @Override
 public void onClick(View v) {

  ...........
  mPaint.setColor(Color.RED);     
  .............  
 }

 

익명사용자 님이 2014년 10월 18일 질문

1개의 답변

+1 추천
canvas.drawPath(mPath, mPaint); 보면 mPaint의 색상 하나로 전부 바뀌게 되어있습니다.

mPath도 배열로 잡고 mPaint도 배열로 잡아서 각  path마다 다른 색상으로 그려지게 만들어야합니다.
q1212 (26,020 포인트) 님이 2014년 10월 20일 답변
...