I have three menu items: Line, Circle, and Rectangle. I want to set it so that depending on the menu item selected, that shape/line is drawn when the user drags their finger across the screen(aka rubber banding). Here's LineDrawView.java:
// Project: Java2Lab11_Lefelhocz
// File: LineDrawView.java
// Date: 4/10/13
// Author: Joshua Lefelhocz
// Description: custom view to draw lines on
package com.lcc.java2lab12_lefelhocz;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
// Notice this class extends View
public class LineDrawView extends View
{
// This view's bounds
@SuppressWarnings("unused")
private int xMin = 0;
@SuppressWarnings("unused")
private int xMax;
@SuppressWarnings("unused")
private int yMin = 0;
@SuppressWarnings("unused")
private int yMax;
private float currentX;
private float currentY;
private float startX;
private float endX;
private float startY;
private float endY;
// Paint object
private Paint paintFill;
// constructor
public LineDrawView(Context context)
{
// call the super class constructor
super(context);
// The Paint class holds the style and color information about how to draw geometries, text and bitmaps.
// For efficiency create the paint objects in the constructor, not in draw
// paint.setStrokeWidth(10); // works on lines
// You can change the color of Paint without effecting objects already drawn
// You can NOT change the style of Paint without effecting objects already drawn
// The Style, TextSize apply to all objects drawn with the paint.
// Create a default Paint object Style=Fill
paintFill = new Paint();
// set the background color when the view is created
this.setBackgroundColor(Color.LTGRAY);
}
// Called to draw the view. Also called by invalidate().
@Override
protected void onDraw(Canvas canvas)
{
paintFill.setColor(Color.BLACK);
canvas.drawLine(startX, startY, endX, endY, paintFill);
}
// Called when the view is first created or its size changes.
@Override
public void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
// Set the view bounds
xMax = width-1;
yMax = height-1;
}
public boolean onTouchEvent(MotionEvent event)
{
currentX = event.getX();
currentY = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
startX = currentX;
startY = currentY;
return true;
case MotionEvent.ACTION_MOVE:
endX = currentX;
endY = currentY;
invalidate();
return true;
case MotionEvent.ACTION_UP:
return true;
}
return super.onTouchEvent(event);
}
}
Extra details: I am starting to get it to work, but am having trouble making the distance formula {sqrt(x2-x1)^2 + (y2-y1)^2)}
.
stackoverflow.comm
No comments:
Post a Comment