Android 属性动画

//第一种写法

//参数  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

//使用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();
                
            }
        });





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值