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. HttpURLConnection with local network, socket time up

Hello!

I have a problem with my app, when I try to get data from a remote
server (in local network). I already try this:

1: I made a Ad-Hoc network with the server (apache2 + php5 + mysql5),
and everything works!. I use this method

public static int userLoginPSG(String user, String pass, String IMEI,
String number){
                //Arreglo que contiene los pares de datos: variable y valor
                ArrayList<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>();
                nameValuePairs.add(new 
BasicNameValuePair(RQ_FUNCTION,RQ_F_LOGIN));
                nameValuePairs.add(new BasicNameValuePair(RQ_USER,user));
                nameValuePairs.add(new BasicNameValuePair(RQ_PASS,pass));
                nameValuePairs.add(new BasicNameValuePair(RQ_IMEI,IMEI));
                //Realiza la consulta HTTP POST
                InputStream is = httpResponse(URL,nameValuePairs);
                //Convierte la respuesta (InputStream) a un String
                String response = null;
                if (is != null)
                        response = convertInputStream2String(is);
                else
                        //No se pudo conectar al servidor
                        return -1;
                //Realiza la transformacin de los datos a JSON
                int isLogged = -1;
                if ((response !=
null)&&(response.length()>0)&&(response.compareToIgnoreCase("null")!
=0))
                        isLogged = ParserJSON2VO.loginJSON2Boolean(response);
                //Se retorna el valor
                return isLogged;
        }

public static InputStream httpResponse(String url,
ArrayList<NameValuePair> nameValuePairs){
                //Realiza la consulta HTTP POST
                InputStream is = null;
                try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                HttpGet httpGet = new HttpGet(url);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
                is = null;
                }
                return is;
        }

And the server returns me a JSON encode... no problems.
-----------------------------------------------------------------------------

2: In a local network, with a special APN, I can connect my
cellphone. I use the browser to test the file .php and put som entries
like

http://172.29.XX.XX/bll.php?f=0&user=cmoreira&pass=kjsahdkhasdk

And I got results form that. (all correct)

3: Now, the problem is when I try to connect (using code) in the
local network to the server... the app waits in "HttpResponse response
= httpclient.execute(httppost);" for ever and ever.. and... ever...

4 I used

public static InputStream getInputStreamFromUrl(String urls) {
        HttpURLConnection con = null;
                URL url;
                InputStream is=null;
                try {
                        url = new URL(urls);
                        con = (HttpURLConnection) url.openConnection();
                        con.setReadTimeout(10000 /* milliseconds */);
                        con.setConnectTimeout(15000 /* milliseconds */);
                        con.setRequestMethod("GET");
                        con.setDoInput(true);
                        //con.addRequestProperty("Referer", 
"http://blog.dahanne.net");
                        // Start the query
                        con.connect();
                        is = con.getInputStream();
                }catch (IOException e) {
                        //handle the exception !
                        e.printStackTrace();
                }
                return is;

And I got the same problem in con.connect();
"SocketTimeOutException", "Socket is not connected"


What could be the reason? some proxy in server? in network?...

THX!

-- 

2. Populating the groups table in contacts2.db

Hi Group,

  Can you please throw light on the groups table in contacts2.db.

Who is populating it? Is it when the accounts are created ?

I have explicitly add contents into groups table,but then when the
phone is booted that table is empty and all contacts under the group
is deleted.


 Please help

-- 

3. help connecting my app to an external Mysql DB

4. GUI Design Patterns of the Twitter Client presented at Google I/O

5. For usage question

6. ListView : TextView : text on left and right not working

7. We are looking for a Software Engineer/Development Manager at Bangalore India.