Wednesday 7 November 2012

Android Image Effecting

Step 1:Creat ConvolutionMatrix class

ConvolutionMatrix.java

package com.test.emboss;

import android.graphics.Bitmap;
import android.graphics.Color;

public class ConvolutionMatrix
{
    public static final int SIZE = 3;

    public double[][] Matrix;
    public double Factor = 1;
    public double Offset = 1;

    public ConvolutionMatrix(int size) {
        Matrix = new double[size][size];
    }

    public void setAll(double value) {
        for (int x = 0; x < SIZE; ++x) {
            for (int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = value;
            }
        }
    }

    public void applyConfig(double[][] config) {
    for(int x = 0; x < SIZE; ++x) {
    for(int y = 0; y < SIZE; ++y) {
    Matrix[x][y] = config[x][y];
    }
    }
    }

    public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

    int A, R, G, B;
    int sumR, sumG, sumB;
    int[][] pixels = new int[SIZE][SIZE];

    for(int y = 0; y < height - 2; ++y) {
    for(int x = 0; x < width - 2; ++x) {

    // get pixel matrix
    for(int i = 0; i < SIZE; ++i) {
    for(int j = 0; j < SIZE; ++j) {
    pixels[i][j] = src.getPixel(x + i, y + j);
    }
    }

    // get alpha of center pixel
    A = Color.alpha(pixels[1][1]);

    // init color sum
    sumR = sumG = sumB = 0;

    // get sum of RGB on matrix
    for(int i = 0; i < SIZE; ++i) {
    for(int j = 0; j < SIZE; ++j) {
    sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
    sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
    sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
    }
    }

    // get final Red
    R = (int)(sumR / matrix.Factor + matrix.Offset);
    if(R < 0) { R = 0; }
    else if(R > 255) { R = 255; }

    // get final Green
    G = (int)(sumG / matrix.Factor + matrix.Offset);
    if(G < 0) { G = 0; }
    else if(G > 255) { G = 255; }

    // get final Blue
    B = (int)(sumB / matrix.Factor + matrix.Offset);
    if(B < 0) { B = 0; }
    else if(B > 255) { B = 255; }

    // apply new pixel
    result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
    }
    }

    // final image
    return result;
    }
}


Test2.java

package com.test.emboss;

import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;

public class Test2 extends Activity {
ImageView img;
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
img = (ImageView) findViewById(R.id.test2imageview);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// img.setImageBitmap(applyHueFilter(BitmapFactory.decodeResource(getResources(),
// R.drawable.eee),3));
// img.setImageBitmap(createSepiaToningEffect(BitmapFactory.decodeResource(getResources(),
// R.drawable.eee),1,2,100,255));
// img.setImageBitmap(doHighlightImage(BitmapFactory.decodeResource(getResources(),
// R.drawable.eee)));
img.setImageBitmap(engrave(BitmapFactory.decodeResource(
getResources(), R.drawable.eee)));

}
});

}

public Bitmap ConvertToSepia(Bitmap sampleBitmap) {
ColorMatrix sepiaMatrix = new ColorMatrix();
float[] sepMat = { 0.3930000066757202f, 0.7689999938011169f,
0.1889999955892563f, 0, 0, 0.3490000069141388f,
0.6859999895095825f, 0.1679999977350235f, 0, 0,
0.2720000147819519f, 0.5339999794960022f, 0.1309999972581863f,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 };
sepiaMatrix.set(sepMat);
final ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(
sepiaMatrix);
Bitmap rBitmap = sampleBitmap.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Canvas myCanvas = new Canvas(rBitmap);
myCanvas.drawBitmap(rBitmap, 0, 0, paint);
return rBitmap;
}

public Bitmap toGrayscale(Bitmap bmpOriginal) {
int width, height;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();

Bitmap bmpGrayscale = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas c = new Canvas(bmpGrayscale);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
c.drawBitmap(bmpOriginal, 0, 0, paint);
return bmpGrayscale;
}

private Bitmap convertColorIntoBlackAndWhiteImage(Bitmap orginalBitmap) {
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);

ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
colorMatrix);

Bitmap blackAndWhiteBitmap = orginalBitmap.copy(
Bitmap.Config.ARGB_8888, true);

Paint paint = new Paint();
paint.setColorFilter(colorMatrixFilter);

Canvas canvas = new Canvas(blackAndWhiteBitmap);
canvas.drawBitmap(blackAndWhiteBitmap, 0, 0, paint);

return blackAndWhiteBitmap;
}

