/ / ImageView Arrayを画面に残すようにしようとする - アンドロイド

ImageView Arrayを画面上に残そうとしています - アンドロイド

私は10を作成したい ImageViews ランダムな位置にあり、私はそれらを同じにしたい、私は ImageView[]、私は画面のサイズ(幅と高さ)を取得し、それぞれのランダムな場所を作成 ImageView (画面サイズの範囲内)。いくつかの ImageViews 時には7,8 ImageViews 私は見えます。ここに私のコード:

 ImageView ball[], you;
int layoutwidth, layoutheight;
Random rand = new Random();
RelativeLayout rlt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlt = (RelativeLayout) findViewById(R.id.rlt);
you = (ImageView) findViewById(R.id.imageView);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
layoutwidth = size.x;
layoutheight = size.y;
ball = new ImageView[11];
for (int i = 0; i < 10; i++) {
ball[i] = new ImageView(this);
ball[i].setTag(i);
ball[i].setBackgroundResource(R.mipmap.ball);
int randomx = rand.nextInt(layoutwidth+1);
int randomy = rand.nextInt(layoutheight+1);
ball[i].setX(randomx);
ball[i].setY(randomy);
rlt.addView(ball[i]);
}
}

コードを必要に応じてどのように動作させることができますか?

回答:

回答№1は0

あなたはこれを交換しようとすることができます

int randomx = rand.nextInt(layoutwidth+1);
int randomy = rand.nextInt(layoutheight+1);
ball[i].setX(randomx);
ball[i].setY(randomy);
rlt.addView(ball[i]);

〜と

int randomx = rand.nextInt(layoutwidth+1-widthOfImageView);
int randomy = rand.nextInt(layoutheight+1-heightOfImageView);
rlt.addView(ball[i]);
ball[i].setTranslationX(randomx);
ball[i].setTranslationY(randomy);

どこで widthOfImageView、heightOfImageView 画像ビューの幅と高さです