用Android 写休闲拼图游戏(一)

用Android 写休闲拼图游戏(一)_第1张图片

 自定义 MyGridView.class

 

package com.example.pintu;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

/**
 *
 * @author apple
 *
 */
public class MyGridView extends GridView {

    public MyGridView(Context context) {
        super(context);
    }

    public MyGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO 自动生成的构造函数存根
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO 自动生成的方法存根
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}

创建bean类  ImagePiece.class

package com.example.pintu;

import android.graphics.Bitmap;
/**
 * bean类
 * @author apple
 *
 */
public class ImagePiece {

    public int index = 0;  

    public Bitmap bitmap = null;  
}

 

创建 动画类 AnimationManger.class

package com.example.pintu;

import android.os.Message;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;

/**
 *
 * @author 作者 Your-Name: 张跃华
 *
 * @version 创建时间:2019年4月17日 下午5:20:31
 *
 *          类说明
 *
 */
public class AnimationManger {
    
    // 缩放动画
    static ScaleAnimation anim3;
    
    
    
    
    
    

    /**
     * 制作 linearlayout的出场动画
     * @return
     */
    public static AnimationSet MakeLearAnimation() {
        AnimationSet animationSet = new AnimationSet(false);
        anim3 = new ScaleAnimation(0.9f, 1.0f, 0.9f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);// 缩放动画
        animationSet.addAnimation(anim3);
        animationSet.setDuration(500);
        animationSet.setFillAfter(false);// 动画执行结束时, 是否恢复初始状态
        return animationSet;
    }

//    /**
//     * 制作LinearLayout的收回动画
//     * @return
//     */
//    public static AnimationSet MakeLearAnimationDao() {
//        AnimationSet animationSet = new AnimationSet(false);
//        anim1 = new AlphaAnimation(1.0f, 0.0f);
//        anim2 = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 旋转动画
//        anim3 = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
//                0.5f);// 缩放动画
//        animationSet.addAnimation(anim1);
//        animationSet.addAnimation(anim2);
//        animationSet.addAnimation(anim3);
//        animationSet.setDuration(200);
//        animationSet.setFillAfter(true);// 动画执行结束时, 是否恢复初始状态
//        return animationSet;
//    }
//
//    /**
//     * 制作 button的 展开动画
//     * @return
//     */
//    public static AnimationSet MakeAnimationSet() {
//        AnimationSet animationSet = new AnimationSet(false);
//
//        anim1 = new AlphaAnimation(1.0f, 0.5f); // 透明度变化
        anim1.setDuration(500);
//        /**
//         * 旋转动画 参数1 : 开始角度 参数2 : 结束角度 参数3 : 当前旋转的X轴位置 基于父容器View 还是 基于当前View自己,
//         * 或者基于屏幕 (绝对值) 参数4 : X轴旋转中心点的值
//         *
//         */
//        anim2 = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 旋转动画
        anim2.setDuration(500);
//        /**
//         * 缩放动画: 参数1: 开始的X倍数 (取值范围 可以从0 - 无穷大 ) 0:没了 0.1:缩小为原来大小的十分之一 1.0:不变
//         * 2.0放大一倍 参数2: 结束的X倍数 参数3: 开始的Y倍数 参数4: 结束的Y倍数 参数5 : 当前缩放的X轴位置 基于父容器View
//         * 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数6 : X轴缩放中心点的值 参数7 : 当前缩放的Y轴位置 基于父容器View
//         * 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数8 : Y轴缩放中心点的值
//         */
//        anim3 = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
//                0.5f);// 缩放动画
        anim3.setDuration(500);
//        /**
//         * 平移动画: 参数1: 开始平移的X轴的位置 是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数2:
//         * 开始的具体的X点 参数3: 结束平移的X轴的位置是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数4:
//         * 结束的具体的X点 参数5: 开始平移的Y轴的位置 是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数6:
//         * 开始的具体的Y点 参数7: 结束平移的Y轴的位置是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数8:
//         * 结束的具体的Y点
//         */
//
//        anim4 = new TranslateAnimation(0, -400, 0, -700);
        anim4.setDuration(500);
//        animationSet.addAnimation(anim1);
//        animationSet.addAnimation(anim2);
//        animationSet.addAnimation(anim3);
//        animationSet.addAnimation(anim4);
//
//        anim1 = new AlphaAnimation(0.0f, 1.0f); // 透明度变化
        anim1.setDuration(500);
//        animationSet.addAnimation(anim1);
//         animationSet.setDuration(300);
//        // animationSet.setFillAfter(true);// 动画执行结束时, 是否恢复初始状态
//
//        animationSet.setAnimationListener(new AnimationListener() {
//
//            @Override
//            public void onAnimationStart(Animation animation) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                // TODO Auto-generated method stub
//                // logI("到这里了");
//                // Message mgs = handler.obtainMessage();
//                // mgs.obj = 1;
//                // handler.sendMessage(mgs);
//            }
//        });
//
//        return animationSet;
//
//    }
//
//    /**
//     * 制作 button的 收回动画
//     * @return
//     */
//    public static AnimationSet MakeAnimationSetDao() {
//        AnimationSet animationSet = new AnimationSet(false);
//
//        anim1 = new AlphaAnimation(1.0f, 0.1f); // 透明度变化
//        /**
//         * 旋转动画 参数1 : 开始角度 参数2 : 结束角度 参数3 : 当前旋转的X轴位置 基于父容器View 还是 基于当前View自己,
//         * 或者基于屏幕 (绝对值) 参数4 : X轴旋转中心点的值
//         *
//         */
//        anim2 = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);// 旋转动画
//        /**
//         * 缩放动画: 参数1: 开始的X倍数 (取值范围 可以从0 - 无穷大 ) 0:没了 0.1:缩小为原来大小的十分之一 1.0:不变
//         * 2.0放大一倍 参数2: 结束的X倍数 参数3: 开始的Y倍数 参数4: 结束的Y倍数 参数5 : 当前缩放的X轴位置 基于父容器View
//         * 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数6 : X轴缩放中心点的值 参数7 : 当前缩放的Y轴位置 基于父容器View
//         * 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数8 : Y轴缩放中心点的值
//         */
//        anim3 = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
//                0.5f);// 缩放动画
//        /**
//         * 平移动画: 参数1: 开始平移的X轴的位置 是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数2:
//         * 开始的具体的X点 参数3: 结束平移的X轴的位置是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数4:
//         * 结束的具体的X点 参数5: 开始平移的Y轴的位置 是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数6:
//         * 开始的具体的Y点 参数7: 结束平移的Y轴的位置是基于父容器View 还是 基于当前View自己, 或者基于屏幕 (绝对值) 参数8:
//         * 结束的具体的Y点
//         */
//        // anim4=new TranslateAnimation(Animation.RELATIVE_TO_PARENT,-0.4f ,
//        // Animation.RELATIVE_TO_PARENT,0.0f ,
//        // Animation.RELATIVE_TO_PARENT,-0.45f ,
//        // Animation.RELATIVE_TO_PARENT,0.0f );
//        anim4 = new TranslateAnimation(-400, 0, -700, 0);
//        animationSet.addAnimation(anim1);
//        animationSet.addAnimation(anim2);
//        animationSet.addAnimation(anim3);
//        animationSet.addAnimation(anim4);
//        animationSet.setDuration(500);
//        // animationSet.setFillAfter(true);// 动画执行结束时, 是否恢复初始状态
//
//        animationSet.setAnimationListener(new AnimationListener() {
//
//            @Override
//            public void onAnimationStart(Animation animation) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//                // TODO Auto-generated method stub
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                // Message mgs = handler.obtainMessage();
//                // mgs.obj = 1;
//                // handler.sendMessage(mgs);
//            }
//        });
//
//        return animationSet;
//
//    }
//
//    
//    
//    
//    public static AnimationSet MakeYidongAnimation(float startx,float starty,float endx,float endy){
//        AnimationSet animationSet = new AnimationSet(false);
//        TranslateAnimation anim = new TranslateAnimation(startx, endx, starty, endy);
//        animationSet.addAnimation(anim);
//        animationSet.setDuration(500);
//        return animationSet;
//    }
    
   
    
}

 

 

