0

I am creating a game and I have assigned three image views of (balls) at the top of my screen and code an animation for them. The balls are going down to the bottom from the top, but I want the balls from the top of the screen to not show at the start of the animation... any advice?

Here is the relevant Activity code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ball1 = (ImageView) findViewById(R.id.ball1);
        ball2 = (ImageView) findViewById(R.id.ball2);
        ball3 = (ImageView) findViewById(R.id.ball3);  

    }

private void startBallAnimation() {
    final TranslateAnimation animation = new TranslateAnimation(
            TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0.0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0.86f
    );

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            mhandle.post(new Runnable() {
                @Override
                public void run() {
                    setBallColors();
                }
            });
        }

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(final Animation animation) {}

    }
}

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context="com.example.hp.colorspoof.MainActivity">

    <!-- ball imageviews -->
    <ImageView
        android:id="@+id/ball1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="left"
        android:layout_marginLeft="40dp"
        app:srcCompat="@drawable/ball_0" />

    <ImageView
        android:id="@+id/ball2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/row2_2" />

    <ImageView
        android:id="@+id/ball3"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_gravity="right"
        android:layout_marginRight="40dp"
        app:srcCompat="@drawable/row3_1" />
</FrameLayout>
Joey Harwood
  • 961
  • 1
  • 17
  • 27
  • It's not clear from your question how you want the balls to appear. Should they pop into existence, should they come from outside the frame? Something else? – Joey Harwood Feb 15 '18 at 16:25
  • I want them to come outside of the top frame sir @JoeyHarwood – francis Escolar Feb 15 '18 at 16:27
  • Why did you undo my edit? This isn't javascript, code snippets don't work. – Joey Harwood Feb 15 '18 at 16:33
  • Anyway it looks like you're defining the balls in xml. So you should just move the balls above the frame with positioning done relative to parent. If your animation is a set distance you may need to extend its distance to account for the fact that the ball is higher now. – Joey Harwood Feb 15 '18 at 16:38
  • oh did I? I'm sorry I just added some code. I get the logic sir but I don't know how to do that in my code.currently this is my xml for the one ball – francis Escolar Feb 15 '18 at 16:43
  • You should remove the snippet part. That's only for javascript. Just indenting by 4 spaces formats as code. You can see a preview before you submit your edit. You should include your full XML for the scene in the question. If I'm going to correct your code I need to see the parent layouts as well. It's really hard to read code in a comment since you can't include formatting. – Joey Harwood Feb 15 '18 at 17:22
  • Actually, it looks like Kling Klang removed the snippet again while I was writing that comment, so just don't add it back in – Joey Harwood Feb 15 '18 at 17:23
  • i edited it now sir @JoeyHarwood – francis Escolar Feb 15 '18 at 17:28
  • I apologize, I'm not doing well being clear with how to edit your question. I'm going to fix your question, if you could kindly accept the edit right away before it gets rejected for changing your meaning I'd much appreciate it. It'll be a couple minutes. – Joey Harwood Feb 15 '18 at 17:37
  • okay sir @JoeyHarwood – francis Escolar Feb 15 '18 at 17:42
  • Okay I edited it, please note that there's no box that says "Run Code Snippet". That only works with Javascript and when you include it with other code it implies that your code should run, and could confuse users. I'll come back with an answer in a little while if no one else does, but I don't have the time right this moment. – Joey Harwood Feb 15 '18 at 17:45
  • Okay sir @JoeyHarwood thanks! – francis Escolar Feb 15 '18 at 17:46

1 Answers1

0

Below is a simple untested implementation of what you want to do. I unfortunately no longer have an environment for programming Android setup, so I can't test the code for you, but this should work.

Please note that I made use of negative margins because that is the easiest way to implement it, but it isn't necessarily the best way to achieve it. See here for more: Is it a bad practice to use negative margins in Android?

To provide a summary of the changes in the XML below from what you provided:

  1. I provided a RelativeLayout to contain your playfield. It's generally a good practice to include a FrameLayout at the top level and then use a RelativeLayout or LinearLayout below it to contain the views that will need to be manipulated. RelativeLayout and LinearLayout provide good means of placing views in relation to each other. There is also a newer type of Layout called the ConstraintLayout that I'm not personally familiar with but you may want to look at instead of RelativeLayout. See more here: What are the differences between LinearLayout, RelativeLayout, and AbsoluteLayout?
  2. I removed the android:layout_gravity statements. You don't need those with a RelativeLayout.
  3. I utilized android:layout_alignParentTop and android:layout_marginTop to place the balls above the top of your layout by exactly the size of the ball.
  4. I utilized android:layout_marginLeft, android:layout_centerHorizontal, and android:layout_marginRight to place the balls horizontally on the screen as you did.

XML below:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    tools:context="com.example.hp.colorspoof.MainActivity">

    <RelativeLayout
        android:id="@+id/play_area"
        android:layout_width="match_parent"
        android:layout_height"match_parent">

        <!-- ball imageviews -->
        <ImageView
            android:id="@+id/ball1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="-30dp"
            android:layout_marginLeft="40dp"
            app:srcCompat="@drawable/ball_0" />

        <ImageView
            android:id="@+id/ball2"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="-30dp"
            app:srcCompat="@drawable/row2_2" />

        <ImageView
            android:id="@+id/ball3"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginTop="-30dp"
            android:layout_marginRight="40dp"
            app:srcCompat="@drawable/row3_1" />

    </RelativeLayout>

</FrameLayout>
Joey Harwood
  • 961
  • 1
  • 17
  • 27