第三十講:Android之Animation(五)

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   ar   java   sp   

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


本講內容:逐幀動畫 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;@Overrideprotected 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);}@Overridepublic 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;}}}

下面是運行結果:



本講到這裡,謝謝大家!

第三十講:Android之Animation(五)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.