Android---Tween動畫樣本(XML定義的動畫)

來源:互聯網
上載者:User

1、在res/anim目錄下建立XML檔案:tween_anim.xml


[html]
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <alpha 
        android:fromAlpha="0.2" 
        android:toAlpha="1.0" 
        android:duration="3000" 
        android:repeatMode="reverse" 
        android:repeatCount="10" /> 
    <scale 
        android:fromXScale="0.2" 
        android:toXScale="1.0" 
        android:fromYScale="0.2" 
        android:toYScale="1.0" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="3000" 
        android:repeatMode="reverse" 
        android:repeatCount="10" /> 
    <translate 
        android:fromXDelta="50" 
        android:toXDelta="100" 
        android:fromYDelta="50" 
        android:toYDelta="100" 
        android:duration="3000" 
        android:repeatMode="restart" 
        android:repeatCount="10" /> 
    <rotate 
        android:fromDegrees="0" 
        android:toDegrees="360" 
        android:duration="3000" 
        android:repeatMode="restart" 
        android:repeatCount="10" /> 
</set> 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <alpha
  android:fromAlpha="0.2"
  android:toAlpha="1.0"
  android:duration="3000"
  android:repeatMode="reverse"
  android:repeatCount="10" />
 <scale
  android:fromXScale="0.2"
  android:toXScale="1.0"
  android:fromYScale="0.2"
  android:toYScale="1.0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:duration="3000"
  android:repeatMode="reverse"
  android:repeatCount="10" />
 <translate
  android:fromXDelta="50"
  android:toXDelta="100"
  android:fromYDelta="50"
  android:toYDelta="100"
  android:duration="3000"
  android:repeatMode="restart"
  android:repeatCount="10" />
 <rotate
  android:fromDegrees="0"
  android:toDegrees="360"
  android:duration="3000"
  android:repeatMode="restart"
  android:repeatCount="10" />
</set>
2、在res/layout目錄下建立XML檔案:tween_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:id="@+id/imgTween" 
        android:src="@drawable/c01" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_weight="1.0" /> 
    <Button 
        android:id="@+id/btnControl" 
        android:text="開始" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" /> 
 
</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:id="@+id/imgTween"
  android:src="@drawable/c01"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:layout_weight="1.0" />
 <Button
  android:id="@+id/btnControl"
  android:text="開始"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" />

</LinearLayout>
3、Activity裡面添加代碼:


[java]
package com.bison; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.ImageView; 
 
public class TweenAnimationDemo extends Activity { 
    // 聲明一個開始停止的標識符  
    private boolean flags = true; 
    private ImageView imgTween; 
    private Button btnCtrl; 
 
    /** 初始化 */ 
    public void init() { 
        imgTween = (ImageView) findViewById(R.id.imgTween); 
        // 聲明Tween動畫  
        final Animation anim = AnimationUtils.loadAnimation(this, 
                R.anim.tween_anim); 
 
        btnCtrl = (Button) findViewById(R.id.btnControl); 
        btnCtrl.setOnClickListener(new OnClickListener() { 
 
            public void onClick(View v) { 
                if (flags) { 
                    btnCtrl.setText("停止"); 
                    imgTween.startAnimation(anim); 
                    flags = false; 
                } else { 
                    btnCtrl.setText("開始"); 
                    imgTween.clearAnimation(); 
                    flags = true; 
                } 
 
            } 
        }); 
 
    } 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.tween_anim_layout); 
        init(); 
    } 

package com.bison;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class TweenAnimationDemo extends Activity {
 // 聲明一個開始停止的標識符
 private boolean flags = true;
 private ImageView imgTween;
 private Button btnCtrl;

 /** 初始化 */
 public void init() {
  imgTween = (ImageView) findViewById(R.id.imgTween);
  // 聲明Tween動畫
  final Animation anim = AnimationUtils.loadAnimation(this,
    R.anim.tween_anim);

  btnCtrl = (Button) findViewById(R.id.btnControl);
  btnCtrl.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
    if (flags) {
     btnCtrl.setText("停止");
     imgTween.startAnimation(anim);
     flags = false;
    } else {
     btnCtrl.setText("開始");
     imgTween.clearAnimation();
     flags = true;
    }

   }
  });

 }

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.tween_anim_layout);
  init();
 }
}

PS:Tween動畫有alpha(透明)、scale(縮放)、translate(移動)、rotate(旋轉),每個有不同的定義,仔細研究。  


摘自  今非昔…畢…的專欄 

聯繫我們

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