1

I'm taking a Coursera class on Android programming. Here's a illustration of what I'm trying to do...

Slide and fade interval

Here's the code I have so far...

XML:

<Button
        android:id="@+id/startbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/leftfoot"
        android:layout_alignRight="@+id/leftfoot"
        android:onClick="startRhythmandAnimation"
        android:text="@string/start_button" />

Java:

public class Assignment3MainActivity extends Activity {

private View mMileTimeGoal;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_assignment3_main);
    mMileTimeGoal = findViewById(R.id.miletimegoal);
}

public void startRhythmandAnimation () {
    String MileTime = mMileTimeGoal.getContext().toString();
    String[] time_array = MileTime.split(":");
    int hours = Integer.parseInt(time_array[0]);
    int minutes = Integer.parseInt(time_array[1]);
    int seconds = Integer.parseInt(time_array[2]);
    int duration = 3600 * hours + 60 * minutes + seconds;
    int steps_per_second = 3;

    int running_rate = duration * steps_per_second;

    View rightfoot = findViewById(R.id.rightfoot);
    View leftfoot = findViewById(R.id.leftfoot);

    rightfoot.setVisibility(View.VISIBLE);
    Animation anim = AnimationUtils.makeInChildBottomAnimation(this);
    rightfoot.startAnimation(anim);

    leftfoot.setVisibility(View.VISIBLE);
    leftfoot.startAnimation(anim);
}

Any ideas on how to form my algorithm which would slide and fade my rightfoot view and leftfoot view?

Should I use a while loop and kick off some type of timer?

StacyM
  • 1,036
  • 5
  • 23
  • 41

1 Answers1

3

Activity

private Handler mHandler;    
private long mInterval = 1000;
private View mLeftfoot;
private Animation mFootAnim;

public void onCreate(Bundle bundle) {
   ...
   mHandler = new Handler(); //.os package class when importing
   mLeftfoot = findViewById(R.id.leftfoot);
   mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot);
   stepRecursive();
}

private void stepRecursive() {
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mLeftFoot.startAnimation(mFootAnim );
            stepRecursive();
        }
    }, mInterval);
}

/res/anim/foot.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromYDelta="0" android:toYDelta="-15" android:duration="400"/>
    <alpha android:fromAlpha="1.0" android:toAlpha="0" android:duration="400" />
</set>

Thats straight off the top of my head (thus untested) but should be plenty to get you going in the right direction

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • Thanks so much! A few questions though...Can I just put the foot.xml content in my /layout/main.xml? Also, is the interval fixed? Do I change mInterval to have it adjust according to the user's Mile Time Goal? – StacyM Feb 10 '14 at 03:09
  • Yes adjust interval for your time goal. No, you can't merge the XML, the reason there are different folders in the res directory is they serve different purposes – Nick Cardoso Feb 10 '14 at 07:12
  • If you have time, please see my latest Logcat exceptions: http://stackoverflow.com/questions/21671129/android-illegalstateexception-could-not-execute-method-of-the-activity-cause – StacyM Feb 10 '14 at 07:19
  • Could you explain the where the connection is between the foot.xml and the animation code in the java file? – StacyM Feb 10 '14 at 19:32
  • 1
    This line created an animation with the properties in foot.xml: mFootAnim = AnimationUtils.loadAnimation(this, R.anim.foot); – Nick Cardoso Feb 10 '14 at 20:30