在我的项目中,我想要像这样无限地使用一个大图案来动画化背景:
更新:我的源代码没有重复(注意:在GIF动画中,我水平绘制了图案以简化表示,但实际上我需要垂直平移动画)。
更新:我的源代码没有重复(注意:在GIF动画中,我水平绘制了图案以简化表示,但实际上我需要垂直平移动画)。
background.setImageResource(R.drawable.background);
background.setScaleType(ScaleType.MATRIX);
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
private Matrix matrix = new Matrix();
@Override public void onAnimationUpdate(ValueAnimator animation) {
float factor = (Float) animation.getAnimatedValue();
int width = background.getDrawable().getIntrinsicWidth();
int height = background.getDrawable().getIntrinsicHeight();
float scale = (float) background.getWidth() / (float) width;
matrix.reset();
matrix.postTranslate(0, -height * factor);
matrix.postScale(scale, scale);
background.setImageMatrix(matrix);
}
});
animator.setInterpolator(new LinearInterpolator());
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setRepeatMode(ValueAnimator.RESTART);
animator.setDuration(10000);
animator.start();