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
Manually Put Files to Android Emulator Sd Card
Android M Camera Intent + Permission Bug
Realmchangelistener Does Not Get Called When Realm Gets Updated in Notificationlistenerservice
How to Set Custom Actionbar Color/Style
How to Get an Uri of an Image Resource in Android
Ionic Build Android | Error: No Installed Build Tools Found. Please Install the Android Build Tools
How to Prevent an Application from Being Uninstalled
Android Studio 3.1 "Run" Not Compiling Code
Detecting When User Has Dismissed the Soft Keyboard
How to Set Multiple Alarms Using Alarm Manager in Android
How to Open or Simulate a Click on an Android Preference, Created with Xml, Programmatically
How Do Mpandroidchart Renderers Work and How to Write a Custom Renderer