2012-02-28 8 views
0

こんにちは私のコードで質問があります。以下に示すように、私は、OnDraw()メソッドの下にr.getBlink()を持っています。 "s"スレッドがOnDraw()を呼び出すかどうかを判断する関数はありますか?私はthis.getClass()。getBlink()を試みたが、エラーとして表示されることを意味する。助けてください。メソッドを実行可能にしてスレッドを呼び出すオブジェクトを取得する方法は?

 public class main extends Activity { 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(new BitmapView(this)); 
      } 
    } 

    class BitmapView extends View implements OnTouchListener{ 
      Circle r = new Circle(); 
      Circle s = new Circle(); 

      public BitmapView(Context context) { 
      super(context); 
        this.setOnTouchListener(this); 
      r.starter(); 
      s.starter(); 
} 

public void onDraw(Canvas canvas){ 

    Paint paint = new Paint(); 
    if (r.getBlink()==true){ 
     paint.setColor(Color.YELLOW); 
     r.setBlink(false); 
    } 
    else{ 
     paint.setColor(Color.BLACK); 
     r.setBlink(true); 
    } 

    paint.setStyle(Paint.Style.FILL); 
    canvas.drawCircle(r.getX(), r.getY(), 15, paint); 
    invalidate(); 
} 
class Circle implements Runnable{ 
    Random rnd=new Random(); 
    int x=rnd.nextInt(200), y=rnd.nextInt(200); 
    boolean blink = false; 
    public int getX(){ return this.x; } 
    public int getY(){ return this.y; } 
    public boolean getBlink() { return this.blink; } 
    public void setBlink(boolean b) { this.blink=b; } 

    public void starter(){ 
     Thread running=new Thread(this); 
     running.start(); 
     x=rnd.nextInt(200); 
     y=rnd.nextInt(200); 
    } 
    public void run(){ 
     while(true){ 
     try { 
      Thread.sleep(rnd.nextInt(2000)); 
     } catch (Exception ex) {ex.printStackTrace();} 
     } 
    } 
} 
+0

ここで達成しようとしていることがわからない場合は、どのスレッドがonDrawメソッドを呼び出しているかを特定することが必要な場合は、Thread.currentThread()。getName()を使用できるはずです。スレッドは何をしているのかのデータを保持していないので、スレッドの値を格納するためにThreadlocalクラスを使って探索する必要があることに留意してください。スレッドのクラスのremoveメソッドを呼び出す必要があります。 。あなたが達成しようとしていることの詳細をさらに提供すれば、もう少しお手伝いできるかもしれません。 – XecP277

答えて

-1
Thread.currentThread().get Name(); 

- この方法は、あなたの現在実行中のスレッドオブジェクトの名前を付けます。これがあなたの望むものだと私は信じています。

+0

私はThread.currentThread()。getName()。getBlink()を試しましたが、それでも動作しません。 – banvan

+0

を試してみてください.... Thread.currentThread()。getBlink()..... getName()の部分は名前を取得するだけでした –

+0

それも試しましたが、まだ成功していません。 – banvan