按老师教的,结果一点击就崩溃
public void onClick(View v) {
//两次点击同一个图片
if(mFirst == v){
mFirst.setColorFilter(null);
mFirst = null;
return;
}
else if(mFirst ==null){
mFirst = (ImageView) v;
//点击时出现的颜色变化特效
mFirst.setColorFilter(Color.parseColor("55FF0000"));
}
else{
mSecond = (ImageView) v;
exchangeView();
}
}
//交换Item
private void exchangeView() {
mFirst.setColorFilter(null);
//Tag中存储了item的index
String firstTag = (String) mFirst.getTag();
String secondTag = (String) mSecond.getTag();
//split方法会在每个给的参数("_")处将原字符串分割为前后两个新的字符串,该参数不包含在新的字符串中
String[] firstParams = firstTag.split("_");
String[] secondParams = secondTag.split("_");
//获取mFirst的Bitmap
Bitmap firstBitmap = mItemBitmap.get(Integer.parseInt(firstParams[0])).getBitmap();
mSecond.setImageBitmap(firstBitmap);
Bitmap secondBitmap = mItemBitmap.get(Integer.parseInt(secondParams[0])).getBitmap();
mFirst.setImageBitmap(secondBitmap);
//交换Tag
mFirst.setTag(secondTag);
mSecond.setTag(firstTag);
mFirst=mSecond=null;
}