android - 下一个/上一个按钮幻灯片图像

标签 android image slide

当我想显示 image1 到 image6 时,我在上一个按钮上遇到错误,它显示错误 ArrayIndexOutOfBoundsException: length=6;指数=-1 如何设置当上一个按钮来到 image1 时不尝试显示 image 6 什么都不做?

这是我的代码

public class MainActivity extends Activity {
Intent i;
private int mNekiPhotoIndex =0;
private int[] mSlikeID = new int[] { R.drawable.pic,
        R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
        R.drawable.pic4, R.drawable.ic_launcher, };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     showPhoto(mNekiPhotoIndex);



    // 'Next' button.
        Button nextButton = (Button) findViewById(R.id.next_button);
        nextButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                mNekiPhotoIndex = (mNekiPhotoIndex + 1)
                        % mSlikeID.length;
                showPhoto(mNekiPhotoIndex);
            }
        });
     //  'Prev' button.
        Button prevButton = (Button) findViewById(R.id.prev_button);
        prevButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mNekiPhotoIndex = (mNekiPhotoIndex - 1)
                        % mSlikeID.length;
                showPhoto(mNekiPhotoIndex);
            }
        });

}
@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("photo_index", mNekiPhotoIndex);
    super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    mNekiPhotoIndex = savedInstanceState.getInt("photo_index");
    showPhoto(mNekiPhotoIndex);
    super.onRestoreInstanceState(savedInstanceState);
}

private void showPhoto(int PhotoIndex) {

    // TODO Auto-generated method stub
    ImageView imageView = (ImageView) findViewById(R.id.image_view);
    imageView.setImageResource(mSlikeID[PhotoIndex]);
}

最佳答案

mNekiPhotoIndex = ((mNekiPhotoIndex - 1) >= 0 ? (mNekiPhotoIndex - 1) : 0) % mSlikeID.length;

关于android - 下一个/上一个按钮幻灯片图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994763/

相关文章:

android - 相机预览始终返回 320x240 位图

java - 在回调监听器中设置 imageview 位图

android - 无法在 xml 中对齐按钮和 textView

image - 为什么在matlab中读取图像时必须使用double?

c++ - 来自 std::vector<std::uint32_t> 保存数组中原始 RGB 值的 CBitmap

javascript - 如何使信息框从悬停链接的右侧滑出?

android - 查看DragHelper : how to use it?

android - 如何在 Android gradle 构建过程中启用 fstack-protector

c - 如何发现从一种格式转换为另一种格式的图像?

jquery - 寻找一种在 div 之间滑动底部边框的方法