使用onTouchEvent时无法拖动某些区域

时间:2011-12-07 05:33:03

标签: android ontouchevent ondraw

我正在制作时间表应用。 我现在正在测试onDraw和onTouchEvent。

我制作了一个customeView并绘制了8x10行。 我重写了onTouchEvent,如果每个rect都经过了touch,则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 个答案:

答案 0 :(得分:1)

首先,不要在onDraw函数中创建绘图实例。仅在构造函数中的某处声明/创建它们,这将提高您的绘图性能。其次,我完全无法理解你的代码(即数学),但是包含拖动功能的一个解决方案可能是这样的:

制作一个点变量,让我们说:

Point drag_distance_Point = new Point();

和Action_Move:

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

现在将此点添加到矩形的坐标或您要拖动的任何内容。实际上,这将从当前绘制的坐标中添加/减去拖动的坐标。通过这样做,您的对象将移动/拖动。

干杯