Android两张图片渐变实现

902 阅读1分钟

###Android两张图片渐变实现

  下面代码实现从bg1.png到bg2.png经过4000ms的转变。主要用到了android.graphics.drawable.TransitionDrawable.TransitionDrawable类

//得到此View下的资源
Resource res = getResource();
//图片渐变对象
TransitionDrawable imageTransitionDrawable = null;
imageTransitionDrawable = new TransitionDrawable(
	new Drawable[]{
		res.getDrawable(R.drawable.bg1),
		res.getDrawable(R.drawable.bg2)
	}
);
//设置背景图片为渐变图片
this.setBackgroundDrawable(imageTransitionDrawable);
//经过4000ms的图片渐变过程
imageTransitionDrawable.startTransition(4000);