URAndroid
URAndroid

Reputation: 6277

Draw a Rectangle or Shape at Touch Point(Co-Ordinates) on the Image

How to Draw a rectangle (Using Shape Resources) at the touched point(like coordinates 28,87). I have Created a shape Like This.

android:shape="rectangle" >


<solid 
    android:color="@color/transparent"/>
<stroke

    android:width="3dp"
    android:color="@color/green" />

This rectangle I want to Draw at the touch point on the image.

Upvotes: 2

Views: 7955

Answers (2)

Alfaplus
Alfaplus

Reputation: 1723

@kam' solution needs an update I guess. Everything that was in the constructor must be in init() method and the constructors must be overwritten 3 times.

public class MyView extends View {

private float xDown = 0,yDown = 0, xUp = 0, yUp = 0;

Paint mPaint;

boolean touched = false;
public MyView(Context context) {
    super(context);
    init(context);
}

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

public MyView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context);
}

private void init(Context context) {
    mPaint = new Paint();
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
}

@Override
protected void onDraw (Canvas canvas)  {
    canvas.drawColor(Color.TRANSPARENT);
    if(touched) {
        canvas.drawRect(xDown, yDown, xUp, yUp, mPaint);

    }
}

@Override
public boolean onTouchEvent (MotionEvent event) {
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            xDown = event.getX();
            yDown = event.getY();

            xUp = 0;
            yUp = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            xUp = event.getX();
            yUp = event.getY();
            touched = true;
            break;
        case MotionEvent.ACTION_UP:
            xUp = event.getX();
            yUp = event.getY();
            touched = true;
            break;
    }
    invalidate();
    return true;
}

}

Upvotes: 2

karn
karn

Reputation: 6033

You can draw a shape on a view in the onDraw() method of that view. There is no method available for drawing a shape drawable on a view canvas.
And you don't have to use a shape drawable for drawing a rectangle. You can draw a rectangle using canvas.drawRect() method. Here is a code for this:

public class MyView extends View{

float x,y;
Bitmap bmp;
Paint mPaint;
float width = 100.0f;
float height = 50.0f;

boolean touched = false;

public MyView (Context context)
{
    super(context);
    x = y = 0;
    mPaint = new Paint();
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Style.STROKE);
}

@Override
protected void onDraw (Canvas canvas)
{
    canvas.drawColor(Color.WHITE);
    if(touched)
    {
        canvas.drawRect(x, y, x+width, y+height, mPaint);

    }   
}

@Override
public boolean onTouchEvent (MotionEvent event)
{
    touched = true;
    //getting the touched x and y position
    x = event.getX();
    y = event.getY();
    invalidate();
    return true;
}

}

Upvotes: 3

Related Questions