Drawing an Olympic Logo Using Canvas in Android

Introduction

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:

imagecanvasappli.jpg

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 {

    //Paint paint = new Paint();

    public SampleCanvasActivity(Context context) {

        super(context);

    }

 

    @Override

    public void onDraw(Canvas canvas) {

 

       // paint.setColor(Color.BLACK);

       // paint.setStyle(Paint.Style.STROKE);

        //paint.setStrokeWidth(5);

       // canvas.drawRect(730, 430, 40, 20, paint);

 

        //Paint paint2=new Paint();

 

       // Typeface typeArial;

        //typeArial = Typeface.create("London",Typeface.ITALIC);

        Paint paint3=new Paint();

        Paint paint4=new Paint();

        Paint paint5=new Paint();

        Paint paint6=new Paint();

        Paint paint7=new Paint();

        Paint paint9=new Paint();

        //  paint2.setTextSize(35);

        //canvas.drawText("Deposit Ammount in your account", 140 - paint2.getTextSize(), 100, paint2);

        //paint.setTextSize(25);

        //canvas.drawText("Enter Amount", 300 - paint2.getTextSize(), 200, paint2);

        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);

 

        /*ImageView imageView = new ImageView(getContext());

        Bitmap mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        Bitmap mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        imageView.setImageBitmap( mainImage );*/

       // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.iamge1);

 

        //canvas.drawBitmap(bitmap, 120, 500, null);

        //canvas.drawBitmap(bitmap, 150, 500, null);

 

       // layout.setGravity(Gravity.BOTTOM);

       //paint.setStyle(Paint.Style.STROKE);

       // paint.setColor(Color.WHITE);

        //canvas.drawRect(130, 130, 80, 80, paint);

        //paint.setColor(Color.BLACK);

        //canvas.drawText("SomeText",25,20,paint);

        //paint.setTextSize(20);

        //canvas.drawText("Some Text", 10, 25, paint);

       /* canvas.drawRect(33, 60, 77, 77, paint );

        paint.setColor(Color.YELLOW);

        canvas.drawRect(33, 33, 77, 60, paint );*/

 

    }

}


 

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

 

 imageolympic.jpg


Up Next
    Ebook Download
    View all
    Learn
    View all