博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GamePinTu
阅读量:6289 次
发布时间:2019-06-22

本文共 6201 字,大约阅读时间需要 20 分钟。

package com.example.administrator.pintu;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.util.AttributeSet;import android.util.TypedValue;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;import java.util.Collections;import java.util.Comparator;import java.util.List;/** * Created by Zyh on 2016/11/22. */public class GamePinTuLayout extends RelativeLayout implements View.OnClickListener {    //拼图layout的宽度,游戏面板的宽度    private int mWidth;    //面板内边距    private int mPadding;    //piece图片的间距    private int mMargin;    public GamePinTuLayout(Context context) {        this(context, null);    }    public GamePinTuLayout(Context context, AttributeSet attrs) {        super(context, attrs);//        //设置背景图片//        setBackgroundResource(R.mipmap.chazhuo);        mPadding = min(getPaddingLeft(), getPaddingRight(), getPaddingTop(), getPaddingBottom());        //安卓里面的单位:dip、dp、px、sp        mMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());    }    private boolean once = true;    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //设置面板的宽度        mWidth = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));        //切图乱序,调试间距,将乱序后的imagepiece集合加入面板        if (once) {            initBitmap();            initItem();        }        setMeasuredDimension(mWidth, mWidth);    }    private int mItemWidth;    /**     * 1.重新规定imageView的宽高     * 2.位置需要手动规定     */    private ImageView[] mGamePinTuItems;    private void initItem() {//        //容器我们可以直接使用addView方法将一个view加入容器中//        ImageView item=new ImageView(getContext());//        item.setImageBitmap(mItemBitmap.get(0).bitmap);//        addView(item);        /**         * mWidth游戏面板宽度,面板宽度mPadding,内容边距mMargin         * RelativeLayout.LayoutParams--规定加入控件的宽高和位置属性(addRule);         *         */        RelativeLayout.LayoutParams lp;        mItemWidth=(mWidth-mPadding*2-mMargin*(mPiece-1))/mPiece;        mGamePinTuItems=new ImageView[mPiece*mPiece];        ImagePiece imagePiece;        //当前布局是相对布局        for (int i=0;i
=mPiece){ lp.addRule(RelativeLayout.BELOW,mGamePinTuItems[i-mPiece].getId()); lp.topMargin=mMargin; } addView(item,lp); } } List
mItemBitmap; //游戏的图片 private Bitmap mBitmap; private int mPiece=3; private void initBitmap() { if (mBitmap==null){ //从资源文件中取出图片并且转成bitmap图片 mBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.chazhuo); } //分割图片 mItemBitmap=ImageSplitterUtils.splitImage(mBitmap,mPiece); //通过Collections可以帮助我们完成乱序,sort进行,它里面有两个参数,排序集合排序规则 Collections.sort(mItemBitmap, new Comparator
() { @Override public int compare(ImagePiece imagePiece, ImagePiece t1) { //随机返回1或者-1,达到乱序的效果, return Math.random()>0.5?1:-1; // Math.random();--生成随机数,返回值是double,范围是0.0-1.0 } }); } /** * 可变参数的写法:int...params * 那么params他就是一个参数数组,这里规定他的类型就是一个int型的 * * @return */ private int min(int... params) { int min = params[0]; for (int param : params) { if (min > param) { min = param; } } return min; } private ImageView mFirstIv; private ImageView mSecondIv; @Override public void onClick(View view) { //点击事件// Toast.makeText(getContext(), "----"+view.getId(), Toast.LENGTH_SHORT).show(); if (mFirstIv==null){ mFirstIv= (ImageView) view; //color.red getColor方法获取资源文件color 十六进制的写法 Color.parseColor("#0000000") mFirstIv.setColorFilter(Color.parseColor("#55ff0000")); }else if (mFirstIv==view){ mFirstIv.setColorFilter(null); mFirstIv=null; }else { //发生交换 mSecondIv= (ImageView) view; exchangeView(); } } private Bitmap mFirstBitmap,mSecondBitmap; /** * 作交换,做动画 */ private void exchangeView() { mFirstIv.setColorFilter(null); String firstTag=mFirstIv.getTag().toString(); String secondTag=mSecondIv.getTag().toString(); mFirstBitmap=mItemBitmap.get(getImageIdByTag(mFirstIv.getTag().toString())).bitmap; mSecondBitmap=mItemBitmap.get(getImageIdByTag(mSecondIv.getTag().toString())).bitmap; //交换bitmap,并且交换tag mFirstIv.setImageBitmap(mSecondBitmap); mSecondIv.setImageBitmap(mFirstBitmap); mFirstIv.setTag(secondTag); mSecondIv.setTag(firstTag); mFirstIv=mSecondIv=null; checkGameOver(); } private void checkGameOver() { boolean isOver=true; for (int i=0;i
package com.example.administrator.pintu;import android.graphics.Bitmap;import java.util.ArrayList;import java.util.List;/** * 分割图片,并且将图片放在 * Created by Zyh on 2016/11/22. */public class ImageSplitterUtils {    /**     * 将目标图片进行分割,成piece块,然后存入集合当中返回出去     * @param bitmap     * @param piece     * @return     */    public static List
splitImage(Bitmap bitmap,int piece){ List
mList=new ArrayList<>(); //拿到图片的宽度和高度 int width=bitmap.getWidth(); int height=bitmap.getHeight(); width=Math.min(width,height); //计算出每一个piece的宽度 int pieceWith=width/piece; ImagePiece temImagePiece; for (int i=0;i
package com.example.administrator.pintu;import android.graphics.Bitmap;/** * Created by Zyh on 2016/11/22. */public class ImagePiece {    //存放图片和分割的序号    public  int index;    public Bitmap bitmap;    public ImagePiece() {    }    public ImagePiece(int index, Bitmap bitmap) {        this.index = index;        this.bitmap = bitmap;    }}

 

转载于:https://www.cnblogs.com/zhaiyaohua/p/6090406.html

你可能感兴趣的文章