Android Media and Animation 8

กด walk เดินหน้า ถอยหลัง
กด stop หยุด กด walk เดินต่อ

MainActivity.java
package com.example.myapp4;

import android.os.Bundle;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btWalk=(Button)findViewById(R.id.button1);
btWalk.setOnClickListener(this);
Button btStop=(Button)findViewById(R.id.button2);
btStop.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
AnimationDrawable ad;

ImageView iv;
AnimatorSet as;
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
iv=(ImageView)findViewById(R.id.imageView1);
iv.setBackgroundResource(R.drawable.cartoon);
ad=(AnimationDrawable)iv.getBackground();
ad.start();
//Animation a=AnimationUtils.loadAnimation(this, R.anim.walk);
as =(AnimatorSet)AnimatorInflater.loadAnimator(this, R.animator.walk2);
as.setTarget(iv);
as.start();
//iv.startAnimation(a);
break;
case R.id.button2:
ad.stop();
as.cancel();
//iv.clearAnimation();
break;
}
}
}
--------------------------------------
Walk2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially"
>
<objectAnimator
android:duration="5000"
android:propertyName="x"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:valueTo="400"
android:valueType="floatType"/>
</set>
-------------------------------------
Walk.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate android:fromXDelta="0" android:toXDelta="300"
android:repeatCount="infinite"
android:duration="7000"
android:interpolator="@android:anim/linear_interpolator"
/>
</set>
----------------------------------
Cartoon.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/c1" android:duration="200"/>
<item android:drawable="@drawable/c2" android:duration="200"/>
<item android:drawable="@drawable/c3" android:duration="200"/>
<item android:drawable="@drawable/c4" android:duration="200"/>
<item android:drawable="@drawable/c5" android:duration="200"/>
<item android:drawable="@drawable/c6" android:duration="200"/>

</animation-list>
----------------------------------

Picture 2014-03-08 15_28_59 Picture 2014-03-08 15_29_12 Picture 2014-03-08 15_29_23 Picture 2014-03-08 15_29_35 Picture 2014-03-08 15_29_48