Android

Android Canvas Draw Example
Author : Ishtek

Using Android API Level : 7 
In this example we shall see a very simple ways to work with Canvas API from
Android platform and I have used Canvas API from Android Platform.

Apart from simply drawing a predefined pattern on screen, we are going to see
little bit of touch event handling in form of ontouchlistener being implemented.

Idea here is to place a little circular red color spot on screen at the position
or place where touch on screen has occurred.

Main Activity Screens
Layout and widgets used in these activities are provided by coding only. Code for these activities are as shown below: main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > </LinearLayout>
DrawFunny.java
/** * This code is provided on "AS IS" basis without guarantee/warranty * of any kind. Use of this code subjected to user's own risk. * @Author: Ishtek */ package example.demo; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; public class DrawFunny extends Activity implements OnTouchListener { private float x; private float y; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyCustomPanel view = new MyCustomPanel(this); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); addContentView(view, params); view.setOnTouchListener(this); } private class MyCustomPanel extends View { public MyCustomPanel(Context context) { super(context); } @Override public void draw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.GREEN); paint.setStrokeWidth(6); canvas.drawLine(10,10,50,50,paint); paint.setColor(Color.RED); canvas.drawLine(50, 50, 90, 10, paint); canvas.drawCircle(50, 50, 3, paint); canvas.drawCircle(x,y,3,paint); } } public boolean onTouch(View v, MotionEvent event) { x = event.getX(); y = event.getY(); v.invalidate(); return true; } }
In the above activity code, we have used canvas to draw a V type shape with two arms of different colors. When any portion of the screen is touched, then onTouch method will be executed. This overridden method captured x and y position of the touch point on screen, and invalidate method is going to re-draw the view, thus showing a red filled circle at the location marked by x and y values. There is still another way of achieving this objective by using bitmap object associated with the view and drawing this bitmap on the canvas associated with the view. Following code does use draw caching functionality provided by Android Platform. DrawFunny.java
package example.demo; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; public class DrawFunny extends Activity implements OnTouchListener { private float x; private float y; private Bitmap bitmap; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyCustomPanel view = new MyCustomPanel(this); ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); addContentView(view, params); view.setOnTouchListener(this); view.setDrawingCacheEnabled(true); bitmap= view.getDrawingCache(); } private class MyCustomPanel extends View { public MyCustomPanel(Context context) { super(context); } @Override public void draw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.GREEN); paint.setStrokeWidth(6); if(bitmap != null) { bitmap.setPixel((int)x, (int)y, Color.RED); canvas.drawBitmap(bitmap, x, y, paint); } } } public boolean onTouch(View v, MotionEvent event) { x = event.getX(); y = event.getY(); bitmap = v.getDrawingCache(); v.invalidate(); return true; } }
Hope this helps.
If interested in commenting related to Android Canvas Draw Example
Click Here

