Android:将视图关闭屏幕

时间:2015-06-13 01:06:42

标签: android

所以我试图将一个视图放在屏幕上,并让它(通过translateAnimation)滑过屏幕。我让它从顶部和左侧滑入。但由于某种原因,从右侧或底部滑入并不适合我。以下是代码段:

   final ImageView animatedSquare = new ImageView(this);

    //Creates a random number to choose color
    Random rand = new Random();
    //0 = Red, 1 = blue, 2 = green, 3 = gray
    int whichColor = rand.nextInt(4);



    //sets animatedSquare to the right color
    switch (whichColor) {
        case 0:
            animatedSquare.setImageResource(R.drawable.redsquare);
            break;
        case 1:
            animatedSquare.setImageResource(R.drawable.bluesquare);
            break;
        case 2:
            animatedSquare.setImageResource(R.drawable.greensquare);
            break;
        case 3:
            animatedSquare.setImageResource(R.drawable.graysquare);
            break;
    }

    final int animatedSquareSize = 150;


    RelativeLayout rl = (RelativeLayout) findViewById(R.id.homeScreen);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(animatedSquareSize, animatedSquareSize);

    //Chooses which side of the screen to come from
    //0 = top, 1 = right, 2 = bottom, 3 = left
    int whichSide = rand.nextInt(4);

    //Gets width and height of screen
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;

    switch (whichSide){
        case 0: {
            int xPos = rand.nextInt(width);
            params.leftMargin = xPos;
            params.topMargin = -animatedSquareSize;
            rl.addView(animatedSquare, params);

            TranslateAnimation squareAnimationTop = new TranslateAnimation(0, rand.nextInt(width * 2) - (float)(0.5 * width), 0, height + animatedSquareSize);
            squareAnimationTop.setDuration(5000);
            squareAnimationTop.setFillEnabled(true);
            squareAnimationTop.setFillBefore(true);
            animatedSquare.startAnimation(squareAnimationTop);
        }


            break;
        case 1: {
            int yPos = rand.nextInt(height);
            params.leftMargin = width;
            params.topMargin = yPos;
            rl.addView(animatedSquare, params);

            TranslateAnimation squareAnimationRight = new TranslateAnimation(0, -(width + animatedSquareSize), 0, rand.nextInt(height) - yPos);
            squareAnimationRight.setDuration(5000);
            squareAnimationRight.setFillEnabled(true);
            squareAnimationRight.setFillBefore(true);
            animatedSquare.startAnimation(squareAnimationRight);
        }
            break;
        case 2: {
            int xPos = rand.nextInt(width);
            params.leftMargin = xPos;
            params.topMargin = height;
            rl.addView(animatedSquare, params);

            TranslateAnimation squareAnimationTop = new TranslateAnimation(0, rand.nextInt(width * 2) - (float)(width * 0.5), 0, -(height + animatedSquareSize));
            squareAnimationTop.setDuration(5000);
            squareAnimationTop.setFillEnabled(true);
            squareAnimationTop.setFillBefore(true);
            animatedSquare.startAnimation(squareAnimationTop);
        }
            break;
        case 3:{
            int yPos = rand.nextInt(height);
            params.leftMargin = -animatedSquareSize;
            params.topMargin = yPos;
            rl.addView(animatedSquare, params);

            TranslateAnimation squareAnimationRight = new TranslateAnimation(0, width + animatedSquareSize, 0, rand.nextInt(height * 2) - (float)(0.5 * height));
            squareAnimationRight.setDuration(5000);
            squareAnimationRight.setFillEnabled(true);
            squareAnimationRight.setFillBefore(true);
            animatedSquare.startAnimation(squareAnimationRight);
        }
            break;
    }

就像我说的,案例0和案例3完美无缺。但由于某些原因,当案例1或2执行时,没有任何反应。

另外,如果我这样做:

  case 1: {
            int yPos = rand.nextInt(height);
            params.leftMargin = width - 20; //I changed this line- subtracted 20
            params.topMargin = yPos;
            rl.addView(animatedSquare, params);

            TranslateAnimation squareAnimationRight = new TranslateAnimation(0, -(width + animatedSquareSize), 0, rand.nextInt(height) - yPos);
            squareAnimationRight.setDuration(5000);
            squareAnimationRight.setFillEnabled(true);
            squareAnimationRight.setFillBefore(true);
            animatedSquare.startAnimation(squareAnimationRight);
        }

将图像大小调整为20dp,然后完成翻译。就好像我无法在屏幕右侧或下方放置任何东西。

任何帮助将不胜感激!

0 个答案:

没有答案