androidshapesuser-interaction

How to create a resizable rectangle with user touch events on Android?


I want to create a rectangular shape that will be resized with the touches of the user. Below image is a good example of what i want to do:

Resizable Rectangle

Is there any example like that? What do I need to study to implement this?

Thanks in advance,


Solution

  • To implement a custom view, you derive a class from View :) Override onDraw() for looks, override onTouchEvent() for input processing. Note that in Android, you cannot draw on view outside onDraw(); if you want to refresh the view, call invalidate().

    You can implement draggable corners as separate views. For looks, just use ready-made images (feel free to derive from ImageView). Dragging is implemented as moving your view in response to touch events. RelativeLayout is your friend for arbitrary view positining.

    You can add homemade views to the layout; just go to XML editing and type a <com.mypackage.MyViewClass> element.