微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 android studio 中获取一系列视图以显示/隐藏/更改?

如何解决如何在 android studio 中获取一系列视图以显示/隐藏/更改?

当我按下按钮/活动开始时,我希望出现和消失一系列图像视图(4-9 长 - 随机选择),以便一次只显示一个。这样做的目的是在随机生成的网格上突出显示序列中的形状。 问题是,在整个序列完成之前,网格永远不会更新,并且用于突出显示的图像视图永远不会出现。我已经在真实设备上进行了尝试,而不仅仅是在模拟器上,我也遇到了同样的问题。 这是我的代码

public void startSequence(){
    ImageView OverPos0 = findViewById(R.id.overPos0);
    ImageView OverPos1 = findViewById(R.id.overPos1);
    ImageView OverPos2 = findViewById(R.id.overPos2);
    ImageView OverPos3 = findViewById(R.id.overPos3);
    ImageView OverPos4 = findViewById(R.id.overPos4);
    ImageView OverPos5 = findViewById(R.id.overPos5);
    ImageView OverPos6 = findViewById(R.id.overPos6);
    ImageView OverPos7 = findViewById(R.id.overPos7);
    ImageView OverPos8 = findViewById(R.id.overPos8);
    ImageView OverPoss[] = {OverPos0,OverPos1,OverPos2,OverPos3,OverPos4,OverPos5,OverPos6,OverPos7,OverPos8};

    Random random = new Random(System.currentTimeMillis());
    Pass = "";
    String nums = "12345";
    int position = 1000;
    int passLength = random.nextInt(5)+4;
    System.out.println("Pass length = "+ passLength);
    for (int i = 0; i < passLength;i++){
        Pass = Pass + String.valueOf(nums.charat(random.nextInt(nums.length())));
    }
    int currentPos = random.nextInt(9);
    while (Nodes[currentPos].shape != Integer.parseInt(Pass.substring(0,1))){
        currentPos = random.nextInt(9);
        System.out.println("currentPos = "+currentPos+",Nodes[currentPos].shape = "+Nodes[currentPos].shape+",Pass[0] = "+Integer.parseInt(Pass.substring(0,1)));
    }
    //OverPoss[currentPos].setVisibility(View.VISIBLE);
    OverPoss[currentPos].animate().alpha(1f).setDuration(100);
    position = currentPos;
    for (int x = 0; x < passLength; x++){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printstacktrace();
        }
        randomiseShapes(position);
        hidePoss();
        switch (Pass.charat(x)){
            case '1':
                while (Nodes[currentPos].shape != 1 || position == currentPos){
                    currentPos = random.nextInt(5);
                    currentPos +=4;
                    for (int y = 0; y < Nodes[position].canSee.length; y++){
                        if (currentPos == Nodes[position].canSee[y]){
                            y = Nodes[position].canSee.length;
                        }
                    }

                System.out.println("currentPos = "+currentPos+",Pass["+x+"] = "+Integer.parseInt(Pass.substring(0,1))+",Nodes[position].canSee = "+ Arrays.toString(canSees[position]));
                }
                //OverPoss[position].setVisibility(View.INVISIBLE);
                OverPoss[position].animate().alpha(0f).setDuration(100);
                OverPoss[currentPos].animate().alpha(1f).setDuration(100);
                //OverPoss[currentPos].setVisibility(View.VISIBLE);
                position = currentPos;
                break;
            case '2':
                while (Nodes[currentPos].shape != 2 || position == currentPos){
                    currentPos = random.nextInt(9);
                    for (int y = 0; y < Nodes[position].canSee.length; y++){
                        if (currentPos == Nodes[position].canSee[y]){
                            y = Nodes[position].canSee.length;
                        }
                    }

                    System.out.println("currentPos = "+currentPos+",Nodes[position].canSee = "+Arrays.toString(canSees[position]));
                }
                //OverPoss[position].setVisibility(View.INVISIBLE);
                OverPoss[position].animate().alpha(0f).setDuration(100);
                OverPoss[currentPos].animate().alpha(1f).setDuration(100);
                //OverPoss[currentPos].setVisibility(View.VISIBLE);
                position = currentPos;
                break;
            case '3':
                while (Nodes[currentPos].shape != 3 || position == currentPos){
                    currentPos = random.nextInt(9);
                    for (int y = 0; y < Nodes[position].canSee.length; y++){
                        if (currentPos == Nodes[position].canSee[y]){
                            y = Nodes[position].canSee.length;
                        }
                    }

                    System.out.println("currentPos = "+currentPos+",Nodes[position].canSee = "+Arrays.toString(canSees[position]));
                }
                //OverPoss[position].setVisibility(View.INVISIBLE);
                OverPoss[position].animate().alpha(0f).setDuration(100);
                OverPoss[currentPos].animate().alpha(1f).setDuration(100);
                //OverPoss[currentPos].setVisibility(View.VISIBLE);
                position = currentPos;
                break;
            case '4':
                while (Nodes[currentPos].shape != 4 || position == currentPos){
                    currentPos = random.nextInt(9);
                    for (int y = 0; y < Nodes[position].canSee.length; y++){
                        if (currentPos == Nodes[position].canSee[y]){
                            y = Nodes[position].canSee.length;
                        }
                    }

                    System.out.println("currentPos = "+currentPos+",Nodes[position].canSee = "+Arrays.toString(canSees[position]));
                }
                //OverPoss[position].setVisibility(View.INVISIBLE);
                OverPoss[position].animate().alpha(0f).setDuration(100);
                OverPoss[currentPos].animate().alpha(1f).setDuration(100);
                //OverPoss[currentPos].setVisibility(View.VISIBLE);
                position = currentPos;
                break;
            case '5':
                while (Nodes[currentPos].shape != 5 || position == currentPos){
                    currentPos = random.nextInt(9);
                    for (int y = 0; y < Nodes[position].canSee.length; y++){
                        if (currentPos == Nodes[position].canSee[y]){
                            y = Nodes[position].canSee.length;
                        }
                    }

                    System.out.println("currentPos = "+currentPos+",Nodes[position].canSee = "+Arrays.toString(canSees[position]));
                }
                //OverPoss[position].setVisibility(View.INVISIBLE);
                OverPoss[position].animate().alpha(0f).setDuration(100);
                OverPoss[currentPos].animate().alpha(1f).setDuration(100);
                //OverPoss[currentPos].setVisibility(View.VISIBLE);
                position = currentPos;
                break;
        }
    }
    try {
        TimeUnit.SECONDS.sleep(1);
    } catch (InterruptedException e) {
        e.printstacktrace();
    }
    //OverPoss[position].setVisibility(View.INVISIBLE);
    OverPoss[position].animate().alpha(0f).setDuration(100);
    randomiseShapes(1000);
    entering = false;
}

对于糟糕的提问者礼仪,我很抱歉,但我在我的智慧尽头试图解决这个问题,正如你所看到的,我已经尝试使用动画而不是将视图设置为可见或不可见。任何帮助将不胜感激,如果我能提供更多信息,请告诉我。

  • Nodes = 具有序列形状的网格点\n

  • OverPos = 突出显示所选形状的圆圈

  • Nodes.canSee = 其他节点的位置 从这个直线访问

  • Nodes.shape = 5 种形状中每一种的数值 (1-5)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。