zoukankan      html  css  js  c++  java
  • 第三十讲:Android之Animation(五)

    天行健,君子以自强不息。——《周易·乾·象》


    本讲内容:逐帧动画 Frame Animation

    逐帧动画 Frame Animation就是说一帧一帧的连起来播放就变成了动画,和放电影的机制非常相似。

    我们通过一个样例感受一下,代码的解说都写在凝视里了

    以下是res/layout/activity_main.xml 布局文件:

    <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:orientation="vertical"
        tools:context="com.example.text.MainActivity$PlaceholderFragment" >
     <ImageView 
            android:id="@+id/frame_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
        <Button 
            android:id="@+id/start"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="runFrame"/>
        <Button 
            android:id="@+id/stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="stopFrame"/>
    </LinearLayout>

    以下是新建的res/anim/frame.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
        android:oneshot="false">  
        <item android:drawable="@drawable/b1" android:duration="300" />  
        <item android:drawable="@drawable/b2" android:duration="300" />  
        <item android:drawable="@drawable/b3" android:duration="300" />  
        <item android:drawable="@drawable/b4" android:duration="300" />  
    </animation-list> 

    b1、b2、b3、 b4是四张不同小狐狸图标


    以下是MainActivity.java主界面文件:

    public class MainActivity extends Activity implements OnClickListener {
    	private Button start;
    	private Button stop;
    	private ImageView iv;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		iv = (ImageView) findViewById(R.id.frame_image);
    		start = (Button) findViewById(R.id.start);
    		stop = (Button) findViewById(R.id.stop);
    		start.setOnClickListener(this);
    		stop.setOnClickListener(this);
    		
    		iv.setBackgroundResource(R.anim.frame);
    	}
    	@Override
    	public void onClick(View v) {
    		AnimationDrawable anim = (AnimationDrawable) iv.getBackground();
    		switch (v.getId()) {
    		case R.id.start:
    			// 调用动画可画对象的開始播放方法
    			anim.start();
    			break;
    		case R.id.stop:
    			// 调用动画可画对象的停止播放方法
    			anim.stop();
    			break;
    		}
    	}
    }

    以下是执行结果:



    本说到这里,谢谢大家!

  • 相关阅读:
    子网掩码
    IP详解
    TCP/IP模型和OSI模型的对应
    Nginx模块之请求限制
    Nginx中的压力测试工具
    Nginx服务器的处理机制
    算法笔记-动态规划
    算法笔记-分治法
    算法笔记-贪心算法
    算法笔记-乱七八糟问题
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4183220.html
Copyright © 2011-2022 走看看