public Bitmap ConvertToNegative(Bitmap sampleBitmap) {
ColorMatrix negativeMatrix = new ColorMatrix();
float[] negMat = { -1, 0, 0, 0, 255, 0, -1, 0, 0, 255, 0, 0, -1, 0,
255, 0, 0, 0, 1, 0 };
negativeMatrix.set(negMat);
final ColorMatrixColorFilter colorFilter = new ColorMatrixColorFilter(
negativeMatrix);
Bitmap rBitmap = sampleBitmap.copy(Bitmap.Config.ARGB_8888, true);
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
Canvas myCanvas = new Canvas(rBitmap);
myCanvas.drawBitmap(rBitmap, 0, 0, paint);
return rBitmap;
}

public Bitmap fudiao(Bitmap bmpOriginal) {
int width, height, r, g, b, c, a, gry, c1, a1, r1, g1, b1, red, green, blue;
height = bmpOriginal.getHeight();
width = bmpOriginal.getWidth();
int depth = 30;

Bitmap bmpSephia = Bitmap.createBitmap(width, height,
Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bmpSephia);
Paint paint = new Paint();
ColorMatrix cm = new ColorMatrix();
cm.setScale(.3f, .3f, .3f, 1.0f);
ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
paint.setColorFilter(f);
canvas.drawBitmap(bmpOriginal, 0, 0, null);
for (int y = 1; y < height - 1; y++) {
for (int x = 1; x < width - 1; x++) {
c = bmpOriginal.getPixel(x, y);

r = Color.red(c);
g = Color.green(c);
b = Color.blue(c);

c1 = bmpOriginal.getPixel(x - 1, y - 1);

r1 = Color.red(c1);
g1 = Color.green(c1);
b1 = Color.blue(c1);

red = Math.max(67, Math.min(255, Math.abs(r - r1 + 128)));
green = Math.max(67, Math.min(255, Math.abs(g - g1 + 128)));
blue = Math.max(67, Math.min(255, Math.abs(b - b1 + 128)));
if (red > 255) {
red = 255;
} else if (red < 0) {
red = 0;
}

if (green > 255) {
green = 255;
} else if (green < 0) {
green = 0;
}

if (blue > 255) {
blue = 255;
} else if (blue < 0) {
blue = 0;
}

bmpSephia.setPixel(x, y, Color.rgb(red, green, blue));
// bmpSephia.setPixel(x, y, Color.argb(a1, red, green, blue));
}
}
return bmpSephia;
}

public class GrayscaleConverter extends AsyncTask<Bitmap, Integer, Bitmap> {
// private ProgressBar pb;
private ImageView iv;

// private Button butt;

public GrayscaleConverter(ImageView iv) {
// this.pb = pb;
this.iv = iv;
// this.butt = butt;
}

@Override
protected Bitmap doInBackground(Bitmap... bitmaps) {

if (bitmaps.length > 0) {
Bitmap img = bitmaps[0];
Bitmap copy = img.copy(img.getConfig(), true);
int width = img.getWidth();
int hight = img.getHeight();
for (int i = 0; i < width; i++) {
for (int j = 0; j < hight; j++) {
int pixel = img.getPixel(i, j);
int a = Color.alpha(pixel);
int r = Color.red(pixel);
int g = Color.green(pixel);
int b = Color.blue(pixel);
r = g = b = (int) (0.21 * r + 0.71 * g + 0.07 * b);
copy.setPixel(i, j, Color.argb(a, r, g, b));
}
publishProgress((int) ((i / (float) width) * 100));
}
return copy;
}
return null;
}

protected void onProgressUpdate(Integer... progress) {
// pb.setProgress(progress[0]);
}

protected void onPostExecute(Bitmap result) {

iv.setImageBitmap(fudiao(result));
// pb.setVisibility(View.GONE);
// butt.setEnabled(true);
// butt.setText("Reload original image");
}
}

/*
* private void drawEmbossed(ImageView iv, int imID) { Bitmap bmIn =
* BitmapFactory.decodeResource(getResources(), imID); Bitmap bmOut =
* Bitmap.createBitmap(bmIn.getWidth(), bmIn.getHeight(), bmIn.getConfig());
* RenderScript rs = RenderScript.create(this); Allocation allocIn; allocIn
* = Allocation.createFromBitmap(rs, bmIn,
* Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
* Allocation allocOut = Allocation.createTyped(rs, allocIn.getType());
* ScriptC_emboss script = new ScriptC_emboss(rs,
* getResources(),R.raw.emboss); script.set_in(allocIn);
* script.set_out(allocOut); script.set_script(script);
* script.invoke_filter(); allocOut.copyTo(bmOut); iv.setImageBitmap(bmOut);
* }
*/

