2013-07-18 5 views
1

캔버스에 여러 색상으로 그릴 때 초기 색상이 잘 작동하지만 다른 색상으로 전환하면 선이 이전 색상으로 그려 지지만 이후 색상은 올바른 색상으로 그려집니다. . 예를 들어 한 번 녹색에서 검은 색으로 전환하면 다음 줄은 녹색이지만 검정색은 그 다음부터입니다. 어떤 도움을 주셔서 감사합니다. 당신의 touch_start에서여러 색상 캔버스에 그려진

public class DrawingPanel extends View implements OnTouchListener { 
    private static final String TAG = "DrawView"; 
    private static final float MINP = 0.25f; 
    private static final float MAXP = 0.75f; 
    private Canvas mCanvas; 
    private Path mPath; 
    private Paint mPaint; 
    private Paint bmPaint; 
    private Paint gmPaint; 
    private ArrayList<Path> paths = new ArrayList<Path>(); 
    private Map<Path, Integer> colorsMap = new HashMap<Path,Integer>(); 
    private int selectedcolor; 
    SharedPreferences settings= getSharedPreferences("Macaroni",0); 
    private String selColr = settings.getString("DrawColor", ""); 

    public DrawingPanel(Context context) { 
     super(context); 
     setFocusable(true); 
     setFocusableInTouchMode(true); 

     this.setOnTouchListener(this); 

     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(6); 
     mCanvas = new Canvas(); 

     mPath = new Path(); 
     paths.add(mPath); 
     colorsMap.put(mPath,getColorSel()); 

    }    
     @Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
      super.onSizeChanged(w, h, oldw, oldh); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      for (Path p : paths){ 

       int grabColorFromHash =colorsMap.get(p);  
       mPaint.setColor(grabColorFromHash); 
       canvas.drawPath(p, 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; 
      Log.i("touch_start","touch_start"); 
      } 

    private void touch_move(float x, float y) { 
      float dx = Math.abs(x - mX); 
      float dy = Math.abs(y - mY); 
      Log.i("touch_move","touch_move"); 

      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); 
      mPath = new Path(); 
      paths.add(mPath); 
      colorsMap.put(mPath,getColorSel()); 
      mPaint.setColor(getColorSel()); 

     } 
    public int getColorSel() 
    { 

      selColr = settings.getString("DrawColor", ""); 

     if (selColr.equalsIgnoreCase("Black")){ 
      mPaint.setColor(Color.BLACK); 
      selectedcolor = mPaint.getColor(); 
      } 

      if (selColr.equalsIgnoreCase("Red")) 
      { 
      mPaint.setColor(Color.RED); 
      selectedcolor = mPaint.getColor(); 
      } 

      if (selColr.equalsIgnoreCase("Green")) 
      { 
      mPaint.setColor(Color.GREEN); 
      selectedcolor = mPaint.getColor(); 
      } 

     return selectedcolor; 

    } 
    @Override 
    public boolean onTouch(View arg0, 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; 
    } 
} 
+0

colorsMap의 내용을 인쇄 할 수 있습니까? 나는 당신이 중복을 가지고 있다고 생각한다 –

+0

그것이 선택되었을 때 색깔을 저장하는 코드를 보여줄 수 있습니까? 한 가지 문제는 당신이 mPaint.setColor()를 getColorSel() 함수 안에 설정하고 있다고 생각하는 것입니다. 'touch_up()'마지막 줄에 mPaint.setColor()를 호출 했으므로 불필요합니다. – spartygw

+0

색상을 저장하기 위해 내가하고있는 일은 공유 설정에 원했던 색상을 추가하는 것입니다. – cmhdev

답변

1

당신은 경로를 재설정하지만이 모든 당신이 주변에 손가락을 드래그하는 행위에있는 동안 그려지고 어떻게 볼 수 없습니다, 그래서 당신은 touch_up까지의 ArrayList에 추가하지 않습니다.

onDraw는 ArrayList를 반복 할 때만 경로를 그립니다. 즉, ArrayList에 이미 저장되어있는 모든 항목과 함께 mPath를 그릴 수있는 onDraw에 코드를 추가합니다.

디버거에서 물건을 실행할 수 없다면 말하기 어렵지만 문제가 해결 될 수 있습니다.

+0

감사합니다 spartygw 나는 그에게 시도를 줄 것이다 – cmhdev

+0

나는 당신의 제안을 추가했고 그것은 즉시 효과가 있었다. ... 젠체하는 사람은 나에게 당신의 길을 약간의 업보를 보내 게했다. .. 고마워. – cmhdev

+0

굉장합니다. 후속 조치를 게시 해 주셔서 감사합니다. 일할 때 항상 듣는 것이 좋습니다. – spartygw

관련 문제