//第一种写法
//参数 1,要进行动画的控件2 设置动画属性3属性值 开始到结束
ObjectAnimator.ofFloat(mImageView, "translationX", x).setDuration(100).start(); ObjectAnimator.ofFloat(mImageView, "translationY", y).setDuration(100).start(); ObjectAnimator.ofFloat(mImageView, "rotation", 0F, 360F).setDuration(100).start();
PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("translationX", 0F, 360F); PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationY", 0F, 360F); PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("rotation", 0F, 360F); ObjectAnimator.ofPropertyValuesHolder(mImageView, p1,p2,p3).setDuration(1000).start();
//第三种写法
ObjectAnimator ObjectAnimator1=ObjectAnimator.ofFloat(mImageView, "translationX", 0F, 360F); ObjectAnimator ObjectAnimator2=ObjectAnimator.ofFloat(mImageView, "translationY", 0F, 360F); ObjectAnimator ObjectAnimator3=ObjectAnimator.ofFloat(mImageView, "rotation", 0F, 360F); AnimatorSet set = new AnimatorSet(); //set.playTogether(ObjectAnimator1,ObjectAnimator2,ObjectAnimator3);//动画同时播放 //set.playSequentially(ObjectAnimator1,ObjectAnimator2,ObjectAnimator3);//动画顺序播放 set.play(ObjectAnimator1).with(ObjectAnimator2); //自定义动画顺序 1和2同时执行 set.play(ObjectAnimator3).after(ObjectAnimator1); //1完成后执行3 set.setDuration(1000); set.start();
-------------------------------------------------------------------------------------------动画监听事件-------------------------------------------------------------------------------------------------
//动画事件监听
mImageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // Random r=new Random(); // int x = r.nextInt(display.getWidth()-mImageView.getWidth()); // int y = r.nextInt(display.getHeight()-mImageView.getHeight()); // ObjectAnimator.ofFloat(mImageView, "translationX", x).setDuration(100).start(); // ObjectAnimator.ofFloat(mImageView, "translationY", y).setDuration(100).start(); // ObjectAnimator.ofFloat(mImageView, "rotation", 0F, 360F).setDuration(100).start(); ObjectAnimator ObjectAnimator1=ObjectAnimator.ofFloat(mImageView, "alpha", 0,1,0,1,0,1,0,1,0,1); ObjectAnimator1.setDuration(10000); ObjectAnimator1.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { mTextView.setText("动画已经开始"); } @Override public void onAnimationRepeat(Animator animation) { mTextView.setText("动画运行中"); } @Override public void onAnimationEnd(Animator animation) { mTextView.setText("动画已经结束"); } @Override public void onAnimationCancel(Animator animation) { mTextView.setText("动画已经注销"); } }); ObjectAnimator1.start(); } });
int NoType=0; int[] res = {R.id.a,R.id.b,R.id.c,R.id.d,R.id.e,R.id.f,R.id.g,R.id.h}; List<ImageView> List = new ArrayList<ImageView>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menum); for (int i = 0; i < res.length; i++) { ImageView mImageView=(ImageView) findViewById(res[i]); mImageView.setOnClickListener(this); List.add(mImageView); } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.a: if(NoType==0) { StartAnimator(); NoType=1; }else { StopAnimator(); NoType=0; } break; default: break; } } private void StopAnimator() { for (int i = 1; i < res.length; i++) { ObjectAnimator ObjectAnimator1=ObjectAnimator.ofFloat(List.get(i), "translationY", 75*i,0f); ObjectAnimator1.setDuration(1000); //ObjectAnimator1.setInterpolator(new BounceInterpolator()); ObjectAnimator1.setStartDelay(i*300); ObjectAnimator1.start(); } } private void StartAnimator() { for (int i = 1; i < res.length; i++) { ObjectAnimator ObjectAnimator1=ObjectAnimator.ofFloat(List.get(i), "translationY", 0f,75*i); ObjectAnimator1.setDuration(1000); ObjectAnimator1.setInterpolator(new BounceInterpolator());//回弹效果 ObjectAnimator1.setStartDelay(i*300);//动画速度 ObjectAnimator1.start(); } }
//使用ValueAnimator 设置Button的显示值从1-100 mButton2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ValueAnimator ValueAnimator1=ValueAnimator.ofInt(0,100); ValueAnimator1.setDuration(1000); ValueAnimator1.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int value=(Integer) animation.getAnimatedValue(); mButton2.setText(value+""); } }); ValueAnimator1.start(); } });