Animation 动画透明转换

今天公司让做Animation的动态转换效果。其中一个功能就是将一张图片透明后转换成另外一张图片。用Handler更新UI的时候总是达不到效果,后来才发现Animation有监听事件,写个监听就搞定了。代码如下 有需要的参考吧
view = (ImageView)getTarget();
		alpha1 = new AlphaAnimation((float)fromAlpha, (float)toAlpha);
		alpha2 = new AlphaAnimation((float)toAlpha,(float)fromAlpha);
		alpha1.setDuration((long)(getDuration()*1000));
		alpha2.setDuration((long)(getDuration()*1000));

		alpha1.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
			}	
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onAnimationEnd(Animation animation) {
				// TODO Auto-generated method stub
				Bitmap bmp = BitmapFactory.decodeResource(TransitionActivity.mContext.getResources(), R.drawable.image2);
				view.setImageBitmap(bmp);
				view.startAnimation(alpha2);
			}
		});
		
		handler.post(new Runnable() {
			
			@Override
			public void run() {
					view.startAnimation(alpha1);
			}
		});
	}

另外还有一种方法就是用Android提供的AnimationSet将要播放的动画add到AnimationSet中,根据动画的时间一一执行。
@Override
	public Animation translate(float x, float y) {
		Animation alpha = new AlphaAnimation(fromAlpha, toAlpha);
		AnimationSet amSet = new AnimationSet(false);
		if(ImageObject.resId != 0){
			Animation alphaGlow = new AlphaAnimation(toAlpha,fromAlpha);
			alphaGlow.setDuration(1000);
			alphaGlow.setStartOffset(2000);
			amSet.addAnimation(alpha);
			amSet.addAnimation(alphaGlow);
			return amSet;
		}
		return alpha;
	}

你可能感兴趣的:(UI)