Skip to main content

Android Simple Graphics Example

The android.graphics.Canvas can be used to draw graphics in android. It provides methods to draw oval, rectangle, picture, text, line etc.
The android.graphics.Paint class is used with canvas to draw objects. It holds the information of color and style.
In this example, we are going to display 2D graphics in android.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <TextView  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/hello_world" />  
  15.   
  16. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.simplegraphics;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.content.Context;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Color;  
  9. import android.graphics.Paint;  
  10. import android.view.View;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     DemoView demoview;  
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         demoview = new DemoView(this);  
  20.         setContentView(demoview);  
  21.     }  
  22.   
  23.     private class DemoView extends View{  
  24.         public DemoView(Context context){  
  25.             super(context);  
  26.         }  
  27.   
  28.         @Override protected void onDraw(Canvas canvas) {  
  29.             super.onDraw(canvas);  
  30.   
  31.             // custom drawing code here  
  32.             Paint paint = new Paint();  
  33.             paint.setStyle(Paint.Style.FILL);  
  34.   
  35.             // make the entire canvas white  
  36.             paint.setColor(Color.WHITE);  
  37.             canvas.drawPaint(paint);  
  38.               
  39.             // draw blue circle with anti aliasing turned off  
  40.             paint.setAntiAlias(false);  
  41.             paint.setColor(Color.BLUE);  
  42.             canvas.drawCircle(202015, paint);  
  43.   
  44.             // draw green circle with anti aliasing turned on  
  45.             paint.setAntiAlias(true);  
  46.             paint.setColor(Color.GREEN);  
  47.             canvas.drawCircle(602015, paint);  
  48.   
  49.             // draw red rectangle with anti aliasing turned off  
  50.             paint.setAntiAlias(false);  
  51.             paint.setColor(Color.RED);  
  52.             canvas.drawRect(100520030, paint);  
  53.                            
  54.             // draw the rotated text  
  55.             canvas.rotate(-45);  
  56.                       
  57.             paint.setStyle(Paint.Style.FILL);  
  58.             canvas.drawText("Graphics Rotation"40180, paint);  
  59.               
  60.             //undo the rotate  
  61.             canvas.restore();  
  62.         }  
  63.     }  
  64.     @Override  
  65.     public boolean onCreateOptionsMenu(Menu menu) {  
  66.         // Inflate the menu; this adds items to the action bar if it is present.  
  67.         getMenuInflater().inflate(R.menu.main, menu);  
  68.         return true;  
  69.     }  
  70. }  


Output:

android simple graphics example output 1

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM Abstract Allocating service capacities in cloud computing is based on the assumption that they are unlimited and can be used at any time. However, available service capacities change with workload and cannot satisfy users’ requests at any time from the cloud provider’s perspective because cloud services can be shared by multiple tasks. Cloud service providers provide available time slots for new user’s requests based on available capacities. In this paper, we consider workflow scheduling with deadline and time slot availability in cloud computing. An iterated heuristic framework is presented for the problem under study which mainly consists of initial solution construction, improvement, and perturbation. Three initial solution construction strategies, two greedy- and fair-based improvement strategies and a perturbation strategy are proposed. Different strategies in the three phases result in several heuristics. ...

MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework

            MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework ABSTRACT  In recent years, recommendation systems have seen significant evolution in the field of knowledge engineering. Most of the existing recommendation systems based their models on collaborative filtering approaches that make them simple to implement. However, performance of most of the existing collaborative filtering-based recommendation system suffers due to the challenges, such as: (a) cold start, (b) data sparseness, and (c) scalability. Moreover, recommendation problem is often characterized by the presence of many conflicting objectives or decision variables, such as users’ preferences and venue closeness. In this paper, we proposed MobiContext , a hybrid cloud-based Bi-Objective Recommendation Framework (BORF) for mobile social networks. The MobiContext utilizes multi-objective optimization techniques to generate personalized recommendat...