android - Why is Thread.sleep not working -
i new android , java , having trouble using thread.sleep function.my program simple game that's table tennis 2 players. code not complete thread not working properly.i changing margins move ball.is catch block being run because of exception > code follows :
package com.example.game; import android.app.activity; import android.app.activity; import android.content.context; import android.graphics.point; import android.os.bundle; import android.view.display; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.linearlayout; import android.widget.relativelayout; import android.widget.toast; public class mainactivity extends activity { private int x1=0,x2=0,m=150; public int height; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); display display = getwindowmanager().getdefaultdisplay(); point size= new point(); display.getsize(size); // int width = size.x; height = size.y; showview2(); showview1(); game(); } public void game() { buttonsetup1_rig(); buttonsetup1_lef(); buttonsetup2_rig(); buttonsetup2_lef(); //while(m>=0) for(int i=0;i<150;i++) { showball(); new thread(new runnable() { @override public void run() { try { thread.sleep(500); m=m-10; } catch (interruptedexception e) { e.printstacktrace(); } } }).start(); } } protected void buttonsetup1_rig() { imagebutton ticon=(imagebutton)findviewbyid(r.id.imagebutton2); ticon.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub if(x1<(height-320)) x1=x1+10; showview1(); } }); } protected void buttonsetup1_lef() { imagebutton ticon=(imagebutton)findviewbyid(r.id.imagebutton1); ticon.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub if(x1>(0)) x1=x1-10; showview1(); } }); } protected void buttonsetup2_rig() { imagebutton ticon=(imagebutton)findviewbyid(r.id.imagebutton6); ticon.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub if(x2<(height-320)) x2=x2+10; showview2(); } }); } protected void buttonsetup2_lef() { imagebutton ticon=(imagebutton)findviewbyid(r.id.imagebutton4); ticon.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub if(x2>(0)) x2=x2-10; showview2(); } }); } public void showview1() { imageview image=(imageview)findviewbyid(r.id.imageview1); linearlayout.layoutparams lp ; lp=(linearlayout.layoutparams)(image.getlayoutparams()); lp.setmargins(x1,0,0,0); image.setlayoutparams(lp); } public void showview2() { imageview image=(imageview)findviewbyid(r.id.imageview2); linearlayout.layoutparams lp ; lp=(linearlayout.layoutparams)(image.getlayoutparams()); lp.setmargins(x2,0,0,0); image.setlayoutparams(lp); } public void showball() { imageview img=(imageview)findviewbyid(r.id.imageview3); linearlayout.layoutparams l ; l=(linearlayout.layoutparams)(img.getlayoutparams()); int leftmargin = l.leftmargin; int rightmargin = l.rightmargin; int topmargin = l.topmargin; l.setmargins(m,0,m,0); img.setlayoutparams(l); }
}
my xml :
<relativelayout android:layout_width="match_parent" android:layout_height="400dp" > <relativelayout android:id="@+id/relativelayout1" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_above="@+id/relativelayout2" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" > <imagebutton android:id="@+id/imagebutton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:src="@drawable/left" /> <imagebutton android:id="@+id/imagebutton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:src="@drawable/ic_launcher" /> <imagebutton android:id="@+id/imagebutton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_centervertical="true" android:src="@drawable/right" /> </relativelayout> <relativelayout android:layout_width="fill_parent" android:layout_height="350dp" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_below="@+id/relativelayout1" > <linearlayout android:id="@+id/linearlayout1" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:orientation="vertical" > <imageview android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/lc" /> </linearlayout> <linearlayout android:id="@+id/linearlayout2" android:layout_width="wrap_content" android:layout_height="280dp" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/linearlayout1" android:orientation="vertical" > <imageview android:id="@+id/imageview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="120dp" android:layout_margintop="140dp" android:src="@drawable/untitled" /> </linearlayout> <linearlayout android:layout_width="wrap_content" android:layout_height="30dp" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@+id/linearlayout2" android:orientation="vertical" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginleft="64dp" android:src="@drawable/lc" /> </linearlayout> </relativelayout> </relativelayout> <relativelayout android:layout_width="match_parent" android:layout_height="49dp" android:layout_weight="2.25" > <imagebutton android:id="@+id/imagebutton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:src="@drawable/left" /> <imagebutton android:id="@+id/imagebutton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/imagebutton1" android:layout_alignparentright="true" android:src="@drawable/right" /> <imagebutton android:id="@+id/imagebutton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:src="@drawable/ic_launcher" /> </relativelayout>
Comments
Post a Comment