为了账号安全,请及时绑定邮箱和手机立即绑定

麻烦老师看一下哪出问题了,crash了

54cc3b3d0001a11105000460.jpg

package com.pintu.activity.selfui;


import java.util.Collection;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.concurrent.Exchanger;


import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Color;

import android.graphics.ColorFilter;

import android.util.AttributeSet;

import android.util.TypedValue;

import android.widget.ImageView;

import android.widget.RelativeLayout;


import com.example.caige.R;

import com.pintu.activity.util.ImagePiece;

import com.pintu.activity.util.ImageSplitterUtil;


import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.Animation.AnimationListener;

import android.view.animation.TranslateAnimation;

public class GamePintuLayout extends RelativeLayout implements OnClickListener {


  private int mColumn=3;

  //内边距

  private int mPadding;

  //间隙dp

  private int mMagin=3;

  

  private ImageView[] mGamePintuItems;

  private int mItemWidth;

  

  private Bitmap mBitmap;

  

  private List<ImagePiece>mItemBitmaps;

  

  private boolean once;

  //游戏面板宽度

  private int mWidth;

  

  

  

public GamePintuLayout(Context context) {

this(context,null);

// TODO Auto-generated constructor stub

}

public GamePintuLayout(Context context, AttributeSet attrs) {

this(context,attrs,0);

// TODO Auto-generated constructor stub

}


public GamePintuLayout(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

// TODO Auto-generated constructor stub

   init();

}



private void init() {

// TODO Auto-generated method stub

mMagin=(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,3,

getResources().getDisplayMetrics());

mPadding=min(getPaddingLeft(),getPaddingRight(),

getPaddingTop(),getPaddingBottom());

}


  @Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// TODO Auto-generated method stub

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    

mWidth=Math.min(getMeasuredHeight(),getMeasuredWidth());

if(!once){

//进行切图,排序

  initBitmap();

  //设置ImageView(Item)的宽高

  initItem();

  once=true;

}

setMeasuredDimension(mWidth,mWidth);

  }

  

  

  //切图,排序

private void initBitmap() {

// TODO Auto-generated method stub

if(mBitmap==null){

mBitmap=BitmapFactory.decodeResource(getResources(),

R.drawable.image);

}

mItemBitmaps=ImageSplitterUtil.splitImage(mBitmap,mColumn);


Collections.sort(mItemBitmaps, new Comparator<ImagePiece>() {


@Override

public int compare(ImagePiece a, ImagePiece b) {

//乱序

return Math.random()>0.5?1:-1;

}

});

}


private void initItem(){

mItemWidth=(mWidth-mPadding*2-mMagin*(mColumn-1))/mColumn;

   

mGamePintuItems=new ImageView[mColumn*mColumn];

//生成Item

  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.setTag(i+"_"+mItemBitmaps.get(i).getIndex());

  

  RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(

  mItemWidth,mItemWidth);

  

  

  //不是最后一列,设置横向间距

             

  if((i+1)%mColumn!=0){

  lp.rightMargin=mMagin;

  }

  

  //不是第一列

  if(i%mColumn!=0){

  lp.addRule(RelativeLayout.RIGHT_OF,

  mGamePintuItems[i-1].getId());

  }

  

  //不是第一行

  if((i+1)>mColumn){

  lp.topMargin=mMagin;

  lp.addRule(RelativeLayout.BELOW,mGamePintuItems[i-mColumn].getId());

  

  }

  

  addView(item,lp);

}

}



private int min(int...params) {

// TODO Auto-generated method stub

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(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;

//交换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 lp=new LayoutParams(mItemWidth,mItemWidth);

lp.leftMargin=mFirst.getLeft()-mPadding;

lp.topMargin=mFirst.getTop()-mPadding;

first.setLayoutParams(lp);

mAnimLayout.addView(first);

ImageView second=new ImageView(getContext());

final Bitmap secondBitmap=mItemBitmaps.get(getImageIdByTag((String)mSecond.getTag())).getBitmap();

second.setImageBitmap(secondBitmap);

LayoutParams lp2=new LayoutParams(mItemWidth,mItemWidth);

lp2.leftMargin=mSecond.getLeft()-mPadding;

lp2.topMargin=mSecond.getTop()-mPadding;

second.setLayoutParams(lp2);

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 AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

// TODO Auto-generated method stub

mFirst.setVisibility(View.INVISIBLE);

mSecond.setVisibility(View.INVISIBLE);

}

@Override

public void onAnimationRepeat(Animation animation) {

// TODO Auto-generated method stub

}

@Override

public void onAnimationEnd(Animation animation) {

// TODO Auto-generated method stub


//String firstTag=(String) mFirst.getTag();

// secondTag=(String) mSecond.getTag();

  // String[] firstParams=firstTag.split("_");

  // String[] secondParams=secondTag.split("_");

   

   //Bitmap firstBitmap= mItemBitmaps.get(Integer.parseInt(firstParams[0])).getBitmap();

   //Bitmap secondBitmap= mItemBitmaps.get(Integer.parseInt(secondParams[0])).getBitmap();

   String firstTag=(String) mFirst.getTag();

   String secondTag=(String) mSecond.getTag();

   

   

  

  

  // String[] firstParams=firstTag.split("_");

//String[] secondParams=secondTag.split("_"); 

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();


}

});

   

   

   

   

}

//根据tag获取id

public int getImageIdByTag(String tag){

String[] split= tag.split("_");

return Integer.parseInt(split[0]);

}


public int getImageIndex(String tag){

String[] split= tag.split("_");

return Integer.parseInt(split[1]);

}

//构造动画层

private void setUpAnimLayout() {

// TODO Auto-generated method stub

if(mAnimLayout==null)

{

mAnimLayout=new RelativeLayout(getContext());

addView(mAnimLayout);

}

}





}



正在回答

5 回答

你好,关闭下硬件加速试试,例如:<activity android:hardwareAccelerated="false" ...> 。 

0 回复 有任何疑惑可以回复我~

采摘@两页深海贝壳

竟然解决了,望有同样问题的同学,可以看看

把mAnimLayout.removeAllViews()放在onAnimationEnd()中有的手机可以报错。可以换成

if (mAnimLayout == null) {
mAnimLayout = new RelativeLayout(getContext());
addView(mAnimLayout);
} else {
mAnimLayout.removeAllViews();
}

0 回复 有任何疑惑可以回复我~

我也是这样的问题,求解答求解答~~

0 回复 有任何疑惑可以回复我~

补充一下54cdbc2e0001408005000232.jpg

0 回复 有任何疑惑可以回复我~

把log也粘上来啊,只有源码不好找,话说我的也崩了,报的空指针目前还在找哪错了。。

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Android美女拼图小游戏
  • 参与学习       25697    人
  • 解答问题       97    个

本视频教程将带领大家通过自定义控件实现一个拼图游戏

进入课程

麻烦老师看一下哪出问题了,crash了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信