public static Bitmap emboss(Bitmap src) {
double[][] EmbossConfig = new double[][] { { 1, 1, 1 }, { -1, 0, -1 },
{ -1, 0, 1 } };
ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
convMatrix.applyConfig(EmbossConfig);
convMatrix.Factor = 1;
convMatrix.Offset = 127;
return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
}

public static Bitmap applySnowEffect(Bitmap source) {
// get image size
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
// get pixel array from source
source.getPixels(pixels, 0, width, 0, 0, width, height);
// random object
Random random = new Random();

int R, G, B, index = 0, thresHold = 50;
// iteration through pixels
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// get current index in 2D-matrix
index = y * width + x;
// get color
R = Color.red(pixels[index]);
G = Color.green(pixels[index]);
B = Color.blue(pixels[index]);
// generate threshold
thresHold = random.nextInt(255);
if (R > thresHold && G > thresHold && B > thresHold) {
pixels[index] = Color.rgb(100, 100, 100);
}
}
}
// output bitmap
Bitmap bmOut = Bitmap
.createBitmap(width, height, Bitmap.Config.RGB_565);
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
return bmOut;
}

public static Bitmap applySaturationFilter(Bitmap source, int level) {
// get image size
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
float[] HSV = new float[3];
// get pixel array from source
source.getPixels(pixels, 0, width, 0, 0, width, height);

int index = 0;
// iteration through pixels
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// get current index in 2D-matrix
index = y * width + x;
// convert to HSV
Color.colorToHSV(pixels[index], HSV);
// increase Saturation level
HSV[1] *= level;
HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
// take color back
pixels[index] |= Color.HSVToColor(HSV);
}
}
// output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
return bmOut;
}

public static Bitmap applyReflection(Bitmap originalImage) {
// gap space between original and reflected
final int reflectionGap = 4;
// get image size
int width = originalImage.getWidth();
int height = originalImage.getHeight();

// this will not scale but will flip on the Y axis
Matrix matrix = new Matrix();
matrix.preScale(1, -1);

// create a Bitmap with the flip matrix applied to it.
// we only want the bottom half of the image
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
height / 2, width, height / 2, matrix, false);

// create a new bitmap with same width but taller to fit reflection
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);

// create a new Canvas with the bitmap that's big enough for
// the image plus gap plus reflection
Canvas canvas = new Canvas(bitmapWithReflection);
// draw in the original image
canvas.drawBitmap(originalImage, 0, 0, null);
// draw in the gap
Paint defaultPaint = new Paint();
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
// draw in the reflection
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

// create a shader that is a linear gradient that covers the reflection
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0,
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
// set the paint to use this shader (linear gradient)
paint.setShader(shader);
// set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);

return bitmapWithReflection;
}

public static Bitmap applyHueFilter(Bitmap source, int level) {
// get image size
int width = source.getWidth();
int height = source.getHeight();
int[] pixels = new int[width * height];
float[] HSV = new float[3];
// get pixel array from source
source.getPixels(pixels, 0, width, 0, 0, width, height);

int index = 0;
// iteration through pixels
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// get current index in 2D-matrix
index = y * width + x;
// convert to HSV
Color.colorToHSV(pixels[index], HSV);
// increase Saturation level
HSV[0] *= level;
HSV[0] = (float) Math.max(0.0, Math.min(HSV[0], 360.0));
// take color back
pixels[index] |= Color.HSVToColor(HSV);
}
}
// output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
return bmOut;
}

public static Bitmap createSepiaToningEffect(Bitmap src, int depth,
double red, double green, double blue) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// constant grayscale
final double GS_RED = 0.3;
final double GS_GREEN = 0.59;
final double GS_BLUE = 0.11;
// color information
int A, R, G, B;
int pixel;

// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
// get color on each channel
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// apply grayscale sample
B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);

// apply intensity level for sepid-toning on each channel
R += (depth * red);
if (R > 255) {
R = 255;
}

G += (depth * green);
if (G > 255) {
G = 255;
}

B += (depth * blue);
if (B > 255) {
B = 255;
}

// set new pixel color to output image
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}

// return final image
return bmOut;
}

public static Bitmap doHighlightImage(Bitmap src) {
// create new bitmap, which will be painted and becomes result image
Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96,
src.getHeight() + 96, Bitmap.Config.ARGB_8888);
// setup canvas for painting
Canvas canvas = new Canvas(bmOut);
// setup default color
canvas.drawColor(0, PorterDuff.Mode.CLEAR);

