Drawing/Erasing using touch is a common feature in many mobile phone/tablet based apps. Writing code for this feature is pretty simple for android. Here are steps to do that:
This code finds the LinearLayout object which was defined in XML and then adds an object of DrawPanel to this. The DrawPanel is the custom View class which is being used for drawing. Here is the implementation of DrawPanel
The above code is for drawing. With a small tweak, erasing can be implemented. While erasing, set the paint color as that of background, that's it!!
The above code is used in Scratchpad, a drawing app for Android tablets.
- add a custom view on which you will draw.
- process touch event on view.
- on touch event, record points where touch event occurred.
- in onDraw method, draw points that were recorded.
Adding a custom view:
We will derive a class from View class for implementing drawing canvas. And we will add this to UI tree. To add this to UITree, create a LinearLayout in which the custom view will be added. The LinearLayout is declared in layout xml as:
and from the code, add the custom view to this LinearLayout as:This code finds the LinearLayout object which was defined in XML and then adds an object of DrawPanel to this. The DrawPanel is the custom View class which is being used for drawing. Here is the implementation of DrawPanel
The above code is for drawing. With a small tweak, erasing can be implemented. While erasing, set the paint color as that of background, that's it!!
The above code is used in Scratchpad, a drawing app for Android tablets.
hank you very much. Simple and accessible.
ReplyDeleteCan you mail me code on swapnil_go20@yahoo.co.in ???
ReplyDeleteI need it.
Please do the needful...
Dang, I so need this... any chance you can tell me how to get this line to work ?
ReplyDeletefrom the on draw method for(ArrayList stroke: strokes){
drawStroke(stroke, c);
I get a type mismatch error on this line...
Thanks for this code ( and fix hopefully ) Thanks.
Thanks for reporting typo.. corrected..
Deletethanks...please add more drawing samples
ReplyDeleteIt 's very helpful for me! Thank you very much!
ReplyDeletehow can i do it for multitouch ?? here only single line is drawn I want to draw full area which I touch... Can anyone help me??
ReplyDeleteA well-designed custom view is much like whatever other well-designed class.It encapsulates a particular set of functionality with a simple to utilize interface,it utilizes CPU and memory efficiently, and so forth.Notwithstanding being a well-designed class,however,a custom view should:Conform to Android standards,Provide custom style-able attributes that work with Android XML layouts,Send accessibility events,Be compatible with multiple Android platforms.
ReplyDelete----------------------------------------------
hidden movie game for android
How to implement Eraser?
ReplyDeleteI have read your blog its very attractive and impressive. I like it your blog.
ReplyDeleteJava Training in Chennai Core Java Training in Chennai Core Java Training in Chennai
Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai
This comment has been removed by the author.
ReplyDeletedriveragent plus product key
ReplyDeleteمزايا نقل الأثاث من قبل
ReplyDeleteواحدة من أهم ميزات شركة لنقل الأثاث في الرياض ، والتي تميزت عن غيرها من الشركات المتخصصة في مجال نقل الأثاث ، حيث توفر ، كأفضل شركة لنقل الأثاث في الرياض ،شركة نقل عفش
شركة نقل عفش من الرياض الى الامارات
شركة نقل اثاث من الرياض الى الامارات
شركة نقل عفش بالجبيل
Spyhunter 5 crack
ReplyDelete