0

我有一个 ImageButton,它的背景设置如下:

final ImageButton[][] gridButton = new ImageButton[5][5];
gridButton[0][0] = (ImageButton) findViewById(R.id.imageButton1);
gridButton[0][0].setImageResource(ButtonIcon.l[0]); //ButtonIcon.l[0] is an image ID from my resources folder.

gridButton[0][0].setOnClickListener(new View.OnClickListener() {
        ButtonIcon justAnObject = new ButtonIcon();
        @Override
        public void onClick(View v) {
            int newId = justAnObject.changeIconId(((BitmapDrawable)gridButton[0][0].getDrawable()).getBitmap());
//          int newId = justAnObject.changeIconId(gridButton[0][0].getDrawable());
            gridButton[0][0].setImageResource(newId);

        }
    });

在 buttonIcon 类中,我有一个方法进行比较:

public int changeIconId(Bitmap object){
    if (object == ((BitmapDrawable) getResources().getDrawable(R.drawable.load0)).getBitmap()){
        return R.drawable.load1;
    }
    else return R.drawable.straight0;
}

所有 ID 都引用了我的资源文件夹中的图像。我不太确定如何解决这个问题。

谢谢。

4

1 回答 1

2

比较两个位图

public boolean equals(Bitmap bitmap1, Bitmap bitmap2) {
    ByteBuffer buffer1 = ByteBuffer.allocate(bitmap1.getHeight() * bitmap1.getRowBytes());
    bitmap1.copyPixelsToBuffer(buffer1);

    ByteBuffer buffer2 = ByteBuffer.allocate(bitmap2.getHeight() * bitmap2.getRowBytes());
    bitmap2.copyPixelsToBuffer(buffer2);

    return Arrays.equals(buffer1.array(), buffer2.array());
}

*更新后*

比较两个可绘制对象

Bitmap bitmap = ((BitmapDrawable)fDraw).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)sDraw).getBitmap();

if(bitmap == bitmap2)
    {
        //Code blcok
    }
于 2013-04-06T05:19:27.170 回答