Android---Frame動畫樣本

來源:互聯網
上載者:User

Frame動畫:

1、找到一組圖片c01.jpg,c02.jpg,c03.jpg,c04.jpg,c05.jpg,copy到res/drawable目錄下;

2、在res/drawable目錄下建立XML檔案:frame_anim.xml


[html]
<?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/c01" android:duration="500"/> 
 <item android:drawable="@drawable/c02" android:duration="500"/> 
 <item android:drawable="@drawable/c03" android:duration="500"/> 
 <item android:drawable="@drawable/c04" android:duration="500"/> 
 <item android:drawable="@drawable/c05" android:duration="500"/> 
</animation-list> 
<?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/c01" android:duration="500"/>
 <item android:drawable="@drawable/c02" android:duration="500"/>
 <item android:drawable="@drawable/c03" android:duration="500"/>
 <item android:drawable="@drawable/c04" android:duration="500"/>
 <item android:drawable="@drawable/c05" android:duration="500"/>
</animation-list>3、在res/layout目錄下建立XML檔案:frame_anim_layout.xml


[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:scaleType="centerInside" 
        android:id="@+id/imgFrame" 
        android:background="@drawable/frame_anim"></ImageView> 
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 
        <Button 
            android:text="開始" 
            android:id="@+id/btnStart" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1.0"></Button> 
        <Button 
            android:text="結束" 
            android:id="@+id/btnEnd" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1.0"></Button> 
    </LinearLayout> 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent">
 <ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scaleType="centerInside"
  android:id="@+id/imgFrame"
  android:background="@drawable/frame_anim"></ImageView>
 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <Button
   android:text="開始"
   android:id="@+id/btnStart"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1.0"></Button>
  <Button
   android:text="結束"
   android:id="@+id/btnEnd"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1.0"></Button>
 </LinearLayout>
</LinearLayout>4、Activity檔案中的代碼:


[java]
package com.bison; 
 
import android.app.Activity; 
import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class AnimationDemoActivity extends Activity implements OnClickListener { 
    // 開始按鈕  
    private Button btnStart; 
    // 結束按鈕  
    private Button btnEnd; 
    private ImageView imgFrame; 
    // 聲明Frame動畫對象  
    private AnimationDrawable frameAnim; 
 
    /** 初始化 */ 
    public void init() { 
        btnStart = (Button) findViewById(R.id.btnStart); 
        btnEnd = (Button) findViewById(R.id.btnEnd); 
        btnStart.setOnClickListener(this); 
        btnEnd.setOnClickListener(this); 
 
        imgFrame = (ImageView) findViewById(R.id.imgFrame); 
        // 將ImageView的backgroud聲明給Frame動畫對象  
        frameAnim = (AnimationDrawable) imgFrame.getBackground(); 
    } 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        // 載入layout.frame_anim_layout頁面  
        setContentView(R.layout.frame_anim_layout); 
        init(); 
    } 
 
    public void onClick(View v) { 
        // 判斷按鈕事件  
        switch (v.getId()) { 
        case R.id.btnStart: 
            frameAnim.start(); 
            break; 
        case R.id.btnEnd: 
            frameAnim.stop(); 
            break; 
        } 
    } 

package com.bison;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationDemoActivity extends Activity implements OnClickListener {
 // 開始按鈕
 private Button btnStart;
 // 結束按鈕
 private Button btnEnd;
 private ImageView imgFrame;
 // 聲明Frame動畫對象
 private AnimationDrawable frameAnim;

 /** 初始化 */
 public void init() {
  btnStart = (Button) findViewById(R.id.btnStart);
  btnEnd = (Button) findViewById(R.id.btnEnd);
  btnStart.setOnClickListener(this);
  btnEnd.setOnClickListener(this);

  imgFrame = (ImageView) findViewById(R.id.imgFrame);
  // 將ImageView的backgroud聲明給Frame動畫對象
  frameAnim = (AnimationDrawable) imgFrame.getBackground();
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // 載入layout.frame_anim_layout頁面
  setContentView(R.layout.frame_anim_layout);
  init();
 }

 public void onClick(View v) {
  // 判斷按鈕事件
  switch (v.getId()) {
  case R.id.btnStart:
   frameAnim.start();
   break;
  case R.id.btnEnd:
   frameAnim.stop();
   break;
  }
 }
}


PS:Frame動畫原理類似於電影膠片,一幕一幕的閃過,在人的視覺停留期快速變動,形成組圖,產生動畫。

 

摘自  今非昔…畢…的專欄 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.