Java - Lag al mover imageview (touch)

 
Vista:

Lag al mover imageview (touch)

Publicado por 70n1 (5 intervenciones) el 02/02/2016 00:42:50
Buenas gente, espero que todo bien por hay.

La cosa es que estoy desarollando un control para el PC. (Streaming de la pantalla y manejo del raton desde android)

El touch va perfectamente, pero cuando junto la clase touch con la clase de streaming el touch se relentiza con mucho lag.

Sabeis el porque?

Este es el codigo del touch class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
public class Touchpad {
 
	private ViewGroup mainLayout;
	private ImageView image;
	private ImageView image1;
	private TextView text1;
	private TextView text2;
	private Display display;
	private int xDelta;
	private int yDelta;
 
 
 
	 Context mContext;
 
 
 
	void EmpesarTouchpad(Activity a, Context b)
	{
		mainLayout = (RelativeLayout) a.findViewById(R.id.root);
		image = (ImageView) a.findViewById(R.id.img);
		image1 = (ImageView) a.findViewById(R.id.img1);
		text1=(TextView) a.findViewById(R.id.textView1);
	    text2=(TextView) a.findViewById(R.id.textView2);
 
	    this.mContext = b;
	display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 
				image1.setOnTouchListener(onTouchListener());
 
 
 
 
		//Quitamos la barra de título de nuestra aplicación
 
 
 
 
	}
 
	private OnTouchListener onTouchListener() {
		return new OnTouchListener() {
 
			@SuppressLint("ClickableViewAccessibility")
			@Override
			public boolean onTouch(View view, MotionEvent event) {
 
				final int x = (int) event.getRawX();
				final int y = (int) event.getRawY();
 
				switch (event.getAction() & MotionEvent.ACTION_MASK) {
 
				case MotionEvent.ACTION_DOWN:
					RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
					image.getLayoutParams();
 
					xDelta = x - lParams.leftMargin;
					yDelta = y - lParams.topMargin;
					break;
 
				case MotionEvent.ACTION_UP:
 
					break;
 
				case MotionEvent.ACTION_MOVE:
					RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) image
							.getLayoutParams();
 
 
 
			        if((x - xDelta)<0){break;};
			        if((y - yDelta)<0){break;};
 
			        if((x - xDelta)>display.getWidth()){break;};
			        if((y - yDelta)>display.getHeight()){break;};
 
					layoutParams.leftMargin = x - xDelta;
					layoutParams.topMargin = y - yDelta;
					layoutParams.rightMargin = 0;
					layoutParams.bottomMargin = 0;
					image.setLayoutParams(layoutParams);
					text1.setText(Integer.toString(x - xDelta));
					text2.setText(Integer.toString(y - yDelta));
					break;
				}
				image.invalidate();
				return true;
			}
		};
	}
 
 
 
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Lag al mover imageview (touch)

Publicado por arck (145 intervenciones) el 02/02/2016 13:02:44
Si puedes poner el de streaming e indicar como lo juntas creo que falicitaría el entenderlo un poco.

entiendo que si ejecutas el touchpad va bien y si ejecutar el streaming va bien, pero si los lanzas a la vez no.

¿Puede ser que la ejecución de uno y otro sea secuencial? cosa que creo que no puede ser porque entiendo que el refresco del touchpad no es el mismo que el de el streaming.
(pero todo son ideas, porque no entiendo bien tu pregunta)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Lag al mover imageview (touch)

Publicado por 7on1 (5 intervenciones) el 02/02/2016 21:39:23
Agradeceria cualquier ayuda.

Este es el touch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
public class Touchpad {
 
 
 
	private ViewGroup mainLayout;
 
	private ImageView image;
 
	private ImageView image1;
 
	private TextView text1;
 
	private TextView text2;
 
	private Display display;
 
	private int xDelta;
 
	private int yDelta;
 
 
 
 
 
 
 
	 Context mContext;
 
 
 
 
 
 
 
	void EmpesarTouchpad(Activity a, Context b)
 
	{
 
		mainLayout = (RelativeLayout) a.findViewById(R.id.root);
 
		image = (ImageView) a.findViewById(R.id.img);
 
		image1 = (ImageView) a.findViewById(R.id.img1);
 
		text1=(TextView) a.findViewById(R.id.textView1);
 
	    text2=(TextView) a.findViewById(R.id.textView2);
 
 
 
	    this.mContext = b;
 
	display = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
 
 
 
				image1.setOnTouchListener(onTouchListener());
 
 
 
 
 
 
 
 
 
		//Quitamos la barra de título de nuestra aplicación
 
 
 
 
 
 
 
 
 
	}
 
 
 
