Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Create a counter in game android
- public class GameView extends View{
- private int count = 200;
- private TimeCountThread timeCountThread;
- public GameView(Context context, AttributeSet attrs) {
- super(context, attrs);
- timeCountThread = new TimeCountThread();
- timeCountThread.start();
- }
- protected void onDraw(Canvas canvas) {
- Paint paint = new Paint();
- paint.setColor(Color.WHITE);
- canvas.drawText("Time :"+count, 10, 35, paint);
- }
- public class TimeCountThread extends Thread{
- public void run(){
- while(count > 0){
- try {
- sleep(1000);
- count--;
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public boolean onTouchEvent(MotionEvent event){
- if(event.getAction()==MotionEvent.ACTION_DOWN){
- int x1=(int) event.getX();
- int y1=(int) event.getY();
- Toast.makeText(getContext(), "x1 = "+x1+", y1 ="+y1,1).show();
- }
- return true;
- }
- }
- I want creat a counter .It begin from 200 and then 1s reduced about 0
- Handler handler=new Handler();
- Runnable r=new Runnable() {
- public void run() {
- count--;
- if(count > 0) {
- handler.postDelayed(this, 1000);
- }
- }
- };
- if(count > 0) {
- handler.postDelayed(r, 1000);
- }
- timeCountThread = new TimeCountThread();
- timeCountThread.start();
Advertisement
Add Comment
Please, Sign In to add comment