How to I use touch screen to draw a straight line

by gandor » Tue, 14 Apr 2009 13:05:12 GMT


Sponsored Links
 Hi,

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



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();

                }

               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;
        }


     }


<?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>



--~--~---------~--~----~------------~-------~--~----~



How to I use touch screen to draw a straight line

by Raphael » Tue, 14 Apr 2009 13:33:25 GMT


 A couple suggestions:

- don't make your x/y fields static.
- in onTouch, test that MotionEvent.getAction() is ACTION_DOWN.
- to draw you should create a custom View class and override the
onDraw method. See ApiDemos or the LunarLander samples for a start.

R/




--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Other Threads

1. m5 GetMethod.getResponseBody as String in SDK 0.9

Hi,

Any idea about where can I find GetMethod class's
getResonseBodyAsString() method in SDK 0.9.

I need to perform the web service client activity, it was fine in the
m5 but I can't find the method to retrieve web service respone as
string in new SDK.
--~--~---------~--~----~------------~-------~--~----~

2. SystemClock.setCurrentTimeMillis() faile

I am using v0.9 beta SDK and got the following warning when I call
SystemClock.setCurrentTimeMillis()

WARN/SystemClock(1000): Unable to open alarm driver: Permission denied

Does anyone know which permission should be specified?

--~--~---------~--~----~------------~-------~--~----~

3. Why is my e-mail being spammed by this group

4. Feature request : Aggregate view of different calendars

5. text color won't change for TextView.setEnabled(false

6. is there a way to install back the SDK m5-rc15 after upgrading into 0.9 beta

7. More camera misery with SDK 0.9 beta