我想要做的是水平移动背景并让它无限重复.
我尝试使用带有动画的ImageSwitcher来实现这种效果,但无法让它正常工作.这是我到目前为止的代码
public class MainActivity extends AppCompatActivity implements VIEwSwitcher.VIEwFactory { private Animation animslIDe; private ImageSwitcher image; private ImageVIEw imagePop; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); image = (ImageSwitcher) findVIEwByID(R.ID.image_switcher); image.setFactory(this); image.setimageResource(R.drawable.zc06); Animation in = AnimationUtils.loadAnimation(this, androID.R.anim.slIDe_in_left); in.setDuration(10000); Animation out = AnimationUtils.loadAnimation(this, androID.R.anim.slIDe_out_right); out.setDuration(10000); image.setInAnimation(in); image.setoutAnimation(out); Timer timer = new Timer(); timer.scheduleAtFixedrate(new TimerTask() { @OverrIDe public voID run() { runOnUiThread(new Runnable() { @OverrIDe public voID run() { image.setimageResource(R.drawable.zc06); } }); } }, 0, 10000); Animation mZoomInAnimation = AnimationUtils.loadAnimation(this, R.anim.zoom_in); Animation mZoomOutAnimation = AnimationUtils.loadAnimation(this, R.anim.zoom_out); imagePop.startAnimation(mZoomInAnimation); imagePop.startAnimation(mZoomOutAnimation); } @OverrIDe public VIEw makeVIEw() { ImageVIEw myVIEw = new ImageVIEw(getApplicationContext()); return myVIEw; }}
解决方法:
为什么不尝试自己动画背景而不是使用VIEwSwitcher?您只需要一个简单的ValueAnimator:
首先将两个相同的ImageVIEw添加到您的布局中,并将相同的背景图像设置为它们:
<?xml version="1.0" enCoding="utf-8"?><FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <ImageVIEw androID:ID="@+ID/background_one" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:src="@drawable/background"/> <ImageVIEw androID:ID="@+ID/background_two" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:src="@drawable/background"/></FrameLayout>
然后使用ValueAnimator为其translateX属性设置动画,但按宽度偏移它们:
final ImageVIEw backgroundOne = (ImageVIEw) findVIEwByID(R.ID.background_one);final ImageVIEw backgroundTwo = (ImageVIEw) findVIEwByID(R.ID.background_two);final ValueAnimator animator = ValueAnimator.offloat(0.0f, 1.0f);animator.setRepeatCount(ValueAnimator.INFINITE);animator.setInterpolator(new linearInterpolator());animator.setDuration(10000L);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @OverrIDe public voID onAnimationUpdate(ValueAnimator animation) { final float progress = (float) animation.getAnimatedValue(); final float wIDth = backgroundOne.getWIDth(); final float translationX = wIDth * progress; backgroundOne.setTranslationX(translationX); backgroundTwo.setTranslationX(translationX - wIDth); }});animator.start();
这导致连续动画无限期地重复背景,应该看起来像这样:
总结以上是内存溢出为你收集整理的java – Android通过动画连续移动背景全部内容,希望文章能够帮你解决java – Android通过动画连续移动背景所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)