i am making a time table app.
i am now testing how does the onDraw and onTouchEvent.
I Made a customeView and draw 8×10 lines.
i override onTouchEvent, and if touch is passing by the each rect, the rect is filled with green color.
if i drag from left to right, and from up to bottum, the drag is working right, and each rect is filled with color.
but when i drag back(from right to left, from bottom to up), the first line and last line doesn’t work!
even though I touch the position inside the rect, the rects are not filled with color..
i’m looking into my code and tried to fix really many times but cannot solve out the prob..
please see my code and help me~!
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;
}
}
}

First of all, dont create paint instances in onDraw function. Declare/Create them only once somewhere in constructor, this will improve your drawing performance. Secondly I couldn’t understand your code (i.e. Mathematics) completely but one solution for including drag functionality might be like this:
Make a point variable, let say:
and on Action_Move:
now add this point to the coordinates of rectangle or anything you want to drag. Actually this will add/subtract the dragged coordinates from your currently drawn coordinates. By doing this your object will move/drag.
Cheers