This article explains how to draw an Olympics Logo using the Canvas in Android.
You will first create an object of the Paint class. Then call the setColor() method of Paint to set the color of the circle by ing the color as an argument. The setStyle() method sets the style of the circle, here I ed a stroke as an argument that sets the circle having the stroke on its parameter.
The setStrokeWidth() method sets the stroke width of the circle. Finally, I called drawCircle() to draw the circle.
Do the same to draw each of the other five circles.
Step 1
Create the project like this:
Step 2
XML file
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Text" />
-
- </LinearLayout>
Step 3
Create a Java file with the following:
- package com.SampleCanvas;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
-
- public class Second extends Activity {
- SampleCanvasActivity drawView;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- drawView = new SampleCanvasActivity(this);
- drawView.setBackgroundColor(Color.WHITE);
- setContentView(drawView);
- }
- }
Step 4
Create another Java file with the following:
- package com.SampleCanvas;
-
- import java.util.ArrayList;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.graphics.Typeface;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.EditText;
- import android.widget.FrameLayout;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
-
- public class SampleCanvasActivity extends View {
-
- public SampleCanvasActivity(Context context) {
- super(context);
- }
-
- @Override
- public void onDraw(Canvas canvas) {
-
-
-
-
-
-
-
-
-
-
- Paint paint3=new Paint();
- Paint paint4=new Paint();
- Paint paint5=new Paint();
- Paint paint6=new Paint();
- Paint paint7=new Paint();
- Paint paint9=new Paint();
-
-
-
-
- paint3.setColor(Color.BLUE);
- paint3.setStrokeWidth(15);
- paint3.setStyle(Paint.Style.STROKE);
- canvas.drawCircle(150,200,100,paint3);
-
- paint4.setColor(Color.BLACK);
- paint4.setStrokeWidth(15);
- paint4.setStyle(Paint.Style.STROKE);
- canvas.drawCircle(380,200,100,paint4);
-
- paint5.setColor(Color.RED);
- paint5.setStrokeWidth(15);
- paint5.setStyle(Paint.Style.STROKE);
- canvas.drawCircle(610,200,100,paint5);
-
- paint6.setColor(Color.YELLOW);
- paint6.setStrokeWidth(15);
- paint6.setStyle(Paint.Style.STROKE);
- canvas.drawCircle(265,300,100,paint6);
-
- paint7.setColor(Color.GREEN);
- paint7.setStrokeWidth(15);
- paint7.setStyle(Paint.Style.STROKE);
- canvas.drawCircle(500,300,100,paint7);
-
- Paint paint8=new Paint();
- paint8.setColor(Color.DKGRAY);
- paint8.setTextSize(100);
- canvas.drawText("London",200,500,paint8);
-
- paint9.setColor(Color.DKGRAY);
- paint9.setTextSize(100);
- canvas.drawText("2012",250,600,paint9);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
Step 5
Android Manifest.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.databaseapplication"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="7"
- android:targetSdkVersion="16" />
-
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.databaseapplication.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>
Step 6
Image