Draw Rubber Band Line With An Ontouchevent
Ok, I'm trying to make a program that utilizes a linedrawview. When the user starts a touch event(action DOWN), it gets the current x and y and stores them in variables. Then whe
Solution 1:
Okay, I found out: Make sure to set the start position x and y, like this:
currentXExample = event.getX(); currentYExample = event.getY();
exampleStartX = currentXExample; exampleStartY = currentYExample;
exampleEndX = currentXExample;
exampleEndY = currentYExample;
You can leave it out of the Up event, but never leave it out of the MOVE event or it will not work.
LineDrawView.java
// Project: Java2Lab// File: LineDrawView.java// Date: 4/9/13// Author: Joshua Lefelhocz// Description: custom view to draw lines onpackage com.lcc.java2lab11lefelhocz;
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 ViewpublicclassLineDrawViewextendsView
{
// This view's boundsprivateintxMin=0;
privateint xMax;
privateintyMin=0;
privateint yMax;
privatefloat currentX;
privatefloat currentY;
privatefloat startX;
privatefloat endX;
privatefloat startY;
privatefloat endY;
// Paint objectprivate Paint paintFill;
// constructorpublicLineDrawView(Context context)
{
// call the super class constructorsuper(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 = newPaint();
// set the background color when the view is createdthis.setBackgroundColor(Color.LTGRAY);
}
// Called to draw the view. Also called by invalidate().@OverrideprotectedvoidonDraw(Canvas canvas)
{
//canvas.drawColor(Color.LTGRAY); // this works in onDraw to set the background color// Draw a Red Diagonal line from upper left corner to bottom right corner
paintFill.setColor(Color.BLACK);
canvas.drawLine(startX, startY, endX, endY, paintFill);
}
// Called when the view is first created or its size changes.@OverridepublicvoidonSizeChanged(int width, int height, int oldWidth, int oldHeight)
{
// Set the view bounds
xMax = width-1;
yMax = height-1;
}
publicbooleanonTouchEvent(MotionEvent event)
{
currentX = event.getX();
currentY = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
startX = currentX;
startY = currentY;
returntrue;
case MotionEvent.ACTION_MOVE:
endX = currentX;
endY = currentY;
invalidate();
returntrue;
case MotionEvent.ACTION_UP:
returntrue;
}
returnsuper.onTouchEvent(event);
}
}
Post a Comment for "Draw Rubber Band Line With An Ontouchevent"