	private OnTouchListener onTouchListener() {
 
		return new OnTouchListener() {
 
 
 
			@SuppressLint("ClickableViewAccessibility")
 
			@Override
 
			public boolean onTouch(View view, MotionEvent event) {
 
 
 
				final int x = (int) event.getRawX();
 
				final int y = (int) event.getRawY();
 
 
 
				switch (event.getAction() & MotionEvent.ACTION_MASK) {
 
 
 
				case MotionEvent.ACTION_DOWN:
 
					RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
 
					image.getLayoutParams();
 
 
 
					xDelta = x - lParams.leftMargin;
 
					yDelta = y - lParams.topMargin;
 
					break;
 
 
 
				case MotionEvent.ACTION_UP:
 
 
 
					break;
 
 
 
				case MotionEvent.ACTION_MOVE:
 
					RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) image
 
							.getLayoutParams();
 
 
 
 
 
 
 
			        if((x - xDelta)<0){break;};
 
			        if((y - yDelta)<0){break;};
 
 
 
			        if((x - xDelta)>display.getWidth()){break;};
 
			        if((y - yDelta)>display.getHeight()){break;};
 
 
 
					layoutParams.leftMargin = x - xDelta;
 
					layoutParams.topMargin = y - yDelta;
 
					layoutParams.rightMargin = 0;
 
					layoutParams.bottomMargin = 0;
 
					image.setLayoutParams(layoutParams);
 
					text1.setText(Integer.toString(x - xDelta));
 
					text2.setText(Integer.toString(y - yDelta));
 
					break;
 
				}
 
				image.invalidate();
 
				return true;
 
			}
 
		};
 
	}
 
 
 
 
 
 
 
}


Y este el streaming:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
public class Pantalla {
 
	  private BluetoothAdapter btAdapter = null;
	  private BluetoothSocket btSocket = null;
	  private DataInputStream inStream = null;
	  private DataOutputStream outStream = null;
	  private Handler mHandler = new Handler();
	  // Well known SPP UUID
 
 
 
 
	  private static final UUID MY_UUID =
	      UUID.fromString( "0000abcd-0000-0000-0000-000000000000");
 
