歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android動畫案例,淡入淡出效果

1、Android動畫測試程序,界面如圖:

顏色隨機變化,點擊視圖,左淡出,右淡入,下淡出,上淡入效果。

Android動畫案例,淡入淡出效果源碼下載

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /2013年資料/3月/24日/Android動畫案例,淡入淡出效果

主要源代碼解析:

package com.example.objectanimatortest;

import java.util.Random;

import android.os.Build;
import android.os.Bundle;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ArgbEvaluator;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.TextureView;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class MainActivity extends Activity implements View.OnClickListener{
 private TextView[] tv;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.objectanimatortest);
  tv = new TextView[]{
   (TextView) findViewById(R.id.tv01),
   (TextView) findViewById(R.id.tv02),
   (TextView) findViewById(R.id.tv03),
   (TextView) findViewById(R.id.tv04),
   
   (TextView) findViewById(R.id.tv11),
   (TextView) findViewById(R.id.tv12),
   (TextView) findViewById(R.id.tv13),
   (TextView) findViewById(R.id.tv14)
  };
  initTextViews();
 }
 private void initTextViews(){
  for(TextView textView :tv){//顏色隨機變化代碼    使用ValueAnimator類給每個視圖加上北京顏色隨時間變化的動畫
   int color1 = Color.rgb((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)) ;
   int color2 = Color.rgb((new Random()).nextInt(255), (new Random()).nextInt(255), (new Random()).nextInt(255)) ;
   ValueAnimator animator = ObjectAnimator.ofInt(textView, "backgroundColor", color1,color2);
   animator.setDuration(3000);
   animator.setEvaluator(new ArgbEvaluator());//設置數值計算器,保證動畫變化過程中的數值正確
   animator.setRepeatCount(ValueAnimator.INFINITE);
   animator.setRepeatMode(ValueAnimator.REVERSE);
   animator.start();
   
   textView.setOnClickListener(this);
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  //getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 @Override
 public void onClick(final View view) {
  final ValueAnimator animator1 = ObjectAnimator.ofFloat(view, "alpha",1,0);//淡出效果
  animator1.setDuration(1000);
  animator1.setInterpolator(new AccelerateInterpolator());
  ValueAnimator animator2 = ObjectAnimator.ofFloat(view,"x",view.getX(),(view.getX()-view.getWidth()));//向左移動效果
  animator2.setDuration(1000);
  animator2.setInterpolator(new DecelerateInterpolator());
 
  AnimatorSet animatorSet = new AnimatorSet();//合起來就是左淡出效果
  animatorSet.play(animator2).before(animator1);
//  animatorSet.start();
 
  final ValueAnimator animator3 = ObjectAnimator.ofFloat(view, "alpha",0,1);//淡入效果
  animator3.setDuration(1000);
  animator3.setInterpolator(new AccelerateInterpolator());
  ValueAnimator animator4 = ObjectAnimator.ofFloat(view,"x",view.getX()+2*view.getWidth(),view.getX());//從右邊向左移動
  animator4.setDuration(1000);
  animator4.setInterpolator(new DecelerateInterpolator());
 
  animator4.addListener(new AnimatorListenerAdapter() {//當動畫播放完,我們做什麼

   @Override
   public void onAnimationEnd(Animator animation) {//向下移動淡出,然後向上移動淡入
    super.onAnimationEnd(animation);
    final ValueAnimator animatorY = ObjectAnimator.ofFloat(view, "y", view.getY(),view.getY()+view.getHeight());
    animatorY.setDuration(1000);
    final ValueAnimator alphaY = animator1.clone();
   
    ValueAnimator rotate = ObjectAnimator.ofFloat(view,"rotationY",0,90);
    rotate.setDuration(2000);
//    rotate.start();
   
    animatorY.addListener(new AnimatorListenerAdapter() {
     
     @Override
     public void onAnimationEnd(Animator animation) {
      super.onAnimationEnd(animation);
      animatorY.reverse();
      animator3.clone().start();
     }
     
    });
    AnimatorSet set = new AnimatorSet();
    set.play(animatorY).with(alphaY);
    set.start();
   }
   
  });
 
//  animatorSet.play(animator3).after(animator1);//合起來就是左淡出,右淡入效果
//  animatorSet.play(animator3).with(animator4);
//  animatorSet.start();
 
  AnimatorSet animatorSet1 = new AnimatorSet();
  animatorSet1.play(animator3).with(animator4);
 
  AnimatorSet animatorSet2 = new AnimatorSet();
  animatorSet2.play(animator2).with(animator1);
 
  AnimatorSet set = new AnimatorSet();
  set.playSequentially(animatorSet2,animatorSet1);//使用playSequentially方法測試效果
  set.start();
 }

}

在源代碼中還有測試類,修改一下mainifest.xml文件,讓另外的那個類運行便可以看到效果。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved