0

我有一个非常简单的Gallery方法,我用它以 2 秒的间隔滚动浏览一些图片。我的问题:如何让这个画廊“无限”,所以第一张照片在最后一张之后又出现了?

笔记

  • 我知道画廊声称已弃用的事实
  • 我还没有对我的代码进行任何优化,因为这只是在高端平板电脑上的快速测试。尽管您对优化的建议也值得赞赏

主要的:

      public class MainActivity extends Activity {
  private Gallery ga;
private Runnable r = null;
private Handler mHandler = null;
private MediaPlayer mp;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    setUpGallery();

            mHandler = new Handler();
    r = new Runnable() {

        public void run() {

            mHandler.postDelayed(r, 2000);
            ga.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);

        }

    };

    r.run();
           }

            private void setUpGallery() {
    ga = (Gallery) findViewById(R.id.gallery);
    ga.setAdapter(new ImageAdapter(this));
    mp = MediaPlayer.create(getApplicationContext(), R.raw.you);
    mp.start();
    mp.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.reset();
            mp.release();

        }
    });

}

适配器:

                 public class ImageAdapter extends BaseAdapter {
private int[] pics = { R.drawable.s1, R.drawable.s2, R.drawable.s3,
        R.drawable.s4, R.drawable.s5, R.drawable.s6, R.drawable.s7,
        R.drawable.s8, R.drawable.s9, R.drawable.s10, R.drawable.s11,
        R.drawable.s12, R.drawable.s13, R.drawable.s14 };
private Context ctx;


public ImageAdapter(Context c) {

    ctx = c;

}

@Override
public int getCount() {

    return pics.length;

}

@Override
public Object getItem(int arg0) {

    return arg0;

}

@Override
public long getItemId(int arg0) {

    return arg0;

}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = new ViewHolder();
    holder.img = new ImageView(ctx);
    Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), pics[position]);
    holder.img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(bmp, 10));

    return holder.img;

}

static class ViewHolder {

    public ViewHolder() {

    }

    ImageView img;

}

    }
4

2 回答 2

1

您必须通过扩展类来制作自定义 Gallaryadapter ..

我刚刚从这里找到了解决方案

http://blog.blundell-apps.com/infinite-scrolling-gallery/

我可能对你有帮助;

你也可以在你的代码中尝试这个..

mHandler = new Handler();
    r = new Runnable() {

        public void run() {
            if (count == adapter.getCount()) {
                count = 0;

                ga.setSelection(0);
                mHandler.postDelayed(r, 2000);
            } else {
                mHandler.postDelayed(r, 2000);
                ga.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, null);
                count++;

            }
        }

    };


    r.run();
于 2013-03-08T13:42:51.043 回答
0

好吧,实际上解决方案比我想象的要容易得多。它可能不适合所有人,但在我的情况下没关系。刚刚在getView()方法中添加了一个 Random

  Random rnd  = new Random();

    if(position>=pics.length){
        position = rnd.nextInt(13);
    }
于 2013-03-08T14:12:10.377 回答