2011-12-07 2 views
2

시간대 앱을 만들고 있습니다. 나는 onDraw와 onTouchEvent가 어떻게 작동하는지 테스트 중입니다.onTouchEvent를 사용할 때 일부 영역을 끌 수 없습니다.

나는 customeView를 만들고 8x10 선을 그립니다. 나는 onTouchEvent를 오버라이드하고 터치가 각 rect를지나 가면 rect는 녹색으로 채워진다.

왼쪽에서 오른쪽으로 드래그하고 위쪽에서 아래쪽으로 끌면 드래그가 올바르게 작동하고 각 직사각형이 색상으로 채워집니다. 하지만 뒤로 (오른쪽에서 왼쪽으로, 아래에서 위로) 끌면 첫 번째 줄과 마지막 줄이 작동하지 않습니다! 나는 RECT 내부의 위치를 ​​터치하더라도 의의 구형은 ..

난 내 코드에보고 정말 많은 시간을 해결하기 위해 노력하지만 확률값을 해결할 수있어 .. 색상으로 채워지지

내 코드를보고 도와주세요 ~!

public class TimeTableActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
       OnDrawing drawing = new OnDrawing(this); //customView 
    setContentView(drawing); 

} 

class OnDrawing extends View{ 
//x,y = first touch position, endX, endY = last touch position 
//startX, startY = calculate where to start for filling color. 
// stopX, stopY = calculate where to finish for filling color. 
    float x, y, endX, endY, startX, startY, stopX, stopY; 
    public OnDrawing(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     //Paint Color 1, 2 ,3 
     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     Paint paint2 = new Paint(); 
     paint2.setColor(Color.YELLOW); 
     Paint paint3 = new Paint(); 
     paint3.setColor(Color.GREEN); 

     /***row 10lines***/ 
     for(int i=0; i<11; i++){ 
      canvas.drawLine(0, getHeight()/10*i, getRight(), getHeight()/10*i, paint); 
     } 
     /***colum 8lines***/ 
     for(int i=0; i<9; i++){ 
      canvas.drawLine(getWidth()/8*i, 0, getWidth()/8*i, getBottom(), paint); 
     } 
     /***first rows background color***/ 
     for(int i=0; i<10; i++){ 
      canvas.drawRect(0, getHeight()/10, getWidth()/8, getBottom(), paint2); 
     } 
     /***first column background color***/ 
     for(int i=0; i<8; i++){ 
      canvas.drawRect(getWidth()/8, 0, getRight(), getHeight()/10, paint2); 
     } 
     /***first cell color***/ 
     canvas.drawRect(0, 0, getWidth()/8, getHeight()/10, paint3); 

     /***days in the first line***/ 
     canvas.drawText("Mon", getWidth()/8*1+10, getHeight()/10*1/2, paint); 
     canvas.drawText("Tue", getWidth()/8*2+10, getHeight()/10*1/2, paint); 
     canvas.drawText("Wed", getWidth()/8*3+10, getHeight()/10*1/2, paint); 
     canvas.drawText("Thu", getWidth()/8*4+10, getHeight()/10*1/2, paint); 
     canvas.drawText("Fri", getWidth()/8*5+10, getHeight()/10*1/2, paint); 
     canvas.drawText("Sat", getWidth()/8*6+10, getHeight()/10*1/2, paint); 
     canvas.drawText("Sun", getWidth()/8*7+10, getHeight()/10*1/2, paint); 


     /***time in the first line***/ 
     canvas.drawText("icon", 10, getHeight()/10*1/2, paint); 
     canvas.drawText("1", 10, getHeight()/10*1+getHeight()/10*1/2, paint); 
     canvas.drawText("2", 10, getHeight()/10*2+getHeight()/10*1/2, paint); 
     canvas.drawText("3", 10, getHeight()/10*3+getHeight()/10*1/2, paint); 
     canvas.drawText("4", 10, getHeight()/10*4+getHeight()/10*1/2, paint); 
     canvas.drawText("5", 10, getHeight()/10*5+getHeight()/10*1/2, paint); 
     canvas.drawText("6", 10, getHeight()/10*6+getHeight()/10*1/2, paint); 
     canvas.drawText("7", 10, getHeight()/10*7+getHeight()/10*1/2, paint); 
     canvas.drawText("8", 10, getHeight()/10*8+getHeight()/10*1/2, paint); 
     canvas.drawText("9", 10, getHeight()/10*9+getHeight()/10*1/2, paint); 
     canvas.drawText("10", 10, getHeight()/10*10+getHeight()/10*1/2, paint); 


     canvas.drawRect(startX, startY, stopX, stopY, paint3);//fill background color, from start position to stop position 

    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     switch(event.getAction()){ 
     case MotionEvent.ACTION_DOWN: 
      x = event.getX(); //first touch position 
      y = event.getY(); 

      break; 

     case MotionEvent.ACTION_MOVE: 
      endX = event.getX(); //last touch point 
      endY = event.getY(); 


      if(endX>x && endY>y){ //If Dragging toward right bottom 
      /***First Touch Position***/ 
      for(int i=0; i<8; i++){ 
       if(x>getWidth()/8*i && x<getWidth()/8*(i+1)){ 
        for(int j=0; j<10; j++){ 
         if(y>getHeight()/10*j && y<getHeight()/10*(j+1)){ 
          startX = getWidth()/8*i; //startX = left side of the cell 
          startY = getHeight()/10*j; //startY = upside of the cell 

         } 
        } 
       } 
      }//for 

      /***Last Touch position***/ 

      for(int i=0; i<8; i++){ 
       if(endX>getWidth()/8*i){ 
        for(int j=0; j<10; j++){ 
         if(endY>getHeight()/10*j && endY<getHeight()/10*(j+1)){ 
          stopX = getWidth()/8*(i+1); //stopX = right side of the cell 
          stopY = getHeight()/10*(j+1); //stopY = bottom side of the cell 
         } 
        } 
       } 
      }//for 


      if(endX<x && endY<y){ //if dragging toward left top side.(backward) this part is trouble 



       /***First Touch position***/ 
       for(int i=0; i<8; i++){ 
        if(x>getWidth()/8*i && x<getWidth()/8*(i+1)){ 
         for(int j=0; j<10; j++){ 
          if(y>getHeight()/10*j && y<getHeight()/10*(j+1)){ 
           startX = getWidth()/8*(i+1); //startX = right side of the cell 
           startY = getHeight()/10*(j+1); //startY = down side of the cell 

          } 
         } 
        } 
       }//for 

       /***Last Touch position***/ 

       for(int i=0; i<8; i++){ 
        if(endX>getWidth()/8*i){ 
         for(int j=0; j<10; j++){ 
          if(endY>getHeight()/10*j && endY<getHeight()/10*(j+1)){ 
           stopX = getWidth()/8*(i); //stopX = left side of the cell 
           stopY = getHeight()/10*(j); //stopY = upside of the cell 
          } 
         } 
        } 
       }//for 


      } 


      break; 
     } 

     invalidate(); 
     return true; 
    } 

} 

} 모든

enter image description here

답변

1

먼저, onDraw는 기능에 페인트 인스턴스를 작성 해달라고. 생성자의 어딘가에 한 번만 선언/작성하면 그리기 성능이 향상됩니다. 둘째 난 당신의 코드 (즉, 수학) 완전히하지만, 드래그 기능을 포함한 하나 개의 솔루션은 다음과 같이 수 있습니다 이해할 수 없었다 :

는 점 변수를 확인을 가정 해 봅시다 :

Point drag_distance_Point = new Point(); 

와 ACTION_MOVE에를 :

drag_distance_Point.set(start.x - end.x, start.y-end.y); 

이제이 점을 사각형 또는 드래그하려는 좌표에 추가하십시오. 실제로 이것은 현재 그려진 좌표에서 드래그 된 좌표를 더하거나 뺍니다. 이렇게하면 오브젝트가 움직이거나 드래그됩니다.

환호

+0

감사합니다. 귀하의 조언은 prob 문제를 해결하는 데 매우 도움이되었습니다. – beginners

+0

환영합니다. 친구 투표하십시오. –