注册 登录
编程论坛 Android开发

拼图不知道怎么继续,能不能给点思路?谢谢

笔墨痕干 发布于 2015-06-19 08:24, 3709 次点击
程序代码:
package com.example.androidtest;

import java.util.ArrayList;
import java.util.Collections;
import java. java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class gameView extends View {

    private Bitmap bg;
    private Bitmap background;//背景图片
    private Paint paint;
    private int screenW;
    private int screenH;
    private int pw;
    private int ph;
    public static Rect puzzRect;
    private Bitmap puzzImage;//拼图图片。
    private Rect thumbRect;//拼图缩放区域
    private static int line;
    private static int list;
    private List<PuzzleCell> bitImages=new ArrayList<PuzzleCell>();//小图片
    private List<Rect> RectPiece=new ArrayList<Rect>();
    public gameView(Context context) {
        super(context);
        paint=new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        line = gameActivity.line;
        list = gameActivity.list;
    }
    //画背景图,画格子,画小图片
    @Override
    protected void onDraw(Canvas canvas) {
        Set<Integer> indexs=new HashSet<Integer>();
        super.onDraw(canvas);
        canvas.drawBitmap(background,0, 0, null);
        canvas.drawRect(puzzRect,paint);
//        canvas.drawBitmap(puzzImage, null,puzzRect,null);
        canvas.drawBitmap(puzzImage, null,thumbRect,null);
        int value=(int)ph;
        int value1=(int)pw;
        for(int i=0;i< line;i++){
            canvas.drawLine(puzzRect.left, puzzRect.top+value*(i+1), puzzRect.right, puzzRect.top+value*(i+1), paint);
        }
        for(int i=0;i< list;i++){
            canvas.drawLine(puzzRect.left+value1*(i+1), puzzRect.top,puzzRect.left+value1*(i+1), puzzRect.bottom, paint);
        }
        makeRectPiece();//
        for(int i=bitImages.size()-1;i>=0;i--){
            PuzzleCell cell=bitImages.get(i);
            int count;
            count=(int)(Math.random()*(line*list));
            while(true){
            if(indexs.contains(count)==true){
                count=(int)(Math.random()*(line*list));
            }
            else{
                indexs.add(count);
                break;
            }
            }
            cell.draw(canvas,RectPiece.get(count));
           
        }
      
    }
    //获取切割的方格
    private void makeRectPiece() {
        // TODO Auto-generated method stub
        for(int i=0;i<line;i++){
            for(int j=0;j<list;j++){
                Rect r=new Rect(puzzRect.left+j*pw,puzzRect.top+i*ph,puzzRect.left+pw*(j+1),puzzRect.top+ph*(i+1));
                RectPiece.add(r);
            }
        }
    }

    @Override
    //画方格,画拼图区域,加载图片
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        // TODO Auto-generated method stub
        super.onSizeChanged(w, h, oldw, oldh);
      
        if(w>h){
            screenW=w;
            screenH=h;
            }
        else{
            screenW=h;
            screenH=w;
        }
      
        pw=(int) ((screenW-dip2px(10)-dip2px(10)-dip2px(10))/(list+1.5));
        ph=(screenH-dip2px(20)-dip2px(20))/line;
        puzzRect = new Rect(dip2px(10),dip2px(20),dip2px(10)+(int)(list*pw),dip2px(20)+(int)(line*ph));
      
        thumbRect=new Rect(puzzRect.width()+dip2px(10)+dip2px(10),dip2px(20),screenW-dip2px(10),dip2px(20)+ph);
      
      
        bg=BitmapFactory.decodeResource(getResources(),R.drawable.wallpaper);//加载背景图片
        background=Bitmap.createScaledBitmap(bg,screenW, screenH, false);//设置背景图片,并且缩放
        bg.recycle();//释放原始拼图图像。
      
        Bitmap pic=BitmapFactory.decodeResource(getResources(), R.drawable.pic02);//加载拼图图片
        puzzImage = Bitmap.createScaledBitmap(pic, puzzRect.width(), puzzRect.height(), false);
      
        pic.recycle();
        splitImage();
      
    }
    //切割图片
    private void splitImage() {
        // TODO Auto-generated method stub
        Set<Integer> indexs=new HashSet<Integer>();
        Rect puzzR;
        for(int i=0;i<line;i++){
            for(int j=0;j<list;j++){
                puzzR=new Rect(j*pw,i*ph,(j+1)*pw,(i+1)*ph);
                PuzzleCell cell=new PuzzleCell();
                cell.image=Bitmap.createBitmap(puzzImage, puzzR.left, puzzR.top, puzzR.width(), puzzR.height());
                cell.x0=puzzRect.left+j*pw;
                cell.y0=puzzRect.top+i*ph;
                bitImages.add(cell);
            }
    }
        Collections.sort(bitImages,new Comparator<PuzzleCell>(){

            @Override
            public int compare(PuzzleCell arg0, PuzzleCell arg1) {
                // TODO Auto-generated method stub
                return arg0.index-arg1.index;
            }
           
        });
    }
    //像素转化。
    private int dip2px(float dip){
        final float scale=getResources().getDisplayMetrics().density;
        return (int)(dip*scale+0.5);
      
    }
    //实现触摸功能。
    public boolean onTouchEvent(MotionEvent event){
        //获取触摸的动作,和触摸的坐标.
        int act=event.getAction();
        int x=(int)event.getX();
        int y=(int)event.getY();
        //根据触摸的动作,做出相应的处理
        switch(act){
        case MotionEvent.ACTION_DOWN:
            for(int i=0;i<bitImages.size();i++){
                PuzzleCell cell=bitImages.get(i);
                if(cell.isTouched(x, y)){
               
                }
            }
        }
        return false;
      
    }
  
初学android,看了课本上的拼图,自己想写个,但是不知道如何实现触摸功能,看网上的实现触摸功能的做法,他们设为ImageView,在网上看了一会也不会用,所以请问下,按照我写的能否实现触摸功能呢?
3 回复
#2
邓士林2015-07-09 12:21
在 public boolean onTouchEvent(MotionEvent event){
        //获取触摸的动作,和触摸的坐标.
        int act=event.getAction();
        int x=(int)event.getX();
        int y=(int)event.getY();
        //根据触摸的动作,做出相应的处理
        switch(act){
        case MotionEvent.ACTION_DOWN:
            for(int i=0;i<bitImages.size();i++){
                PuzzleCell cell=bitImages.get(i);
                if(cell.isTouched(x, y)){
               
                }
            }
        }
        return false;
      
    }
的MotionEvent.Move中判断
#3
笔墨痕干2015-07-16 12:48
回复 2楼 邓士林
这是移动了把!!谢谢
#4
hijobs招手2015-08-27 09:49
private void splitImage() {
        // TODO Auto-generated method stub
        Set<Integer> indexs=new HashSet<Integer>();
        Rect puzzR;
        for(int i=0;i<line;i++){
            for(int j=0;j<list;j++){
                puzzR=new Rect(j*pw,i*ph,(j+1)*pw,(i+1)*ph);
                PuzzleCell cell=new PuzzleCell();
                cell.image=Bitmap.createBitmap(puzzImage, puzzR.left, puzzR.top, puzzR.width(), puzzR.height());
                cell.x0=puzzRect.left+j*pw;
                cell.y0=puzzRect.top+i*ph;
                bitImages.add(cell);
是移动招聘么?
1