Android 一般動畫animation和屬性動畫animator

來源:互聯網
上載者:User

標籤:android   java   


 
package com.example.animation;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}    public void click(View view){Toast.makeText(this, "click", Toast.LENGTH_SHORT).show();} public void move(View view ){float fromXDelta=0;float toXDelta=0;float fromYDelta=0;float toYDelta=200;TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);/** * time */animation.setDuration(1000);animation.setFillAfter(true);ImageView imageView=(ImageView)findViewById(R.id.imageView);imageView.startAnimation(animation);}     }

這是一般動畫,再看屬性動畫

區別:一般動畫變換後只是圖片移動了,view的位置不變,然而屬性動畫view隨著圖片移動。

public void move(View view ){ImageView imageView=(ImageView)findViewById(R.id.imageView);ObjectAnimator.ofFloat(imageView, "translationY", 0f,200f).setDuration(1000).start();}

多種動畫同時進行

public void move(View view ){ImageView imageView=(ImageView)findViewById(R.id.imageView);ObjectAnimator.ofFloat(imageView, "translationY", 0f,200f).setDuration(1000).start();ObjectAnimator.ofFloat(imageView, "translationX", 0f,200f).setDuration(1000).start();ObjectAnimator.ofFloat(imageView, "rotation", 0,360f).setDuration(1000).start();}
Google提供了一種更節省系統資源的多種動畫同時播放

public void move(View view ){ImageView imageView=(ImageView)findViewById(R.id.imageView);PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation", 0,360f);PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX", 0f,200f);PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY", 0f,200f);ObjectAnimator.ofPropertyValuesHolder(imageView, p1,p2,p3).setDuration(1000).start();}
同時Google提供了animatorset,允許多種不同動畫按照使用者要求播放,如使用set.palyTpgether()   set.playSequentially()

public void move(View view ){ImageView imageView=(ImageView)findViewById(R.id.imageView);ObjectAnimator animator1=ObjectAnimator.ofFloat(imageView, "rotation", 0,360f);ObjectAnimator animator2=ObjectAnimator.ofFloat(imageView, "translationX", 0,200f);ObjectAnimator animator3=ObjectAnimator.ofFloat(imageView, "translationY", 0,200f);AnimatorSet set=new AnimatorSet();//set.playTogether(animator1,animator2,animator3);set.playSequentially(animator1,animator2,animator3);set.setDuration(1000);set.start();}





Android 一般動畫animation和屬性動畫animator

聯繫我們

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