如何添加按钮随机位置和动画?

时间:2016-12-20 12:07:13

标签: java android android-layout android-fragments

我想在这个video中实现动画。

我从API类别获得。并为每个类别添加按钮

DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        int width = displaymetrics.widthPixels;
        int height = displaymetrics.heightPixels;
        Random r = new Random();

        for(Map.Entry<String, String> entry : data.entrySet()){

            Button circleBtn = new Button(getActivity());
            circleBtn.setLayoutParams(params);

            circleBtn.setY(r.nextInt(width));
            circleBtn.setX(r.nextInt(height));

            circleBtn.setTextSize(6f);
            circleBtn.setText(entry.getValue());
            circleBtn.setBackgroundResource(R.drawable.round_shape_layout);

            categoryContainer.addView(circleBtn);
        }

结果如下图所示 enter image description here

如何在添加新按钮位置之前检查是免费的?

P.s抱歉这么差的英语。

更新===========&GT; 我更新了一些代码

Set<Integer> yPos = new HashSet<>();
    Set<Integer> xPos = new HashSet<>();

    Random r = new Random();

    for (Map.Entry<String, String> entry : data.entrySet()) {

        Integer randY = r.nextInt(width);
        Integer randX = r.nextInt(height);
        Button circleBtn = new Button(getActivity());
        //circleBtn.setLayoutParams(params);

        if(!yPos.contains(randY) && !xPos.contains(randX)) {
            circleBtn.setY(randY);
            circleBtn.setX(randX);
            yPos.add(randY);
            xPos.add(randX);
        }else{
            boolean isFound = false;
            while (!isFound){
                randY = r.nextInt(width);
                randX = r.nextInt(height);
                if(!yPos.contains(randY) && !xPos.contains(randX)){
                    circleBtn.setY(randY);
                    circleBtn.setX(randX);
                    yPos.add(randY);
                    xPos.add(randX);
                    isFound = true;
                }
            }
        }

        circleBtn.setTextSize(6f);
        circleBtn.setText(entry.getValue());
        circleBtn.setBackgroundResource(R.drawable.round_shape_layout);

        categoryContainer.addView(circleBtn, params);
    }

结果如下图所示 enter image description here

X,Y位置输出如下:

D/yPOS: 155
D/yPOS: 344
D/yPOS: 248
D/yPOS: 43
D/yPOS: 353
D/yPOS: 108
D/yPOS: 0
D/yPOS: 183
D/yPOS: 99
D/yPOS: 116
D/yPOS: 330
D/yPOS: 141
D/yPOS: 234
D/yPOS: 304
D/yPOS: 42
D/yPOS: 289
D/yPOS: 305
D/yPOS: 140

D/xPOS: 520
D/xPOS: 67
D/xPOS: 147
D/xPOS: 243
D/xPOS: 416
D/xPOS: 496
D/xPOS: 317
D/xPOS: 485
D/xPOS: 447
D/xPOS: 541
D/xPOS: 362
D/xPOS: 397
D/xPOS: 296
D/xPOS: 558
D/xPOS: 157
D/xPOS: 319
D/xPOS: 358
D/xPOS: 150

现在我没有合并位置,但circleButton仍然合并。 如何用半径计算位置?或者任何想法?

0 个答案:

没有答案