灵动菜单
先看效果图
分析一下,首先,每个小菜单,都是得具有用户交互性,所以肯定不能使用视图动画,必须使用属性动画,其次,只需要针对每个不同的按钮设置不同的动画,并设置相应的插值器就可以实现张开,合拢效果了。理清思路后,实现就比较简单了,下面上代码是实战:
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;
}
}
这里只是一个很简单的例子,更多炫酷的效果,还需要慢慢探索,练习。
计时器动画
点击后,数字会不断增加
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();
}
}