2011-12-07 29 views
2

我在製作時間表應用程序。 我現在正在測試onDraw和onTouchEvent。使用onTouchEvent時無法拖動某個區域

我做了一個customeView並繪製了8x10行。 我重寫onTouchEvent,並且如果touch通過每個rect,則rect用綠色填充。

如果我從左到右拖動,從上到下,拖動工作正確,每個矩形都充滿了顏色。 但是當我拖回(從右到左,從下到上)時,第一行和最後一行不起作用! 即使我碰矩形內的位置,rects不填充顏色..

我在找到我的代碼,並試圖修復還真不少次,但不能解決出概率..

請看我的代碼並幫助我〜!

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

感謝您的幫助。你的建議對解決這個問題非常有幫助。 – beginners

+0

歡迎:) 老兄投票起來然後.. –