Pra*_*dey 18 java android android-animation
我想要做的是水平移动背景并让它无限重复.
我尝试使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
Xav*_*ler 55
你为什么不尝试自己动画背景而不是使用ViewSwitcher?所有你需要的只是一个简单ValueAnimator:
首先添加两个与ImageViews布局相同的内容,并将相同的背景图像设置为两个:
<?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>
Run Code Online (Sandbox Code Playgroud)
然后使用a 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();
Run Code Online (Sandbox Code Playgroud)
这导致连续动画无限期地重复背景,应该看起来像这样:

您可以使用AndroidScrollingImageView库,只需定义速度和可绘制源
<com.q42.android.scrollingimageview.ScrollingImageView
android:id="@+id/scrolling_background"
android:layout_width="match_parent"
android:layout_height="wrap_content"
scrolling_image_view:speed="1dp"
scrolling_image_view:src="@drawable/scrolling_background" />
Run Code Online (Sandbox Code Playgroud)
编辑:
如@Cliff Burton所述,如果要垂直滚动,可以将视图旋转90度。
| 归档时间: |
|
| 查看次数: |
27002 次 |
| 最近记录: |