Animation動畫抽象類別

來源:互聯網
上載者:User

我的小練習,有幾個常用的動畫效果,還不錯奧

主代碼java檔案

package mars.com;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.Button;import android.widget.ImageView;public class Demo_Activity extends Activity {private Button button1;// 啟動private Button button2;// 停止private Button button3;// 旋轉動畫開始按鈕private Button button4;// 尺寸變化動畫開始按鈕private Button button5;// 透明動畫開始按鈕private Button button6;// 動畫集合開始按鈕private Animation animation;// 一般動畫private Animation rotateAnimation;// 旋轉動畫private Animation scaleAnimation;// 尺寸變化動畫private Animation alphaAnimation;// 透明動畫private AnimationSet set;// 動畫類集合private ImageView image;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button3 = (Button) findViewById(R.id.button3);button4 = (Button) findViewById(R.id.button4);button5 = (Button) findViewById(R.id.button5);button6 = (Button) findViewById(R.id.button6);image = (ImageView) findViewById(R.id.image);animation = new TranslateAnimation(0, 300, 0, 300);// 移動動畫效果rotateAnimation = new RotateAnimation(0f, 360f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);// 設定旋轉變化動畫對象scaleAnimation = new ScaleAnimation(0f, 1f, 0f, 1f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);// 設定尺寸變化動畫類alphaAnimation = new AlphaAnimation(0.1f, 1.0f);// 透明動畫類set = new AnimationSet(true);// 設定動畫集合對象// 下面是監聽器button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {animation.setDuration(3000);// 設定動畫期間animation.setRepeatCount(2);// 設定動畫重複次數,不是動畫執行次數animation.setFillBefore(true);// 回到初始位置animation.setFillEnabled(true);// 使能填充效果animation.setRepeatMode(Animation.RESTART);// 重新從頭執行animation.setRepeatMode(Animation.REVERSE);// 反方向執行animation.setStartOffset(3000);// 設定動畫的啟動的時間image.setAnimation(animation);// 設定動畫效果animation.startNow();// 啟動動畫}});button2.setOnClickListener(new OnClickListener() {public void onClick(View v) {animation.cancel();// 取消動畫rotateAnimation.cancel();// 取消動畫scaleAnimation.cancel();// 取消動畫set.cancel();// 取消動畫}});button3.setOnClickListener(new OnClickListener() {public void onClick(View v) {rotateAnimation.setDuration(3000);// 設定動畫過程時間image.setAnimation(rotateAnimation);// 設定動畫效果rotateAnimation.startNow();// 啟動動畫}});button4.setOnClickListener(new OnClickListener() {public void onClick(View v) {scaleAnimation.setDuration(3000);// 設定動畫過程的時間image.setAnimation(scaleAnimation);// 設定動畫效果scaleAnimation.startNow();// 啟動}});button5.setOnClickListener(new OnClickListener() {public void onClick(View v) {alphaAnimation.setDuration(3000);// 設定動畫過程時間image.setAnimation(alphaAnimation);// 設定動畫效果alphaAnimation.startNow();// 啟動動畫}});button6.setOnClickListener(new OnClickListener() {public void onClick(View v) {// 設定各個效果的時間animation.setDuration(10000);scaleAnimation.setDuration(10000);alphaAnimation.setDuration(10000);// 添加各種動畫set.addAnimation(animation);set.addAnimation(rotateAnimation);set.addAnimation(alphaAnimation);set.setFillAfter(true);// 停留在最後的位置set.setFillEnabled(true);image.setAnimation(set);// 設定圖片的動畫效果set.startNow();// 啟動動畫}});}}

xml檔案

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="執行動畫" />    <Button        android:id="@+id/button2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="停止動畫" />    <Button        android:id="@+id/button3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="旋轉動畫類" />    <Button        android:id="@+id/button4"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="尺寸變化動畫類" />    <Button        android:id="@+id/button5"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="透明動畫類" />    <Button        android:id="@+id/button6"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="動畫集合類" />    <ImageView        android:id="@+id/image"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/ic_launcher" /></LinearLayout>

動畫效果如下

聯繫我們

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