A very important problem to solve when using View for dynamic plotting is how to redraw the screen. The Android View class provides a postInvalidate () method, which automatically calls the onDraw method in the View to redraw the screen.
The following code allows you to drag the red line of any line. And automatically update the location of the line.
[Java]
/*
* Use a thread to update the View and draw a line.
*/Www.2cto.com
Public class ThreadUpdateView extends View
{
Paint paint = null;
Int y = 0;
Public ThreadUpdateView (Context context)
{
Super (context );
Paint = new Paint ();
MyThread myThread = new MyThread ();
MyThread. start (); // The red line automatically moves down after the thread is started.
This. setFocusable (true );
}
@ Override
Protected void onDraw (Canvas canvas)
{
Super. onDraw (canvas );
Paint. setColor (Color. RED );
Paint. setStrokeWidth (5 );
Canvas. drawLine (0, y, 444, y, paint); // draw line
}
Class MyThread extends Thread
{
@ Override
Public void run ()
{
// Keep adding his value, less than 400
While (y <1, 400)
{
Y ++;
Try
{
Thread. sleep (100); // update slowly
} Catch (InterruptedException e)
{
E. printStackTrace ();
}
PostInvalidate (); // The method in the View to re-draw the image and re-call the onDraw method because y has been updated.
}
}
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event)
{
Return super. onKeyDown (keyCode, event );
}
@ Override
Public boolean onKeyUp (int keyCode, KeyEvent event)
{
System. out. println ("View onKeyUp" + event. getAction ());
Return super. onKeyUp (keyCode, event );
}
/*
* Create a function to drag a line wherever it is.
*/
@ Override
Public boolean onTouchEvent (MotionEvent event)
{
System. out. println ("View" + event. getAction () + "" + event. getX () + ""
+ Event. getY ());
// Return super. onTouchEvent (event );
Y = (int) event. getY (); // assign the current drag position to y
PostInvalidate (); // call the onDraw () method again for repainting.
Return true;
}
}
There are two questions about screen events:
I. How can I allow a View to capture buttons and touch-screen events, instead of Activity?
We know that when you click on the screen, the system first responds to the onTouchEvent method (onKeyUp and other methods) in the Activity, instead of responding to the View we set. This will invalidate the onTouchEvent we wrote in the View.
There are two solutions:
Method 1. Use a proxy. That is, the onTouchEvent method in the Activity directly calls the onTouchEvent method in the ThreadUpdateView instance.
Method 2. Force the View to obtain the focus. Set this. setFocusable (true) in the View constructor );
2. How to Prevent the touch event from being passed to the Activity?
We know that touch events can be transmitted in Android. For example, after the View is executed, press the event and pass it to the Activity.
So how can we let him not pass it?
Solution:
Write the return value of onTouchEvent as return true;
Instead of return super. onTouchEvent (event );