Android(开发):动画之Frame动画

Frame动画是指将一系列图片按照一定的顺序进行排列显示。它可以被定义在XML文件中,也可以完全编码实现。本文已定义在XML中为例进行介绍。

       首先在res目录下的drawable文件夹(或新建一个anim文件夹)下,新建XML,将动画资源放在这个文件下。

   <?xml version="1.0" encoding="utf-8"?>
   <animation-list 
    <span style="white-space:pre">	</span>xmlns:android="http://schemas.android.com/apk/res/android" 
    <span style="white-space:pre">	</span>android:oneshot="true">  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p1" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p2" android:duration="100" />  
   <span style="white-space:pre">	</span> <item android:drawable="@drawable/p3" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p4" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p5" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p6" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p7" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p8" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p9" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p10" android:duration="100" />  
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p11" android:duration="100" />
    <span style="white-space:pre">	</span><item android:drawable="@drawable/p12" android:duration="100" />
    </animation-list> 
    接着介绍下本文的布局文件
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_margin="10dp"
<span style="white-space:pre">	</span>android:orientation="vertical"
       tools:context=".MainActivity" >
    <Button   
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/btn_start"  
        android:text="开始"/>  
    <Button   
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:id="@+id/btn_close"  
        android:text="停止"/>  
    <ImageView   
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_gravity="center_vertical"  
        android:id="@+id/iv"/> 
</LinearLayout>
     然后为大家阐述一下,代码中如何加载动画
    public class MainActivity extends Activity implements OnClickListener{
    private Button btn_start,btn_close;
    private ImageView iv;
    private AnimationDrawable attackAnimation;//定义动画的对象   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setOnClickListener();
    }
    private void init() {
        // TODO Auto-generated method stub
        initUI();
    }
    private void initUI() {
        // TODO Auto-generated method stub
       btn_start = (Button) findViewById(R.id.btn_start);
       btn_close = (Button) findViewById(R.id.btn_close);
       iv = (ImageView) findViewById(R.id.iv);
       iv.setBackgroundResource(R.drawable.frame);
       attackAnimation = (AnimationDrawable) iv.getBackground();      
    }
    private void setOnClickListener() {
        // TODO Auto-generated method stub
        btn_start.setOnClickListener(this);
        btn_close.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.btn_start:
                attackAnimation.start();//开始动画   
                break;
            case R.id.btn_close:
                attackAnimation.stop();
                break;
            default:
                break;
        }
    }
}
    注意:不能再onCreate中调用AnimationDrawable的start(),因为这是的窗口window对象还没有完全的初始化完成,AnimationDrawable不能狗<span style="font-family: Arial, Helvetica, sans-serif;">完全加入到window对象。为此,我们可以把start()方法添加到onWindowFocusChanged()这个重载方法内,就可以解决上述问题。
  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值