2014-10-07 60 views
-1

我不能使用此代碼移動自定義視圖: Ball.java移動查看與主題

public class Ball extends View { 
int x, y; 
public Ball(Context context) { 
    super(context); 
} 

public void setSizes(int x, int y) { 
    this.x = x; 
    this.y = y; 
} 

protected void onDraw(Canvas c) { 
    Paint paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    int radius = 50; 
    c.drawCircle(x,y,radius,paint); 
} 

}

BounceLoop.java

public class BounceLoop extends Thread { 
int width, height, x, y; 
boolean jumping = false; 

public void setSizes(int width, int height) { 
    this.width = width; 
    this.height = height; 
} 
public void run() { 
    jumping = true; 
    x = 0; 
    y = 0; 
    while(jumping) { 

    } 
} 

}

和MyActivity .java

public class MyActivity extends Activity { 
RelativeLayout content; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    content = (RelativeLayout) findViewById(R.id.content); 
    BounceLoop thread = new BounceLoop(); 
    thread.setSizes(content.getWidth(), content.getHeight()); 
    thread.start(); 
} 



@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.my, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

}

如何從BounceLoop線移動球?我不想用AsyncTask來做,我必須使用處理程序還是?

+0

您無法從非UI線程移動UI元素 – tyczj 2014-10-07 13:26:17

回答

0

你的線程可能需要您的自定義視圖()我本來期望像

的引用,在運行一些
public void run() { 
    jumping = true; 
    x = 0; 
    y = 0; 
    while(jumping) { 
     mCircleView.setPosition(newX, newY); 
     mCircleView.postInvalidate(); 
    } 
} 

注意,你不能碰的UI通過另一個線程,這是我叫postInvalidate()而不是invalidate()。前者將在UI線程

+0

是的,但是如果在佈局中未添加球,只會從遊戲循環線程安裝?我的意思是我沒有在XML文件中的球,我需要它嗎? – 2014-10-07 13:40:45

+0

'content.addView(new Ball(this))',在onCreate後,setContentView – Blackbelt 2014-10-07 13:45:13

+0

太好了,謝謝!最後一個問題,mCircleView.setPosition在線程中,但mCircleView被添加到活動中,我如何進行連接? – 2014-10-07 13:49:48

0

最後重新安排繪製事件我這樣做:

public class MyActivity extends Activity { 
RelativeLayout content; 
Handler myHandler; 
Ball view; 
Thread loop; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 
    content = (RelativeLayout) findViewById(R.id.content); 
    view = new Ball(this); 
    view.setPosition(50, 50); 

    content.addView(view); 

    myHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      int x = (int) msg.arg1; 
      int y = (int) msg.arg2; 
      Log.d("ss", String.valueOf(x)); 
      view.setPosition(x, y); 
     } 
    }; 


} 

public void onWindowFocusChanged(boolean hasChanged) { 
    super.onWindowFocusChanged(hasChanged); 
    loop = new Thread(new Runnable() { 
     @Override 
     public void run() { 
      int x = 0; 
      int y = 0; 
      Log.d("width", String.valueOf(view.getWidth())); 
      while (x < content.getWidth() - view.radius) { 
       x += 5; 
       y += 7; 
       Message msg = new Message(); 
       msg.arg1 = x; 
       msg.arg2 = y; 
       myHandler.sendMessage(msg); 
       try { 
        Thread.sleep(25); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    }); 
    loop.start(); 
} 

}

但這不舒服,讓我有在活動線程,我怎麼能將它導出到另一個類...