	  ImageView image;
 
 
	  // Insert your server's MAC address
	  private  String address = "00:00:00:00:00:00";
 
 
	  int bufferSize = 1024;
	  byte[] buffer = new byte[bufferSize];
	  /** Called when the activity is first created. */
	  public void EmpesarPantalla(Activity a,String b) {
 
 
	     image = (ImageView) a.findViewById(R.id.imageView1);
 
 
	    address= b;
 
 
	    btAdapter = BluetoothAdapter.getDefaultAdapter();
 
	//////////////////////////////////////////////////////////////
 
 
 
 
 
 
 
	    // Set up a pointer to the remote node using it's address.
	    BluetoothDevice device = btAdapter.getRemoteDevice(address);
 
	    // Two things are needed to make a connection:
	    //   A MAC address, which we got above.
	    //   A Service ID or UUID.  In this case we are using the
	    //     UUID for SPP.
	    try {
	      btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
	    } catch (IOException e) {
 
        }
 
	    // Discovery is resource intensive.  Make sure it isn't going on
	    // when you attempt to connect and pass your message.
	   btAdapter.cancelDiscovery();
 
	    // Establish the connection.  This will block until it connects.
	    try {
	      btSocket.connect();
 
	    } catch (IOException e) {
	      try {
	        btSocket.close();
	      } catch (IOException e2) {
 
          }
	    }
 
	    // Create a data stream so we can talk to server.
 
 
 
 
 
 
 
 
 
 
	 //////////////////////////////////////////16983
	    mHandler.postDelayed(runnable, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	  //////////////////////////////////////////////////////////////////////
 
                  private Runnable runnable = new Runnable() {
                	    public void run() {
 
 
 
 
 
                	      	int tamaño=0;
                	      	try {
                	    			 inStream = new DataInputStream(btSocket.getInputStream());
                	    		tamaño= inStream.readInt();
 
                	    		//din.read();
 
                	      	} catch (IOException e) {
                	    			// TODO Auto-generated catch block
                	    			e.printStackTrace();
                	    		}
                	      	/*
                	      	try {
                	    			outStream = new DataOutputStream(btSocket.getOutputStream());
                	    		outStream.writeInt(tamaño);
                	      	} catch (IOException e) {
                	    			// TODO Bloque catch generado automáticamente
                	    			e.printStackTrace();
                	    		}
                	    		*/
                	          //out.setText(Integer.toString(tamaño));
 
                	                      // run any code here...         
 
                	          ////////////////////////////////////////
 
 
                	    	      try
                	    	      {
                	    	    	  //OutputStream os = new FileOutputStream("/sdcard/1/asdf"+toni+".jpeg");
                	    		  /////////////////////////////
 
                	    	    	  byte[] buffer      = new byte[tamaño];
 
                	    	    	  inStream.readFully(buffer);
                	    	    	  //byte c[] = outputStream.toByteArray( );
                	    	    	  	      //os.write(result);    
 
                	    	    	  	      //os.close();
 
                	    	    	  	    Bitmap bmp = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
                	    	    	  	  image.setImageBitmap(bmp);
 
 
 
                	    	    	  //os.flush();;
 
 
                	    	    	    	 //os.close();
                	    	    	  //out.setText("\n"+totalBytesRead);
                	    	    	     //toni++;
 
 
 
 
 
 
                	    	      }
 
                	    	      catch(Exception ex){ System.exit(0);  }
 
 
                	          /////////////////////////////////////////
                	                      // queue the task to run again in 1 seconds...
                	                      mHandler.postDelayed(runnable, 0);
 
 
 
 
 
 
                	    }
 
 
 
 
 
 
                  };
 
 
 
 
 
 
 
 
public void stop() {
 
	mHandler.removeCallbacks(runnable);
	try {
		 outStream = new DataOutputStream(btSocket.getOutputStream());
	outStream.writeInt(0);
	outStream.close();
	inStream.close();
btSocket.close();
	//din.read();
 
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
 
}
 
 
 
 
 
 
 
 
 
 
	}

Y los inicio asi:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
 
public class Remote extends Activity {
	Pantalla pant=new Pantalla();
	Touchpad touch=new Touchpad();
 
	  @Override
	  public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.remote);
 
 
	    /////////////////////////////////
	    Bundle bundle = getIntent().getExtras();
	    String address = bundle.getString("direccion");
	    /*
	    Intent open  = new Intent(Remote.this, Pantalla.class);
	   	 open.putExtra("direccion", address);
	   	startActivity(open);
	   	*/
 
 
	    pant.EmpesarPantalla(this,address);
 
	    touch.EmpesarTouchpad(this,this);
 
	  }
 
	  @Override
	  public boolean onKeyDown(int keyCode, KeyEvent event)
	  {
	  if ((keyCode == KeyEvent.KEYCODE_BACK))
	  {
 
		  pant.stop();
	  //aquí vendría las acciones que tengo que realizar
 
		  return super.onKeyDown(keyCode, event);
 
	  }
 
	  return super.onKeyDown(keyCode, event);
	  }
 
	  @Override
	  public void onStart() {
	    super.onStart();
 
	  }
 
	  @Override
	  public void onResume() {
		  super.onResume();
 
	  }
 
 
 
 
 
 
 
 
 
 
 
	  @Override
	  public void onPause() {
		  super.onPause();
	  }
 
	  @Override
	  public void onStop() {
		  super.onStop();
 
 
 
 
 
	  }
 
	  @Override
	  public void onDestroy() {
		  super.onDestroy();
	  }
 
 
 
 
 
 
 
 
	  //////////////////////////////////////////////////////////////////////
 
 
 
	}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Lag al mover imageview (touch)

Publicado por arck (145 intervenciones) el 03/02/2016 09:45:36
¿podrías probar con los dos por separado pero a la vez?

Lo miro esta tarde en casa a ver si se me ocurre algo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Lag al mover imageview (touch)

Publicado por 70n1 (5 intervenciones) el 03/02/2016 11:56:47
Como que probarlos por separado a la vez?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Lag al mover imageview (touch)

Publicado por arck (145 intervenciones) el 04/02/2016 23:21:38
A ver si lo entendí bien o la cague mucho.

Tu el tanto el touch como el streaming envían información al móvil, pero para ello ejecutas los dos a la vez, secuencialmente.

lo que yo indico es que no los ejecutes secuencialmente, genérate hilos, un hilo para la imagen otro para el touch.

La teoría que tengo es que como están en secuencial (te digo que ni mire el código porque no tengo tiempo a nada), la ejecución es imagen touch, imagen touch, pero el touch siempre va a mas frecuencia que la imagen y no debería depender el ratón de la frecuencia de la imagen. Por esta razón ejecutarlo en diferentes hilos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar