靈動菜單
先看效果圖
這里寫圖片描述
分析一下匈仗,首先幌蚊,每個小菜單,都是得具有用戶交互性,所以肯定不能使用視圖動畫纺弊,必須使用屬性動畫,其次,只需要針對每個不同的按鈕設(shè)置不同的動畫,并設(shè)置相應(yīng)的插值器就可以實(shí)現(xiàn)張開脓杉,合攏效果了。理清思路后简逮,實(shí)現(xiàn)就比較簡單了丽已,下面上代碼是實(shí)戰(zhàn):
package com.example.administrator.myapplication;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.net.VpnService;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Administrator on 2015/11/28 0028.
*/
public class MyCountTimeActivity extends Activity {
ArrayList<View> views = new ArrayList<View>();
ImageView i1,i2,i3,i4,i5;
boolean isclose = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countime);
i1 = (ImageView)findViewById(R.id.i1);
i2 = (ImageView)findViewById(R.id.i2);
i3 = (ImageView)findViewById(R.id.i3);
i4 = (ImageView)findViewById(R.id.i4);
i5 = (ImageView)findViewById(R.id.i5);
views.add(i1);
views.add(i2);
views.add(i3);
views.add(i4);
views.add(i5);
}
public void starAnim(View view){
if (isclose ){
star();
}else {
close();
}
}
private void star(){
ObjectAnimator animator1 = ObjectAnimator.ofFloat(
views.get(0),"alpha",1F,0.5F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(
views.get(1),"translationY",200F);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(
views.get(2),"translationX",200F);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(
views.get(3),"translationY",-200F);
ObjectAnimator animator5 = ObjectAnimator.ofFloat(
views.get(4),"translationX",-200F);
AnimatorSet set = new AnimatorSet();
set.setDuration(500);
/**
* 需要不同插值器效果可以自己嘗試
*/
//AccelerateDecelerateInterpolator 先增速,再減速插值器
// BounceInterpolator 彈跳
set.setInterpolator(new BounceInterpolator());
set.playTogether(animator1, animator2, animator3, animator4, animator5);
set.start();
isclose = false;
}
private void close(){
ObjectAnimator animator1 = ObjectAnimator.ofFloat(
views.get(0),"alpha",0.5F,1F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(
views.get(1),"translationY",0F);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(
views.get(2),"translationX",0F);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(
views.get(3),"translationY",0F);
ObjectAnimator animator5 = ObjectAnimator.ofFloat(
views.get(4),"translationX",0F);
AnimatorSet set = new AnimatorSet();
set.setDuration(500);
set.setInterpolator(new BounceInterpolator());
set.playTogether(animator1, animator2, animator3, animator4, animator5);
set.start();
isclose = true;
}
}
這里只是一個很簡單的例子买决,更多炫酷的效果沛婴,還需要慢慢探索,練習(xí)督赤。
計時器動畫
點(diǎn)擊后嘁灯,數(shù)字會不斷增加
package com.example.administrator.myapplication;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
/**
* Created by Administrator on 2015/11/28 0028.
*/
public class MyCountTimeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.countime);
}
public void tvTimer(final View view){
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0,100);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
((TextView)view).setText("$"+animation.getAnimatedValue());
}
});
valueAnimator.setDuration(3000);
valueAnimator.start();
}
}