// create a blur paint for capturing alpha
Paint ptBlur = new Paint();
ptBlur.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
int[] offsetXY = new int[2];
// capture alpha into a bitmap
Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
// create a color paint
Paint ptAlphaColor = new Paint();
ptAlphaColor.setColor(0xFFFFFFFF);
// paint color for captured alpha region (bitmap)
canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
// free memory
bmAlpha.recycle();

// paint the image source
canvas.drawBitmap(src, 0, 0, null);

// return out final image
return bmOut;
}

public static Bitmap doInvert(Bitmap src) {
// create new bitmap with the same settings as source bitmap
Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(),
src.getConfig());
// color info
int A, R, G, B;
int pixelColor;
// image size
int height = src.getHeight();
int width = src.getWidth();

// scan through every pixel
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// get one pixel
pixelColor = src.getPixel(x, y);
// saving alpha channel
A = Color.alpha(pixelColor);
// inverting byte for each R/G/B channel
R = 255 - Color.red(pixelColor);
G = 255 - Color.green(pixelColor);
B = 255 - Color.blue(pixelColor);
// set newly-inverted pixel to output image
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}

// return final bitmap
return bmOut;
}

public static Bitmap createContrast(Bitmap src, double value) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// color information
int A, R, G, B;
int pixel;
// get contrast value
double contrast = Math.pow((100 + value) / 100, 2);

