Android Graphics
Computer graphics is an art which is used to draw charts,lines,circles,etc by using electronic deveices and programming.Computer graphics consist of number of pixel.Pixel is the unit represented on the computer screen. Now we are going to draw Line,Circle,Rectangle,Oval by using onDraw().
You can download source code here.
Create Android Project with name as DrawShape with package name "com.etr.drawshape". Create Activity with name as DrawActivity. This is the code for DrawActivity.
This is code for activity_draw.xml
You can download source code here.
Override onDraw method
To use this onDraw() first we need to inherit View class.parameter of onDraw() is Canvas.The Canvas class have methods for drawing text, lines, bitmaps, and many other graphics primitives.By using this onDraw() we can create custom user interface.
Create Drawing Objects
The android .graphics framework divided into two areas:
- Canvas handles what to draw.
- Paint handles how to draw.
For Example, Canvas provides a method to draw a Oval, while Paint provides methods to define that Oval's color. Canvas has a method to draw a circle, while Paint defines whether to fill that circle with a color or leave it empty.
Create Android Project with name as DrawShape with package name "com.etr.drawshape". Create Activity with name as DrawActivity. This is the code for DrawActivity.
package com.etr.drawshape;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
public class DrawActivity extends Activity {
LinearLayout layout;
Spinner spin;
ArrayList<String> value = new ArrayList<String>();
String selectedShape;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
value.add("Circle");
value.add("Line");
value.add("Oval");
value.add("Rectangle");
spin = (Spinner) findViewById(R.id.shapes);
layout = (LinearLayout) findViewById(R.id.shpes_body);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, value);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedShape = value.get(arg2);
layout.addView(new GraphicsView(DrawActivity.this));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.draw, menu);
return true;
}
class GraphicsView extends View {
Paint paint;
int width;
int height;
RectF rect;
public GraphicsView(Context context) {
super(context);
setFocusable(true);
init();
}
private void init() {
try {
paint = new Paint();
rect = new RectF(50, 50, 150, 100);
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.LTGRAY);
canvas.drawPaint(paint);
paint.setColor(Color.RED);
width = getWidth();
height = getHeight();
if (selectedShape.equals("Circle")) {
canvas.drawCircle(width / 2, height / 2, 25, paint);
} else if (selectedShape.equals("Line")) {
canvas.drawLine(width / 2 - 50, height / 2 - 50,
width / 2 + 50, height / 2 + 50, paint);
} else if (selectedShape.equals("Oval")) {
canvas.drawOval(rect, paint);
} else if (selectedShape.equals("Rectangle")) {
canvas.drawRect(50, 50, 150, 100, paint);
}
}
}
}
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
public class DrawActivity extends Activity {
LinearLayout layout;
Spinner spin;
ArrayList<String> value = new ArrayList<String>();
String selectedShape;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_draw);
value.add("Circle");
value.add("Line");
value.add("Oval");
value.add("Rectangle");
spin = (Spinner) findViewById(R.id.shapes);
layout = (LinearLayout) findViewById(R.id.shpes_body);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, value);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
selectedShape = value.get(arg2);
layout.addView(new GraphicsView(DrawActivity.this));
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.draw, menu);
return true;
}
class GraphicsView extends View {
Paint paint;
int width;
int height;
RectF rect;
public GraphicsView(Context context) {
super(context);
setFocusable(true);
init();
}
private void init() {
try {
paint = new Paint();
rect = new RectF(50, 50, 150, 100);
} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.LTGRAY);
canvas.drawPaint(paint);
paint.setColor(Color.RED);
width = getWidth();
height = getHeight();
if (selectedShape.equals("Circle")) {
canvas.drawCircle(width / 2, height / 2, 25, paint);
} else if (selectedShape.equals("Line")) {
canvas.drawLine(width / 2 - 50, height / 2 - 50,
width / 2 + 50, height / 2 + 50, paint);
} else if (selectedShape.equals("Oval")) {
canvas.drawOval(rect, paint);
} else if (selectedShape.equals("Rectangle")) {
canvas.drawRect(50, 50, 150, 100, paint);
}
}
}
}
This is code for activity_draw.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GraphicsActivity" >
<Spinner
android:id="@+id/shapes"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/shpes_body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".GraphicsActivity" >
<Spinner
android:id="@+id/shapes"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/shpes_body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical" />
</LinearLayout>
Comments
Post a Comment