Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Android Graphics


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.     <TextView
  11.         android:layout_width=“wrap_content”
  12.         android:layout_height=“wrap_content”
  13.         android:text=“@string/hello_world” />
  14. </RelativeLayout>

Activity class

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

download this android example

Output:

simplegraphicsoutput1

Comments

comments