Here you can use one of the methods (however, there are many possibilities for implementing the same). It is based on creating your own view for drawing and tracking the selection of a rectangle. Alternatively, you can simply apply the logic from onTouch() user view to OnTouchListener() .
The main layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent" android:id="@+id/root" android:background="@android:color/background_dark"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/image" android:src="@drawable/up_image" android:scaleType="fitXY" /> <com.example.TestApp.DragRectView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dragRect" /> </RelativeLayout>
Custom view:
import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.text.TextPaint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class DragRectView extends View { private Paint mRectPaint; private int mStartX = 0; private int mStartY = 0; private int mEndX = 0; private int mEndY = 0; private boolean mDrawRect = false; private TextPaint mTextPaint = null; private OnUpCallback mCallback = null; public interface OnUpCallback { void onRectFinished(Rect rect); } public DragRectView(final Context context) { super(context); init(); } public DragRectView(final Context context, final AttributeSet attrs) { super(context, attrs); init(); } public DragRectView(final Context context, final AttributeSet attrs, final int defStyle) { super(context, attrs, defStyle); init(); } public void setOnUpCallback(OnUpCallback callback) { mCallback = callback; } private void init() { mRectPaint = new Paint(); mRectPaint.setColor(getContext().getResources().getColor(android.R.color.holo_green_light)); mRectPaint.setStyle(Paint.Style.STROKE); mRectPaint.setStrokeWidth(5);
The activity is simple:
public class MyActivity extends Activity { private static final String TAG = "MyActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final DragRectView view = (DragRectView) findViewById(R.id.dragRect); if (null != view) { view.setOnUpCallback(new DragRectView.OnUpCallback() { @Override public void onRectFinished(final Rect rect) { Toast.makeText(getApplicationContext(), "Rect is (" + rect.left + ", " + rect.top + ", " + rect.right + ", " + rect.bottom + ")", Toast.LENGTH_LONG).show(); } }); } } }
The output is as follows:

source share