// scan through all pixels
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
A = Color.alpha(pixel);
// apply filter contrast for every channel R, G, B
R = Color.red(pixel);
R = (int) (((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (R < 0) {
R = 0;
} else if (R > 255) {
R = 255;
}

G = Color.red(pixel);
G = (int) (((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (G < 0) {
G = 0;
} else if (G > 255) {
G = 255;
}

B = Color.red(pixel);
B = (int) (((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
if (B < 0) {
B = 0;
} else if (B > 255) {
B = 255;
}

// set new pixel color to output bitmap
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}

// return final image
return bmOut;
}

public static Bitmap rotate(Bitmap src, float degree) {
// create new matrix
Matrix matrix = new Matrix();
// setup rotation degree
matrix.postRotate(degree);
// return new bitmap rotated using matrix
return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(),
matrix, true);
}

public static Bitmap applyMeanRemoval(Bitmap src) {
double[][] MeanRemovalConfig = new double[][] { { -1, -1, -1 },
{ -1, 9, -1 }, { -1, -1, -1 } };
ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
convMatrix.applyConfig(MeanRemovalConfig);
convMatrix.Factor = 1;
convMatrix.Offset = 0;
return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
}

public static Bitmap engrave(Bitmap src) {
ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
convMatrix.setAll(0);
convMatrix.Matrix[0][0] = -2;
convMatrix.Matrix[1][1] = 2;
convMatrix.Factor = 1;
convMatrix.Offset = 95;
return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
}
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@xml/darkstrip"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/test2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/eee" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@xml/darkstrip"
        android:padding="10dp"
        android:text="Effect"
        android:textColor="#fff" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:background="@xml/darkstrip"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/test2imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>


Photo Effects:

















Sunday 4 November 2012

Android Draw Route between Multiple Address Pointer with Dialog

Android Draw Route between Multiple Address Pointer with Dialog

I have post previously draw route path between multiple distance Refer:http://kumarjkpm.blogspot.in/2012/10/android-draw-route-path-between.html

Then we need to modify
MainActivity.java
package com.froger.routepathexample;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class MainActivity extends MapActivity {
private MapView mapView;
GeoPoint SourceGeoPoint,DescGeoPoint,thirdGeoPoint;
Drawable drawable;
MyItemizedOverlay itemizedOverlay,itemizedOverlay1,itemizedOverlay2;
@Override
protected boolean isRouteDisplayed() { return false; }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        setContentView(R.layout.main);
        mapView = (MapView)findViewById(R.id.mapview);       
        mapView.setBuiltInZoomControls(true);
        List<Overlay> mapOverlays = mapView.getOverlays();

//Here is the your database Value  
String valuesA="13.787404"+"*"+"100.375900"+"*"+"Chennai";//Values of Point A
String valuesB="13.787404"+"*"+"100.375900"+"*"+"Bangalore";//Values of Point B
String valuesC="13.787404"+"*"+"100.375900"+"*"+"Salem,";//Values of Point C


        SourceGeoPoint = new GeoPoint((int) (13.0473735 * 1E6), (int) (80.2442964 * 1E6));// First point
drawable = getResources().getDrawable(R.drawable.first);
itemizedOverlay = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem = new OverlayItem(SourceGeoPoint, "Address A",valuesA);
itemizedOverlay.addOverlay(overlayItem);

        DescGeoPoint= new GeoPoint((int) (13.005485 * 1E6), (int) (77.5840933 * 1E6));// Second point
        drawable = getResources().getDrawable(R.drawable.second);
itemizedOverlay1 = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem2 = new OverlayItem(DescGeoPoint, "Address B",valuesB);
itemizedOverlay1.addOverlay(overlayItem2);

        thirdGeoPoint= new GeoPoint((int) (11.6523558 * 1E6), (int) (78.1566461 * 1E6));//Third Point  
        drawable = getResources().getDrawable(R.drawable.third);
itemizedOverlay2 = new MyItemizedOverlay(drawable, mapView);
OverlayItem overlayItem3 = new OverlayItem(thirdGeoPoint, "Address C",valuesC);
itemizedOverlay2.addOverlay(overlayItem3);

mapOverlays.add(itemizedOverlay);
mapOverlays.add(itemizedOverlay1);
mapOverlays.add(itemizedOverlay2);
final MapController mc = mapView.getController();
mc.animateTo(SourceGeoPoint);
mc.setZoom(16);
        Route route = directions(SourceGeoPoint,DescGeoPoint);
        Route route1 = directions(DescGeoPoint,thirdGeoPoint);
        RouteOverlay routeOverlay = new RouteOverlay(route, Color.BLUE);
        RouteOverlay routeOverlay1 = new RouteOverlay(route1, Color.BLUE);
        mapView.getOverlays().add(routeOverlay);
        mapView.getOverlays().add(routeOverlay1);
      }
    
      
    private Route directions(final GeoPoint start, final GeoPoint dest) {
        Parser parser;
        String jsonURL = "http://maps.google.com/maps/api/directions/json?";
        final StringBuffer sBuf = new StringBuffer(jsonURL);
        sBuf.append("origin=");
        sBuf.append(start.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(start.getLongitudeE6()/1E6);
        sBuf.append("&destination=");
        sBuf.append(dest.getLatitudeE6()/1E6);
        sBuf.append(',');
        sBuf.append(dest.getLongitudeE6()/1E6);
        sBuf.append("&sensor=true&mode=driving");
        parser = new GoogleParser(sBuf.toString());
        Route r =  parser.parse();
        return r;
    }    
}

BalloonOverlayView.java
package com.froger.routepathexample;

import java.util.List;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class BalloonOverlayView<Item extends OverlayItem> extends FrameLayout {

private LinearLayout layout;
private TextView title,snippet,point,foodlevel;
MyItemizedOverlay itemizedOverlay;
List<Overlay> mapOverlays;
ProgressDialog dialog;
static boolean flagstatus=false;
public BalloonOverlayView(final Context context, int balloonBottomOffset) {
super(context);
dialog = new ProgressDialog(context);
setPadding(10, 0, 10, balloonBottomOffset);
layout = new LinearLayout(context);
layout.setVisibility(VISIBLE);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.balloon_overlay, layout);
title = (TextView) v.findViewById(R.id.balloon_item_title);
snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
point= (TextView) v.findViewById(R.id.balloon_item_Point);
foodlevel= (TextView) v.findViewById(R.id.balloon_item_floodlevel);
ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
close.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
layout.setVisibility(GONE);



}
});
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.NO_GRAVITY;
addView(layout, params);
}

public void setData(Item item) {
layout.setVisibility(VISIBLE);
if (item.getTitle() != null) {
point.setVisibility(VISIBLE);
point.setText(item.getTitle());
} else {
point.setVisibility(GONE);
}
if (item.getSnippet() != null) {
snippet.setVisibility(VISIBLE);
title.setVisibility(VISIBLE);
foodlevel.setVisibility(VISIBLE);
String s[]=item.getSnippet().split("\\*");
title.setText("latitude :"+s[0]);
snippet.setText("longitude :"+s[1]);
foodlevel.setText("City :"+s[2]);
} else {
snippet.setVisibility(GONE);
title.setVisibility(GONE);
foodlevel.setVisibility(GONE);
}
}



}

balloon_overlay.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/balloon_main_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/pointer"
    android:orientation="horizontal"
    android:padding="3dp" >

    <LinearLayout
        android:id="@+id/balloon_inner_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:layout_weight="1"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/balloon_item_Point"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="14dip"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/balloon_item_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="13dip" >
        </TextView>

        <TextView
            android:id="@+id/balloon_item_snippet"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="13dip" >
        </TextView>

        <TextView
            android:id="@+id/balloon_item_floodlevel"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textColor="#FF000000"
            android:textSize="13dip" >
        </TextView>
    </LinearLayout>

    <ImageView
        android:id="@+id/close_img_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow" />

</LinearLayout>

Screen shorts: