Tween动画和Frame动画

Tween动画用于操作控件
Frame动画来做作为图片的背景

动画可以以xml形式,也可以代码形式编写

首先Tween动画以xml形式加载

在res下新建anim文件夹,新建alpha.xml  scale.xml  translate.xml  rotate.xml   注意是以Tween-Animal的形式

里面为动画的属性  注意自己加上xmlns:android=http://schemas.android.com/apk/res/android这个标签

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="3000">
</alpha>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:toXScale="2"
android:fromYScale="1"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000">
</scale>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="150"
android:fromYDelta="0"
android:toYDelta="300"
android:duration="3000">
</translate>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000">
</rotate>

如果想让这些动画同时执行,可以新建一个xml文件,把这些属性放在一起,这里新建set.xml文件 注意set节点也要加上xmlns:android="http://schemas.android.com/apk/res/android"

<set xmlns:android="http://schemas.android.com/apk/res/android"
>

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1"
android:toAlpha="0"
android:duration="3000">
</alpha>

<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1"
android:toXScale="2"
android:fromYScale="1"
android:toYScale="0.5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000">
</scale>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="150"
android:fromYDelta="0"
android:toYDelta="300"
android:duration="3000">
</translate>

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000">
</rotate>
</set>


 

public class MainActivity extends Activity {
    private ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        iv = (ImageView) findViewById(R.id.iv);
    }
    
    public void start(View v){
    	//加载动画资源 这是以加载xml的形式
           // Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);//渐变动画 1为完全显示  0为完全不见
           //Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);//拉缩动画
           // Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);//平移动画
          // Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);//旋转动画
          // Animation animation = AnimationUtils.loadAnimation(this, R.anim.set);//动画综合起来用,这里吧上面四种效果定义到set.xml中 
    	//iv.startAnimation(animation);
		//通过代码的形式
    	ScaleAnimation animation = new ScaleAnimation(1, 2, 1, 2);
    	animation.setDuration(500);
    	
    	//保持动画之后的状态
    	animation.setFillAfter(true);
    	
    	//让图片执行动画
    	iv.startAnimation(animation);
    }
}


这里Frame动画实现一组图片的播放

<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/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start"
        android:text="开始" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="stop"
        android:text="结束" />
    </LinearLayout>

</LinearLayout>

新建drawable文件夹,里面存放图片,并且在里面新建anim_list.xml文件

在anim_list.xml文件用animation-list存放图片并声明属性

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

	
    <item android:drawable="@drawable/a1" android:duration="200" />
	<item android:drawable="@drawable/a2" android:duration="200" />
	<item android:drawable="@drawable/a3" android:duration="200" />
	<item android:drawable="@drawable/a4" android:duration="200" />
	<item android:drawable="@drawable/a5" android:duration="200" />
	<item android:drawable="@drawable/a6" android:duration="200" />
	<item android:drawable="@drawable/a7" android:duration="200" />
	<item android:drawable="@drawable/a8" android:duration="200" />
	<item android:drawable="@drawable/a9" android:duration="200" />
	<item android:drawable="@drawable/a10" android:duration="200" />
	<item android:drawable="@drawable/a11" android:duration="200" />
	<item android:drawable="@drawable/a12" android:duration="200" />
	<item android:drawable="@drawable/a13" android:duration="200" />
	<item android:drawable="@drawable/a14" android:duration="200" />
	<item android:drawable="@drawable/a15" android:duration="200" />

</animation-list>

在activity里先给ImageView 设置图片iv.setBackgroundResource(R.drawable.anim_list);

在通过AnimationDrawable这个类得到动画图片,并调用它的start()和stop()方法播放图片

public class MainActivity extends Activity {
    private ImageView iv;
	private AnimationDrawable ad;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        iv = (ImageView) findViewById(R.id.iv);
        iv.setBackgroundResource(R.drawable.anim_list);
        //得到动画图片
        ad = (AnimationDrawable) iv.getBackground();
    }
    
    public void start(View v){
    	ad.start();//调用Frame动画的start()方法
    }
    public void stop(View v){
    	ad.stop();
    }
}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值