我想要做的是水平移動背景並讓它無限重複。Android隨着動畫不斷移動背景
我嘗試使用帶動畫的ImageSwitcher
來提供此效果,但無法使其正常工作。這是我的代碼,以便farL
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;
}
}
你爲什麼使用視圖動畫?你真的不應該再使用它們了......使用新的動畫API。無論如何,您可以通過在佈局中使用兩個「ImageViews」並使用「ValueAnimator」翻譯它們來輕鬆創建這樣的動畫。查看我的答案獲取更多信息。 –