Chapter 32 Property Animation
Simple Property Animation
Now that you have the scene set up, it is time to make it do your bidding by moving parts of it around.
You are going to animate the sun down below the horizon.
But before you start animating, you will want a few bits of information handy in your fragment. Inside
of onCreateView(...), pull out a couple of views into fields on SunsetFragment.
Listing 32.6 Pulling out view references (SunsetFragment.java)
public class SunsetFragment extends Fragment {
private View mSceneView;
private View mSunView;
private View mSkyView;
public static SunsetFragment newInstance() {
return new SunsetFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sunset, container, false);
mSceneView = view;
mSunView = view.findViewById(R.id.sun);
mSkyView = view.findViewById(R.id.sky);
return view;
}
}
Now that you have those, you can write your code to animate the sun. Here is the plan: Smoothly move
mSunView so that its top is right at the edge of the top of the sea. You will do this by translating the
location of the top of mSunView to the bottom of its parent.
The first step is to find where the animation should start and end. Write this first step in a new method
called startAnimation().
Listing 32.7 Getting tops of views (SunsetFragment.java)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
}
private void startAnimation() {
float sunYStart = mSunView.getTop();
float sunYEnd = mSkyView.getHeight();
}