创建 图片切割类  ImageSplitter.class

package com.example.pintu;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.util.Log;

/**
 * 切割图片
 * @author apple
 *
 */
public class ImageSplitter {


    /**
     * 切割图片
     * @param bitmap  原图片
     * @param xPiece  图片横向x轴 被  xPiece等分
     * @param yPiece  图片纵向y轴 被  yPiece等分
     * @param size          图片宽高被等分后 计算子图片 宽 高
     * @return 字图片集合  list
     */
    public static List split(Bitmap bitmap, int xPiece, int yPiece,int size) {  

        List pieces = new ArrayList(xPiece * yPiece);  
        int width = bitmap.getWidth();  
        int height = bitmap.getHeight();  
        
        
        
        int pieceWidth = width / size;  
        int pieceHeight = height / size;  
        for (int i = 0; i < yPiece; i++) {  
            for (int j = 0; j < xPiece; j++) {  
                ImagePiece piece = new ImagePiece();  
                piece.index = j + i * xPiece;  
                int xValue = j * pieceWidth;  
                int yValue = i * pieceHeight;  
                piece.bitmap = Bitmap.createBitmap(bitmap, xValue, yValue,  
                        pieceWidth, pieceHeight);
                pieces.add(piece);  
            }  
        }  

        return pieces;  
    }  
    
}

 

 

