View animation

刘阳荣
2023-12-01

View animation.

有两种动画.一种是Tween.另一种是Frame, 

Tween animation 是对一张图片的Scale伸缩,Rotate旋转,Translate移动,Alpha透明度变化.

Fame animation 是按顺序播放一组图片.


Tween Example:

XML file saved at res/anim/hyperspace_jump.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="false">
	<scale android:interpolator="@android:anim/accelerate_decelerate_interpolator"
	    android:fromXScale="1.0"
	    android:toXScale="1.4"
	    android:fromYScale="1.0"
	    android:toYScale="0.6"
	    android:pivotX="50%"
	    android:pivotY="50%"
	    android:duration="700"/>
    <set android:interpolator="@android:anim/accelerate_interpolator"
        android:startOffset="700">
        <scale android:fromXScale="1.4"
            android:toXScale="0.0"
            android:fromYScale="0.6"
            android:toYScale="0.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="400"
            />
        <rotate android:fromDegrees="0"
            android:toDegrees="-45"
            android:pivotX="50%"
            android:pivotY="50%"
            android:duration="400"/>
    </set>

</set>
调用代码
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ImageView image = (ImageView)findViewById(R.id.imageView1);
        
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
        image.startAnimation(animation);
	}


Frame Example:

XML file saved at res/anim/rocket.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:apk="http://schemas.android.com/apk/res/android" apk:oneshot="false">
    <item apk:drawable="@drawable/img1" apk:duration="500" />
    <item apk:drawable="@drawable/img2" apk:duration="500" />
    <item apk:drawable="@drawable/img3" apk:duration="500" />
</animation-list>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="@drawable/img1"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

调用时.

public class AnimationActivity extends Activity {
	
	private AnimationDrawable rocketAnimation;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        ImageView image = (ImageView)findViewById(R.id.imageView1);
        
        image.setBackgroundResource(R.anim.rocket);
        rocketAnimation = (AnimationDrawable)image.getBackground();
        Button btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				rocketAnimation.start();  //这里不知道为什么一定要用按钮来触发.如果onCreate里start一直不行
				
			}
		});
    }
}



 类似资料:

相关阅读

相关文章

相关问答