2011-10-07 187 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; 
    } 
    }