For those who are still looking for a solution, who will leave what I did, the solution is granted by the user @Triode I just adjust the code to my need.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
public class SimpleDrag extends View {
private final int INVALID_INDEX = -1;
private final int mTotalItems = 3;
private boolean isInitLine = false;
private int mWidth, mHeigth;
private ArrayList<Rect> mItemsCollection;
private ArrayList<Point> mActiveDragPoints;
private ArrayList<Rect> mActiveRects;
private Paint mPaint;
/**
* @param context
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, int width, int height) {
super(context);
mWidth = width;
mHeigth = height;
init();
}
/**
* @param context
* @param attrs
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs, int width, int height) {
super(context, attrs);
mWidth = width;
mHeigth = height;
init();
}
/**
* @param context
* @param attrs
* @param defStyle
* @return of type SimpleDrag
* Constructor function
* @since Feb 19, 2013
* @author rajeshcp
*/
public SimpleDrag(Context context, AttributeSet attrs, int defStyle, int width, int height) {
super(context, attrs, defStyle);
mWidth = width;
mHeigth = height;
init();
}
/* (non-Javadoc)
* @see android.view.View#onDraw(android.graphics.Canvas)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//canvas.drawColor(Color.BLUE, PorterDuff.Mode.CLEAR);
for( Rect rect : mItemsCollection){
canvas.drawRect(rect, mPaint);
}
}
/**
* @param of type null
* @return of type null
* function which will initialize the view
* @since Feb 20, 2013
* @author rajeshcp
*/
private void init(){
mActiveRects = new ArrayList<Rect>(mTotalItems);
mActiveDragPoints = new ArrayList<Point>(mTotalItems);
mItemsCollection = new ArrayList<Rect>();
for( int i = 0; i < mTotalItems; i++){
Rect rect = null;
if ( i == 0){
rect = new Rect(0, mHeigth / 2, mWidth, (mHeigth / 2) + 4);
}else if ( i == 1){
rect = new Rect(0, (mHeigth / 2) + 220, mWidth, ((mHeigth / 2) + 220) + 4);
}else if ( i == 2){
rect = new Rect(0, (mHeigth / 2) - 220, mWidth, ((mHeigth / 2) - 220) + 4);
}
mItemsCollection.add(rect);
}
mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.RED);
}
/* (non-Javadoc)
* @see android.view.View#onTouchEvent(android.view.MotionEvent)
* @since Feb 19, 2013
* @author rajeshcp
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getActionMasked();
final int pointer = event.getActionIndex();
switch (action) {
case MotionEvent.ACTION_DOWN :
Point touchDown = new Point((int)event.getX(), (int)event.getY());
lookForIntersection(touchDown);
break;
case MotionEvent.ACTION_UP :
case MotionEvent.ACTION_CANCEL :
mActiveDragPoints.removeAll(mActiveDragPoints);
mActiveRects.removeAll(mActiveRects);
break;
case MotionEvent.ACTION_MOVE :
int count = 0;
for(Rect rect : mActiveRects){
Point curretPoint = new Point((int)event.getX(count), (int)event.getY(count));
moveRect(curretPoint, mActiveDragPoints.get(count), rect);
count++;
}
Log.d(getClass().getName(), "Active Rects" + mActiveRects.size());
Log.d(getClass().getName(), "Active Points" + mActiveDragPoints.size());
invalidate();
break;
case MotionEvent.ACTION_POINTER_DOWN :
touchDown = new Point((int)event.getX(pointer), (int)event.getY(pointer));
lookForIntersection(touchDown);
//Log.d(getClass().getName(), "ACTION_POINTER_DOWN" + pointer);
break;
case MotionEvent.ACTION_POINTER_UP :
int index = getIntersectionRectIndex(new Point((int)event.getX(pointer), (int)event.getY(pointer)));
if( index != INVALID_INDEX ){
Rect rect = mItemsCollection.get(index);
mActiveDragPoints.remove(mActiveRects.indexOf(rect));
mActiveRects.remove(rect);
}
break;
default:
break;
}
return true;
}
/**
* @param touchDown of type Point
* @return of type null
* function which will find the
* intersecting rect and add to the
* active collection
* @since Feb 20, 2013
* @author rajeshcp
*/
private void lookForIntersection(Point touchDown){
final int index = getIntersectionRectIndex(touchDown);
if( index != INVALID_INDEX ){
final Rect rect = mItemsCollection.get(index);
if( mActiveRects.indexOf(rect) == INVALID_INDEX ){
mActiveDragPoints.add(touchDown);
mActiveRects.add(mItemsCollection.get(index));
}
}
Log.d(getClass().getName(), "Active Rects" + mActiveRects.size());
Log.d(getClass().getName(), "Active Points" + mActiveDragPoints.size());
}
/**
* @param point of type Point
* @return of type int
* function which will return the index of
* the rect contaning the given point
* @since Feb 20, 2013
* @author rajeshcp
*/
private int getIntersectionRectIndex(final Point point){
int index = INVALID_INDEX;
for(Rect rect : mItemsCollection){
if(rect.top < point.y && point.y < rect.top + 40){
index = mItemsCollection.indexOf(rect);
break;
}
}
return index;
}
/**
* @param currentPoint of type Point
* @param prevPoint of type Point
* @param rect of type Rect
* @return of type null
* function which will move the change the
* bounds of teh rect
* @since Feb 20, 2013
* @author rajeshcp
*/
private void moveRect(Point currentPoint, Point prevPoint, final Rect rect){
int xMoved = currentPoint.x - prevPoint.x;
int yMoved = currentPoint.y - prevPoint.y;
rect.set(0, rect.top + yMoved, rect.right + xMoved, rect.bottom + yMoved);
// rect.set(rect.left + xMoved, rect.top + yMoved, rect.right + xMoved, rect.bottom + yMoved);
mActiveDragPoints.set(mActiveDragPoints.indexOf(prevPoint), currentPoint);
}
}