Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am new to android application.I want to know how to draw over the image in android application?
Posted
Comments
thhangaraj 27-Sep-11 4:04am    
I want to know draw line over the image

try this Code:

Java
package com.cal;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class CircleCanvas extends View
{
    public List<circleinfo> mCircleInfos = new ArrayList<circlecanvas.circleinfo>();

    public static class CircleInfo
    {
        private float x;
        private float y;
        private float radius;
        private int color;

        public float getX()
        {
            return x;
        }
        public void setX(float x)
        {
            this.x = x;
        }
        public float getY()
        {
            return y;
        }
        public void setY(float y)
        {
            this.y = y;
        }
        public float getRadius()
        {
            return radius;
        }
        public void setRadius(float radius)
        {
            this.radius = radius;
        }
        public int getColor()
        {
            return color;
        }
        public void setColor(int color)
        {
            this.color = color;
        }
    }
    public CircleCanvas(Context context)
    {
        super(context);
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        for (CircleInfo circleInfo : mCircleInfos)
        {
            Paint paint = new Paint();
            paint.setColor(circleInfo.getColor());
            canvas.drawCircle(circleInfo.getX(), circleInfo.getY(), circleInfo.getRadius(), paint);
        }
    }
}
</circlecanvas.circleinfo></circleinfo>


and use it in the Activity ,eg in btn clicked:
just like this:
Java
listener3 = new OnClickListener() {
            public void onClick(View v) {
                 Random random = new Random();
                    float randomX =( 100 + random.nextInt(100));        //
                    float randomY =( 100 + random.nextInt(100));         //
                    float randomRadius =( 20 + random.nextInt(40));     //
                    int randomColor = 0;
                    //
                    if(random.nextInt(100) > 50)
                    {
                        randomColor = Color.BLUE;
                        //randomColor = Color.RED;
                    }
                    //
                    else
                    {
                        if(random.nextInt(100) > 50)
                            randomColor = Color.RED;
                        else
                            randomColor = Color.GREEN;
                    }
                    CircleInfo circleInfo = new CircleInfo();
                    circleInfo.setX(randomX);
                    circleInfo.setY(randomY);
                    circleInfo.setRadius(randomRadius);
                    circleInfo.setColor(randomColor);
                    mCircleCanvas.mCircleInfos.add(circleInfo);        //
                    mCircleCanvas.invalidate();                    //

            }
        };
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900