Name (To be shown on this Page:*

Email (Not to be shown):*

Website (To be shown on this Page)

Your Comments here (To be shown on this Page):*

(You can use <a> Tag in your comments)

Please enter same number as shown here 685

Name: AshishDated: 2013-10-28 16:18:17
THANKS    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: Ashani PatraDated: 2013-11-22 00:56:30
this help a lot    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: AbhiDated: 2013-11-27 13:59:10
Hello    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: rupeshDated: 2013-12-12 11:45:58
i want to change a color of bitmap using seekbar by create a four seekbar of value red alpha green and blue help me    
Name: chinnaraoDated: 2016-05-17 11:17:03
I want bitmap drawn from center of screen, Please help me
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: JamalDated: 2013-12-28 23:19:26
Is there any ways to attach touch event for any specific image that is 
being drawn on Canvas?
I am looking for drawing multiple images on a canvas with different touch events attached to 
different images?
Any help would be appreciated.    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: rupeshDated: 2013-12-30 17:29:04
i want to change by bitmap color dynamically by getting the value of seekbar that represent four colors alpha red green blue please help  i tried on this from 1 week but cant solved it    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: RameshDated: 2014-02-06 10:09:46
How to approach designing screen for a stylus type capabilities in phones that is not having stylus capabilities?

I mean, trying to achieve stylus degree of flexibilities from finger touch capabilities.

    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: UmeshDated: 2014-02-16 09:50:29
How to draw a circle using Canvas class from Android API?
I have to provide an image withing the circle only.
just something like an Album with photos with circular border
area clipped.

Any ideas/approach that one can point here for me to look at it please.    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: sandeepDated: 2014-03-12 20:45:28
good    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: saravana pandianDated: 2014-04-23 09:00:49
I am doing a project that i must know about this topic in android...    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: MresDated: 2015-05-25 15:04:54
good to use    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: ortusDated: 2015-06-08 18:49:04
nice    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: RinkiDated: 2016-03-16 12:46:11
Great Article and good work!
I also found one article which I also like and would like to share with you people- http://findnerd.com/list/view/Draw-shapes-using-canvas-in-Android/2603/
    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552

Name: IshtekDated: 2016-08-15 18:14:01
Can you please provide example code for Android 4.0 version as well? Thanks in advance.    
Reply
Click Here

Name (To be shown on Page):*

Email (Optional): (never shown)

Website (To be shown on this Page)

Your Comments here (To be shown on Page):*

Please enter same number as shown here 552




Android Thread Example

REST Interview Questions

JAX-WS Webservice Example

Android Overlap SurfaceView Example

Spring3 Integration of File Endpoint

JAXWS Webservice Example

Android WebView Example

Android Context Menu Example

Setting up Eclipse for Android App Development

Android SensorEvent Orientation Example

Android ImageSwitcher Example

REST API Design for Android App

Android ExpandableListView Example

Singleton Serializable on Android

Android GPS with Google MAP Example

Android Project With Example

Android Project Manageing Files

Project Ideas for App on Android Platform

Android EditText with Background

Android SQLite Database Example

Android AutoCompleteTextView Example

Android Tester

Android Interview Questions

Android ListView Example

Android Intent Notification Example

Android Intent Broadcast Example

Android TextView Link Example

Android Sensor List

Android Sensor Orientation Example

Android Intent Broadcast Receiver

Android Gallery SurfaceViews Example

Android Location Example

Android Text to Speech Example

Android Intent Example

Android Layout Example

Android Download File Example

Android Expandable ListView Example

Android Tab Example

Android RelativeLayout Example

Android Intent Example

Android Text to Speech Example

Android SQLite Example

Android CustomView Example

Android Canvas Example

Android SharedPreferences Example

Android Shape Background Example

Android ShapeButton Example

Android ViewFlipper Example

Android Gallery Example

Android TimePickerDialog Example





If interested commenting on this Page :

comments powered by Disqus
Any source code found on any of the page(s) in Techiecommunity.net is associated with following notice: 
   /*
     * TECHIECOMMUNITY.NET MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
     * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
     * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
     * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. TECHIECOMMUNITY.NET SHALL NOT BE LIABLE FOR
     * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
     * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
     *
     * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
     * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
     * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
     * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
     * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
     * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
     * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). TECHIECOMMUNITY.NET
     * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
     * HIGH RISK ACTIVITIES.
     */
   
The content provided in this page is NOT warranted and/or NOT guaranteed by techiecommunity.net .
techiecommunity.net is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

For any of the content, if you would like to bring it to notice for removal from this web site, 
please write to this web site administrator @ EMAIL-ID (please remove space before and after @),
with appropriate concern and supporting proof(s). After thorough review and if found genuine concern,
we would take appropriate action and remove disputed content from this web site within 24 hours 
starting from the time it has brought to techiecommunity.net Administrator notice.

This website uses cookies and third party cookies to improve the user experience. 
And to provide more relevant ads. 
In case of continued use of this website implies that you accept the use of cookies on this website.
		
While using this web site, you agree to have read and accepted techiecommunity.net terms of use and privacy policy.