How to Create a Resizable Rectangle with User Touch Events on Android

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

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.

How to create a resizable circle with user touch events?

So, the center of circle will be:

float cx = (left + right) / 2f; 
float cy = (top + bottom) / 2f;

-- quite obvious. Radius can be calculated using Math.hypot():

float radius = Math.hypot(top - bottom, right - left) / 2f;

Thus, we have center and radius to draw a circle:

drawCircle(cx, cy, radius, paint);


Related Topics



Leave a reply



Submit