2011-10-07 127 views
0

我知道这个问题已经被问及在StackOverflow这里回答..但我似乎无法让变量工作。有人建议我使用这个代码,但仍然没有去。简单的解释是我需要一个功能来在你的手指停在屏幕上时运行(显然不能在UI内完成)。因此,一个新的线程.. x,y,高度,宽度。将参数传递给线程

//global variables 
boolean var = false; 
//instance variable to check if thread has started running for first time 
int x, y, width, height; 
// variables as instance variables 
boolean fingerdown; 

Thread myThread = new Thread() 
{  
    @Override 
    public void run() 
    {    
     while(fingerdown==true);   
     {     
      // class object = new class() in the activity area    
      object.function(this.x ,this.y , this.height, this.width);    
     }   
    }  
}; 

// this section is within the UI function that detections MotionEvents 
if (event.getAction() == MotionEvent.ACTION_DOWN) 
{ 
    this.x = event.getX();  
    this.y = event.getY();  
    this.width = widthf;  
    this.height = heightf; 
    fingerdown = true;  
    if(!var) 
    { 
     var = true; 
     myThread.start();  
    } 
} 

if (event.getAction() == MotionEvent.ACTION_UP) 
{ 
    fingerdown = false 
} 

回答

1
private class myThread extends Thread{ 

     int x, y; 
     public myThread(int x, int y) { 
      this.x = x; 
      this.y = y; 
     } 
     @Override 
     public void run() { 
      // your stuff 
     } 
    } 

要调用这个线程使用

 myThread obj = new myThread(100,200); 
     obj.start(); 
+0

的这个修改后的版本是什么得到它的工作..非常感谢你..我不是一个图形的人,所以我很少处理瓦特/线程。 – DJPlayer

+0

没有问题的朋友......你是最受欢迎的。 :) –

1

建设,而不是anonymousnamed类。

public class TestThread implements Runnable 
{ 
    private volatile boolean fingerdown; 
    @Override 
    public void run() 
    {    
     .... 
    } 
    public void stopThread() 
    { 
    fingerdown=false; 
    }  
} 

事件处理代码:

if (event.getAction() == MotionEvent.ACTION_DOWN) 
{ 
    this.x = event.getX();  
    this.y = event.getY();  
    this.width = widthf;  
    this.height = heightf; 
    fingerdown = true;  
    if(!var) 
    { 
     mythread=new TestThread(); 
     myThread.start();  
    } 
} 

if (event.getAction() == MotionEvent.ACTION_UP) 
{ 
    if(mythread!=null) 
    { 
     mythread.stopThread(); 
     mythread=null; 
    } 
    }