-1

我是 android 的初学者..我开发了一个应用程序,例如,如果我按下 prev 和 next 按钮,它将显示上一个和下一个图像....

但是我的应用程序有问题。这里的问题是,如果应用程序执行,我将当前图像源设置为IMAGE_IDS[0] ...所以如果我按下左箭头应用程序会强制关闭,实际上如果我按下左箭头它显示数组 IMAGE_IDS中的最后一个图像...知道吗???谢谢你 !

  public class MainActivity extends Activity  implements OnClickListener{

    ImageButton play;

    ImageButton ib_left_arrow, ib_right_arrow;  

    ImageView slidingimage;

    Animation rotateimage;  


    private int[] IMAGE_IDS = { R.drawable.image1, R.drawable.image2,
        R.drawable.image3};

    int imglength=IMAGE_IDS.length;

    int img_position;

    int img_minus;

    int img_plus;


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


    ib_left_arrow = (ImageButton) findViewById(R.id.iv_left_arrow);
    ib_right_arrow = (ImageButton) findViewById(R.id.iv_right_arrow);

    ib_left_arrow.setOnClickListener(this);
    ib_right_arrow.setOnClickListener(this);

    slidingimage = (ImageView) findViewById(R.id.ImageView3_Left);
    slidingimage.setImageResource(IMAGE_IDS[0]);

}



@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


switch (v.getId()) {

case R.id.iv_left_arrow:


    img_minus=--img_position;

    slidingimage.setImageResource(IMAGE_IDS[img_minus% IMAGE_IDS.length]);

    rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);

    slidingimage.startAnimation(rotateimage);

    break;



case R.id.iv_right_arrow:

    img_plus=++img_position;

    slidingimage.setImageResource(IMAGE_IDS[img_plus% IMAGE_IDS.length]);

    rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);

    slidingimage.startAnimation(rotateimage);


    break;              


    }
  }


 }
4

3 回答 3

1

我认为问题在于img_plus% IMAGE_IDS.length在数组中设置图像位置时。

我会尝试:

@Override
protected void onCreate(Bundle savedInstanceState) {
    img_position = 0;
    imglength = IMAGE_IDS.length;
    slidingimage.setImageResource(IMAGE_IDS[img_position]);

// ...
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub


    switch (v.getId()) {

        case R.id.iv_left_arrow:

            img_position--;    
            break;

        case R.id.iv_right_arrow:

            img_position++;
            break;

    }

    // this is to check if your current position is out of array bounds, you could
    // handle here your exit if you want to close the app when pressing left and 
    // img_position = 0 || img_position < 0
    if(img_position < 0) img_position = imglength-1;
    if(img_position >= imglength) img_position = imglength -1;

    slidingimage.setImageResource(IMAGE_IDS[img_position]);
于 2013-05-31T12:30:59.690 回答
0

图像= 0;

  case R.id.iv_left_arrow:

        if(0<img_position){        
        --img_position;

        slidingimage.setImageResource(IMAGE_IDS[img_position]);

        rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_left);

        slidingimage.startAnimation(rotateimage);}

        break;



    case R.id.iv_right_arrow:
    if(2>=counter)
{ 
        ++img_position;

        slidingimage.setImageResource(IMAGE_IDS[img_position]);

        rotateimage = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);

        slidingimage.startAnimation(rotateimage);}


        break; 
于 2013-05-31T12:31:21.610 回答
0

它的发生是因为这条线

img_minus=--img_position;

而这个计算

img_plus % IMAGE_IDS.length

如果它是 0 那么

img_minus=--img_position;//img_minus = --0 therefore img_minus = -1

//img_plus % IMAGE_IDS.length // -1 % 3 = 2
slidingimage.setImageResource(IMAGE_IDS[2]);//which is the last image.

因此,如果您想在按下左键的情况下关闭活动,那么

img_minus=--img_position;
if(img_minus < 0({
    finish();
    return;
}
于 2013-05-31T12:37:10.143 回答