迷途奔跑的野猪

来源:互联网 发布:gis地图数据购买 编辑:程序博客网 时间:2024/04/28 22:26

1、res文件夹下创建anim目录

a、创建名称为motionright.xml的xml资源文件

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/pig1" android:duration="40" />    <item android:drawable="@drawable/pig2" android:duration="40" /></animation-list>

b、创建名称为motionleft.xml的xml资源文件

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:drawable="@drawable/pig3" android:duration="40" />    <item android:drawable="@drawable/pig4" android:duration="40" /></animation-list>

c、创建名称为tramslateright.xml的xml资源文件

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translate     android:fromXDelta="0"    android:toXDelta="850"    android:fromYDelta="0"    android:toYDelta="0"    android:duration="3000"></translate> </set>

d、创建名称为tramslateleft.xml的xml资源文件

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><translate     android:fromXDelta="850"    android:toXDelta="0"    android:fromYDelta="0"    android:toYDelta="0"    android:duration="3000"></translate> </set>


2、布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/linearLayout1"android:background="@drawable/background"       android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@anim/motionright"        android:layout_marginTop="280px"        android:layout_marginLeft="30px"/></LinearLayout>

3、onCreate方法中

public class MainActivity extends Activity {private AnimationDrawable anim;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final ImageView iv = (ImageView)findViewById(R.id.imageView1);//获取要应用动画效果的ImageView        //获取向右奔跑的动画资源        final Animation  translateright = AnimationUtils.loadAnimation(this, R.anim.translateright);        //获取向左奔跑的动画资源        final Animation  translateleft = AnimationUtils.loadAnimation(this, R.anim.translateleft);        anim = (AnimationDrawable)iv.getBackground();//获取应用的帧动画        LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);//获取线性布局管理器        Toast.makeText(this, "触摸屏幕开始播放....", Toast.LENGTH_SHORT).show();        ll.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {anim.start();//开始播放帧动画iv.startAnimation(translateright);//播放香油泵奔跑的动画return false;}});        translateright.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {iv.setBackgroundResource(R.anim.motionleft);//重新设置ImageView应用的帧动画iv.startAnimation(translateleft);//播放向左奔跑的动画anim = (AnimationDrawable)iv.getBackground();//获取应用的帧动画anim.start();//开始播放帧动画}});        translateleft.setAnimationListener(new AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {iv.setBackgroundResource(R.anim.motionright);//重新设置ImageView应用的帧动画iv.startAnimation(translateright);//播放向右奔跑的动画anim = (AnimationDrawable)iv.getBackground();//获取应用的帧动画anim.start();//开始播放帧动画}});    }}



0 0
原创粉丝点击