You aren't limiting this to ACTION_DOWN and/or ACTION_UP so on every
ACTION_MOVE, you are running this code. Probably the reason it
crashes. I'd do this:
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
//Set Line Start Point
static_x = event.getX();
static_y = event.getY();
break;
}
case MotionEvent.ACTION_UP:
{
//Set Line End Point
static_x = event.getX();
static_y = event.getY();
break;
}
}
}
You'd actually do the drawing of the line in the onDraw function.
On Apr 13, 5:12 pm, gandor <[email protected]> wrote:
> Hi Folks,
>
> Want to draw a line when I touch screen at two points.
> I can see OnTouch been invoked but after than everything breaks looks,
> the application crashes
>
> I am using drawLine in Canvas.
> Please let me know what I am doing wrong
>
> Thanks a ton
>
> ----------------
> <?xml version="1.0" encoding="utf-8"?>
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/
> android"
> android:id="@+id/layout_id"
> android:orientation="vertical"
> android:layout_width="fill_parent"
> android:layout_height="fill_parent"
> >
> <TextView
> android:layout_width="fill_parent"
> android:layout_height="wrap_content"
> android:text="@string/hello"
> />
> </LinearLayout>
> --------------
>
> package <>.DrawPoints;
>
> import android.app.Activity;
> import android.content.Context;
> import android.graphics.Canvas;
> import android.graphics.Color;
> import android.graphics.Paint;
> import android.os.Bundle;
> import android.util.Log;
> import android.view.MotionEvent;
> import android.view.View;
> import android.view.View.OnTouchListener;
> import android.widget.TextView;
>
> public class DrawPoints extends Activity implements OnTouchListener{
> static int i = 0;
> static float static_x = 0;
> static float static_y = 0;
> static float static_x1 = 0;
> static float static_y1 = 0;
>
> /** Called when the activity is first created. */
> @Override
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.main);
> View topLayout = this.findViewById(R.id.layout_id);
> // register for events for the view, previously
> topLayout.setOnTouchListener((OnTouchListener) this);
>
> }
>
> // the following callback is called when touch event on screen
> happens
> @Override
> public boolean onTouch(View v, MotionEvent event) {
> String tag = null;
>
> if(i == 0) {
> static_x = event.getX();
> static_y = event.getY();
> i = 1;
> } else
> {
> static_x1 = event.getX();
> static_y1 = event.getY();
>
> }
> // want to draw a line at this stage after figuring out two screen
> touches have been performed ??
> if(i == 1) {
> Paint p = new Paint();
> p.setColor(Color.WHITE);
> p.setStyle(Paint.Style.STROKE);
> Canvas canvas = new Canvas();
> canvas.drawColor(Color.BLUE);
>
> canvas.drawLine(static_x, static_y, static_x1, static_y1,
> p);
> i = 0;
> }
>
> return false;
> }
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---