如何在点击时更改按钮的图像?

时间:2015-03-08 18:15:02

标签: java android image bitmap imagebutton

我有一系列位图ArrayList<Bitmap> images = new ArrayList<>(3),需要在每次点击后更改ImageButton的图片,然后点击images(0)后再点击image(1) image(2)

为此,我使用myImageButton.setImageBitmap(images.get(0))作为第一张图片,如何更改为下一张图片,然后更改为第三张图片?

2 个答案:

答案 0 :(得分:0)

你应该使用

int currentPos = 0;
onclick(){
 currentPos = (currentPos+1)%(images.size()-1)
 myImageButton.setImageBitmap(images.get(currentPos))
}

答案 1 :(得分:0)

您可以使用

int currentPos = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        myImageButton.setImageBitmap(images.get(0))
}


@Override
public void onClick(View v){
    currentPos++;
    if(currentPos == 2){
        // if the imagebutton has the bitmap from position 2, you can do what you want
        //example:
        currentPos = 0;// so you have the first Bitmap you had when the user didn't click on the ImageButton
    }
    myImageButton.setImageBitmap(images.get(currentPos));
}