Android ViewFlipper仅动画第一个URL图像

时间:2015-06-25 13:41:06

标签: android viewflipper autostart

我有一个带有ViewFlipper的android活动。 它从URL中选取图像并以布局显示。 它应该是所有图像的动画。 但是,它只会动画第一个url图像并且不断重复。 它不显示其他图像。 以下是代码:

公共类DynamicViewFlipper扩展了Activity {

/**
 * List of Image URL that will populate the ViewFlipper
 */
private List<String> imageURLs = Arrays.asList(new String[] { 
        "http://example.com/image1.jpg",
        "http://example.com/image2.jpg", 
        "http://example.com/image3.jpg",
        "http://example.com/image4.jpg"}); 



private int index = 0;

private TextView mTextView;

private ViewFlipper mViewFlipper;

private Button mNextButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_flipper);
    mTextView = (TextView) findViewById(R.id.title);
    mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);


            ImageView image = new ImageView(getApplicationContext());
            UrlImageViewHelper.setUrlDrawable(image, getNextImage(), R.drawable.loading);
            mTextView.setText("Showing: " + index);
            mViewFlipper.addView(image);
            mViewFlipper.showNext();
            mViewFlipper.setAutoStart(true);
            mViewFlipper.setFlipInterval(2000);
            mViewFlipper.startFlipping();

    mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_in_left));
    mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.slide_out_right));
}

protected String getNextImage() {
    if (index == imageURLs.size())
        index = 0;
    return imageURLs.get(index++);
}

}

1 个答案:

答案 0 :(得分:1)

你应该有类似的东西:

for(String url: imageURLs){
   ImageView image = new ImageView(getApplicationContext());

   UrlImageViewHelper.setUrlDrawable(image, url,R.drawable.loading);

  mViewFlipper.addView(image);
}

mViewFlipper.setAutoStart(true);
mViewFlipper.setFlipInterval(2000);
mViewFlipper.startFlipping();