View animation

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一直不行
				
			}
		});
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值