Android学习笔记(26) --- 动画Animation使用

最近做个小应用需要用到动画效果,组员已经实现了效果,拿过来修改下。

原理:动画看起来的效果是图片在移动,而且几张图片不停的在切换。

实现过程:

1、在res目录下创建anim文件夹,创建myfish.xml,把几张图片的切换动画设置好:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false" >

    <item
        android:drawable="@drawable/xiaoyu2"
        android:duration="300"/>
    <item
        android:drawable="@drawable/xiaoyu0"
        android:duration="300"/>
    <item
        android:drawable="@drawable/xiaoyu1"
        android:duration="300"/>

</animation-list>

2、在main.xml中,给予myfish一个ID

   <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageButton
            android:id="@+id/myjellyfish"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_centerVertical="true"/>

        <ImageButton
            android:id="@+id/myfish"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>

3、在java文件中调用:

	private ImageButton mFish;//鱼仔
添加移动动画效果
	public void Fish_StartAnimation() {
		// ////////////////*********小鱼游动**************///////////////////////
		mFish = (ImageButton) findViewById(R.id.myfish);
		AnimationDrawable fish_anim = new AnimationDrawable();
		mFish.setBackgroundResource(R.anim.my_fish);
		fish_anim = (AnimationDrawable) mFish.getBackground();
		fish_anim.start();
		AnimationSet fish_animset = new AnimationSet(true);
		TranslateAnimation swim_fishTr = new TranslateAnimation(300.0f,
				-850.0f, 0.0f, 0.0f);
		swim_fishTr.setDuration(30000);
		swim_fishTr.setRepeatCount(Animation.INFINITE);
		fish_animset.addAnimation(swim_fishTr);
		mFish.startAnimation(fish_animset);
	}

在需要调用动画的地方调用Fish_StartAnimation()函数

		Fish_StartAnimation();

这样便实现了一边移动一边切换图片的动画效果。


另:关于 TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

里面的参数要看你硬件屏幕的分辨率了,代表屏幕中心是:X轴:0.0f,Y轴:0.0f


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章