如何从android中的图像列表制作幻灯片

时间:2014-06-02 09:59:03

标签: android timer slideshow universal-image-loader picasso

我已经制作了一个Android应用程序,因为我得到一个ImageUrls的arrayList,我想通过使用这个arrayLis在android中创建一个自动化的lsideShow,我试过如下使用线程但它给了我arrayIndexOPutOfBounds异常,它去了到第二张图片abd然后给出异常,请帮我保存,我的代码如下:

resultArray = new ArrayList<String>();
iv_paly.setOnClickListener(new OnCLickListener()){

@Override
OnClick(){
            imageLoader.displayImage(resultArray.get(1), proImage, options);
            proImage.postDelayed(swapImage, 3000);
}
MediaPlayer introSound, bellSound;
    Runnable swapImage = new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i <= resultArray.size(); i++) {
                imageLoader.displayImage(resultArray.get(i), proImage, options);
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

试试这段代码

       package com.example.imagegal;
       import android.os.Bundle;
       import android.app.Activity;
       import android.content.Context;
       import android.view.Menu;
       import android.view.MenuItem;
       import android.view.View;
       import android.view.ViewGroup;
       import android.view.Window;
       import android.view.animation.AnimationUtils;
       import android.widget.AdapterView;
       import android.widget.BaseAdapter;
       import android.widget.Gallery;
        import android.widget.ImageSwitcher;
       import android.widget.ImageView;
       import android.widget.ViewSwitcher; 
       import android.widget.Gallery.LayoutParams;
       import android.support.v4.app.NavUtils;


public class MainActivity extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.slide_out_right));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.slide_in_left));

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}

public void onNothingSelected(AdapterView<?> parent) {
}

public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT));
return i;
}

private ImageSwitcher mSwitcher;

public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView i = new ImageView(mContext);

   i.setImageResource(mThumbIds[position]);
    i.setAdjustViewBounds(true);
    i.setLayoutParams(new Gallery.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    i.setBackgroundResource(R.drawable.background);
    return i;
}

private Context mContext;

}

private Integer[] mThumbIds = {
    R.drawable.a, R.drawable.b,
    R.drawable.c, R.drawable.d};

private Integer[] mImageIds = {
    R.drawable.a, R.drawable.b, R.drawable.c,
    R.drawable.d};

}

xml文件

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<ImageSwitcher android:id="@+id/switcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
/>

<Gallery android:id="@+id/gallery"
    android:background="#55000000"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"

    android:gravity="center_vertical"
    android:spacing="16dp"
/>
  </RelativeLayout>