创建 图片压缩裁剪类ImageTools.class

package com.example.pintu;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

public class ImageTools {
    
      /**
       * 通过uri获取图片并进行压缩
       *
       * @param uri
       * @param activity
       * @return
       * @throws IOException
       */
      public static Bitmap getBitmapFromUri(Uri uri, Activity activity) throws IOException {
        InputStream inputStream = activity.getContentResolver().openInputStream(uri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inDither = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        BitmapFactory.decodeStream(inputStream, null, options);
        inputStream.close();
    
        int originalWidth = options.outWidth;
        int originalHeight = options.outHeight;
        if (originalWidth == -1 || originalHeight == -1) {
          return null;
        }
    
        float height = 800f;
        float width = 480f;
        int be = 1; //be=1表示不缩放
        if (originalWidth > originalHeight && originalWidth > width) {
          be = (int) (originalWidth / width);
        } else if (originalWidth < originalHeight && originalHeight > height) {
          be = (int) (originalHeight / height);
        }
    
        if (be <= 0) {
          be = 1;
        }
        BitmapFactory.Options bitmapOptinos = new BitmapFactory.Options();
        bitmapOptinos.inSampleSize = be;
        bitmapOptinos.inDither = true;
        bitmapOptinos.inPreferredConfig = Bitmap.Config.ARGB_8888;
        inputStream = activity.getContentResolver().openInputStream(uri);
    
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, bitmapOptinos);
        inputStream.close();
    
        return compressImage(bitmap);
      }
    
      /**
       * 质量压缩方法
       *
       * @param bitmap
       * @return
       */
      public static Bitmap compressImage(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        int options = 100;
        while (byteArrayOutputStream.toByteArray().length / 1024 > 100) {
          byteArrayOutputStream.reset();
          //第一个参数 :图片格式 ,第二个参数: 图片质量,100为最高,0为最差 ,第三个参数:保存压缩后的数据的流
          bitmap.compress(Bitmap.CompressFormat.JPEG, options, byteArrayOutputStream);
          options -= 10;
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        Bitmap bitmapImage = BitmapFactory.decodeStream(byteArrayInputStream, null, null);
        return bitmapImage;
      }
    }

 

 

 

 

 布局  activity_pintu.xml

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
 
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.pintu.activity.PintuActivity" >

   

            android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:orientation="horizontal" >

                    android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/act_xc_but"
            android:layout_weight="1"
            android:text="相册获取" />

                    android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="100dp" />
              android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/act_yanzheng_but"
            android:layout_weight="1"
            android:text="验证" />
        
   
          android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

                    android:id="@+id/pintu_jiandan_but"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:text="简单" />

                    android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/pintu_zhongji_but"
            android:layout_weight="1"
            android:text="中级" />

                    android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:id="@+id/pintu_kunnan_but"
            android:layout_weight="1"
            android:text="困难" />
   
            android:layout_gravity="center"
        android:id="@+id/pintu_grod"
          android:gravity="center"
           android:horizontalSpacing="0dp"
        android:layout_width="400dp"
          android:layout_height="450dp"
        android:scrollbars="none" >
   

 

 

 

 

 

你可能感兴趣的:(用Android 写休闲拼图游戏(一))