2011-12-07 13 views
2

タイムテーブルアプリケーションを作成しています。 私は今どのようにonDrawとonTouchEventをテストしています。onTouchEventを使用すると一部の領域をドラッグできません

私はcustomeViewを作成し、8x10行を描画します。 私はonTouchEventをオーバーライドし、タッチが各矩形を通過する場合、矩形は緑色で塗りつぶされます。

左から右に、上から下にドラッグすると、ドラッグが正しく機能し、各矩形が色で塗りつぶされます。 しかし、右から左に、下から上にドラッグすると、最初の行と最後の行は機能しません! 私はRECT内側の位置に触れていても、rectsが色で塗りつぶされていません。..

私は私のコードの中に見て、本当に何度も修正しようとしましたが、PROBを解決することはできませんよ...

私のコードを見て、私を助けてください〜!

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

ようこそ: あなたのおかげです.. –

関連する問題