java - How can I randomly place ImageButtons and prevent them from overlapping? -
i want create array imagebutton prevent overlapping of objects , want add after params.height. question how that?
here code far:
public class play extends activity implements onclicklistener { final context context = this; mediaplayer mplayer; mediaplayer mplplayer1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_play); text = (textview) this.findviewbyid(r.id.textview2); countdowntimer = new mycountdowntimer(starttime, interval); text.settext(text.gettext() + string.valueof(starttime / 1000)); relativelayout rl = (relativelayout) findviewbyid(r.id.relativelayout1); rl.setbackgroundresource(r.drawable.background); /* * for-loop creates imagebutton , place them in random * position problem overlapping how * prevent it? */ (int = 0; < 10; i++) { imagebutton btn = new imagebutton(this); btn.setbackgroundresource(r.drawable.apple); relativelayout.layoutparams params = new relativelayout.layoutparams( layoutparams.wrap_content, layoutparams.wrap_content); btn.setlayoutparams(params); params.width = 83; params.height = 80; int left = (int) (math.random() * 1024 + 83); int right = (int) (math.random() * 600 + 20); if (left == 600 & right == 600) { params.setmargins(left, 10 + 2, right, 0); } else params.setmargins(left, 50 - 1, right, 0); btn.setlayoutparams(params); rl.addview(btn); btn.setid(i); btn.setonclicklistener(this); } } }
int left = (int) (math.floor((math.random()*1024)/83.0) * 83 + 83); int right = (int) (math.floor((math.random()*600)/80.0)*80 + 20);
that separate each random number width , height of buttons.
Comments
Post a Comment