点击图片交换问题。
到现在只能点击交换完成一次,然后就再也不能点击了。麻烦大家帮忙看一下 哪里出了问题 ,
package com.example.game_pintu.PintuView; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.location.OnNmeaMessageListener; import android.text.Layout; import android.util.AttributeSet; import android.util.TypedValue; import android.view.View; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.RelativeLayout; import com.example.game_pintu.R; import com.example.game_pintu.game.utils.imagePiece; import com.example.game_pintu.game.utils.imageSpiliter; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.locks.ReadWriteLock; public class PintuLayout extends RelativeLayout implements View.OnClickListener { private int mColumn=3; private int mPadding;//容器的内边距 private int mMargin=3; //每张小图的横纵距离dp private ImageView[] mGamePintuItems; private int mItemWidth; private Bitmap mBitmap; //游戏的图片 private List<imagePiece> mItemBitmaps; private boolean once; private int mWidth;//游戏面板的宽度 private boolean isGameSuccess; private boolean isGameOver; public PintuLayout(Context context) { this(context,null); } public PintuLayout(Context context, AttributeSet attrs) { this(context, attrs,0); } public PintuLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs,defStyle); init(); } private void init() { mMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics()); mPadding = min(getPaddingLeft(), getPaddingRight(), getPaddingTop(), getPaddingBottom()); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 取宽和高中的小值 mWidth = Math.min(getMeasuredHeight(), getMeasuredWidth()); if(!once){ initBitmap(); initItem();//设置ImageView(Item)的宽高等属性 once=true; } setMeasuredDimension(mWidth, mWidth); } //进行切图,以及排序 private void initBitmap() { if(mBitmap==null){ mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.kaolatwo); } mItemBitmaps = imageSpiliter.splitImage(mBitmap, mColumn); //使用sort完成乱序 Collections.sort(mItemBitmaps, new Comparator<imagePiece>(){ @Override public int compare(imagePiece a, imagePiece b) { return Math.random()>0.5 ? 1: -1; } }); } //设置ImageView(Item)的宽高等属性 private void initItem() { mItemWidth = (mWidth - mPadding * 2 - mMargin * (mColumn - 1)) / mColumn; mGamePintuItems = new ImageView[mColumn * mColumn]; // 生成我们的Item,设置Rule for (int i = 0; i < mGamePintuItems.length; i++) { ImageView item = new ImageView(getContext()); item.setOnClickListener(this); item.setImageBitmap(mItemBitmaps.get(i).getBitmap()); mGamePintuItems[i] = item; item.setId(i + 1); //在Item的tag中存储了index item.setTag(i + "_" + mItemBitmaps.get(i).getIndex()); RelativeLayout.LayoutParams ip = new RelativeLayout.LayoutParams( mItemWidth, mItemWidth); //不是最后一列 if ((i + 1) % mColumn != 0) { ip.rightMargin = mMargin; } //不是第一例 if (i % mColumn != 0) { ip.addRule(RelativeLayout.RIGHT_OF, mGamePintuItems[i - 1].getId()); } //如果不是第一行设置topMargin和Rule if ((i + 1) > mColumn) { ip.topMargin = mMargin; ip.addRule(RelativeLayout.BELOW, mGamePintuItems[i - mColumn].getId()); } addView(item, ip); } } /* 获取多个参数的最小值 */ private int min(int... params) { int min = params[0]; for (int param : params) { if (param < min) min = param; } return min; } private ImageView mFirst;//第一次点击 private ImageView mSecond;//第二次点击 @Override public void onClick(View v) { if (isAniming) return; //两次点击同一个Item if(mFirst==v){ mFirst.setColorFilter(null); mFirst = null; return; } if(mFirst==null){ mFirst = (ImageView) v; mFirst.setColorFilter(Color.parseColor("#55FF0000")); }else{ mSecond = (ImageView) v; // 交换我们的Item exchangeView(); } } /* 动画层 */ private RelativeLayout mAnimLayout; private boolean isAniming; /* 交换我们的Item */ private void exchangeView() { mFirst.setColorFilter(null); // 构造我们的动画层 setUpAnimLayout(); ImageView first = new ImageView(getContext()); final Bitmap firstBitmap = mItemBitmaps.get( getImageIdByTag((String) mFirst.getTag())).getBitmap(); first.setImageBitmap(firstBitmap); LayoutParams ip = new LayoutParams(mItemWidth, mItemWidth); ip.leftMargin = mFirst.getLeft() - mPadding; ip.topMargin = mFirst.getTop() - mPadding; first.setLayoutParams(ip); mAnimLayout.addView(first); ImageView second = new ImageView(getContext()); final Bitmap secondBitmap = mItemBitmaps.get( getImageIdByTag((String) mSecond.getTag())).getBitmap(); second.setImageBitmap(secondBitmap); LayoutParams ip2 = new LayoutParams(mItemWidth, mItemWidth); ip2.leftMargin = mSecond.getLeft() - mPadding; ip2.topMargin = mSecond.getTop() - mPadding; second.setLayoutParams(ip2); mAnimLayout.addView(second); //设置动画 TranslateAnimation anim = new TranslateAnimation(0, mSecond.getLeft() - mFirst.getLeft(), 0, mSecond.getTop() - mFirst.getTop()); anim.setDuration(300); anim.setFillAfter(true); first.startAnimation(anim); TranslateAnimation animSecond = new TranslateAnimation(0, -mSecond.getLeft() + mFirst.getLeft(), 0, -mSecond.getTop() + mFirst.getTop()); animSecond.setDuration(300); animSecond.setFillAfter(true); second.startAnimation(animSecond); //监听动画 anim.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { mFirst.setVisibility(View.INVISIBLE); mSecond.setVisibility(View.INVISIBLE); isAniming = true; } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { String firstTag = (String) mFirst.getTag(); String secondTag = (String) mSecond.getTag(); mFirst.setImageBitmap(secondBitmap); mSecond.setImageBitmap(firstBitmap); mFirst.setTag(secondTag); mSecond.setTag(firstTag); mFirst.setVisibility(View.VISIBLE); mSecond.setVisibility(View.VISIBLE); mFirst = mSecond = null; mAnimLayout.removeAllViews(); // 判断用户游戏是否成功 } }); } /* 根据Tga获取Id */ public int getImageIdByTag(String tag) { String[] split = tag.split("_"); return Integer.parseInt(split[0]); } public int getImageIndexByTag(String tag) { String[] split = tag.split("_"); return Integer.parseInt(split[1]); } /* 构建我们的动画层 */ private void setUpAnimLayout() { if (mAnimLayout == null) { mAnimLayout = new RelativeLayout(getContext()); addView(mAnimLayout); } } }
这是到现在的代码