java - Activity 重新启动后功能发生变化

标签 java android drag-and-drop collision-detection

我有一个益智游戏,其中的棋子在屏幕上拖动但不能重叠。如果它们尝试重叠,它们的位置将更改回未重叠的位置,并使用 invalidate() 重新绘制 UI。除非 Activity 被销毁并重建(例如关闭应用程序并重新启动或更改方向时),否则这种方法效果很好。

似乎发生的情况是,我用于碰撞的位置变量(x、y、right、bottom 等)没有重置为它们在构造函数中初始化的方式。 fragment 与不可见的物体碰撞,捕捉到看似随机的位置,或者不稳定地移动。

修复它的唯一方法是手动终止该应用程序(例如使用任务 killer )或重新安装它。然后它将正常工作,直到第二次创建游戏 Activity 。我究竟做错了什么?有任何想法吗?以下是如何将 fragment 添加到我的 GameView 类中的 onCreate() 中:

Pieces redtiki = new Pieces(this,0,0,R.drawable.tikired);
...
board.addView(redtiki);

这是我的 Pieces 类的一部分:

public class Pieces extends View{

private int x;
private int y;
private int width;
private int height;
private int right;
private int bottom;
private int initialX;
private int initialY;
private Bitmap sizedBitmap;
private Bitmap sourceBitmap;
private int boardSize = 6;
public static ArrayList<Pieces> aPieces = new ArrayList<Pieces>();
//private final Paint mPaint = new Paint();

public Pieces(Context context, int x, int y, int img){
    super(context);
    this.x = x;
    this.y = y;
    sourceBitmap = BitmapFactory.decodeResource(getResources(), img);
    aPieces.add(this);
    initialX=x;
    initialY=y;
}

private void sizePiece(){
    int scaledWidth;
    int scaledHeight;
    int h = sourceBitmap.getHeight();
    int w = sourceBitmap.getWidth();

    if (h>w){
        scaledWidth = 1;
    }else if (w>h){
        scaledWidth = w/h;
    }else{
        scaledWidth = 0;
    }
    if (h>w){
        scaledHeight = h/w;
    }else if (w>h){
        scaledHeight = 1;
    }else{
        scaledHeight = 0;
    }

    int dstWidth = (((((View) getParent()).getWidth())*scaledWidth)/boardSize)-1;//TODO make sure that -1 is necessary for
    int dstHeight = (((((View) getParent()).getHeight())*scaledHeight)/boardSize)-1;//fitting all pieces on the board

    sizedBitmap = Bitmap.createScaledBitmap(sourceBitmap, dstWidth, dstHeight, true);
    width = sizedBitmap.getWidth();
    height = sizedBitmap.getHeight();
    right = x+width;
    bottom = y+height;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    sizePiece();
    canvas.drawBitmap(sizedBitmap, x, y, null);
}

@Override
public boolean onTouchEvent(MotionEvent event){

    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        //check if touch is on piece
        if (eventX > x && eventX < (x+width) && eventY > y && eventY < (y+height)){
            initialX=x;
            initialY=y;
            break;
        }else{
            return false;
        }
    case MotionEvent.ACTION_MOVE:
        //determine if piece should move horizontally or vertically
        if(width>height){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a horizontal collision
                if(this.isAllignedHorizontallyWith(piece)){
                    //check for and handle collisions while moving left
                    if(this.isRightOf(piece)){
                        if(eventX>piece.right+(width/2)){
                            x = (int)(eventX-(width/2)); //move normally
                            /*for(Pieces piece2 : aPieces){
                                if(this.isAllignedHorizontallyWith(piece2)){
                                    if(this.isLeftOf(piece2)){
                                        if(eventX<piece2.x-(width/2)){
                                            x = (int)(eventX-(width/2));
                                            continue;
                                        }else{
                                            x = piece2.x-width-1;
                                        }
                                    }
                                }
                            }*/
                            continue;
                        }else{
                            x = piece.right+1;
                        }
                    }
                    //check for and handle collisions while moving right
                    if(this.isLeftOf(piece)){
                        if(eventX<piece.x-(width/2)){
                            x = (int)(eventX-(width/2));
                            continue;
                        }else{
                            x = piece.x-width-1;
                        }
                    }
                    break;
                }else{
                    x = (int)(eventX-(width/2));
                }
            }
        }
        if(height>width){
            for (Pieces piece : aPieces) {
                //if object equals itself in array, skip to next object
                if(piece==this){
                    continue;
                }
                //check if there the possibility for a vertical collision
                if(this.isAllignedVerticallyWith(piece)){
                    //check for and handle collisions while moving up
                    if(this.isBelow(piece)){
                        if(eventY>piece.bottom+(height/2)){
                            y = (int)(eventY-(height/2)); //move normally
                            continue;
                        }else{
                            y = piece.bottom+1;
                        }
                    }
                    //check for and handle collisions while moving down
                    if(this.isAbove(piece)){
                        if(eventY<piece.y-(height/2)){
                            y = (int)(eventY-(height/2));
                            continue;
                        }else{
                            y = piece.y-height-1;
                        }
                    }
                    break;
                }else{
                    y = (int)(eventY-(height/2));
                }
            }
        }
        invalidate();
        break;

    case MotionEvent.ACTION_UP:
        // end move
        if(this.moves()){
        GameView.counter++;
        }
        initialX=x;
        initialY=y;
        break;
        }
    // parse puzzle
    invalidate();
    return true;
    }

最佳答案

在您的 Activity 类中,实现 OnPause()、OnResume() 方法。

在上述方法中使用日志语句来查看在应用程序关闭/打开过程中位置/坐标是否发生变化。

当调用“OnPause()”时,您可以保存各个组件的状态。当调用“OnResume()”时(即应用程序进入前台)读取保存的状态并使用您最近读取的坐标绘制 View 。

请注意,“OnCreate()”只会在创建 Activity 时调用一次。切换方向不会调用“OnCreate()”

您将找到解释 Android API 文档的流程图。 http://developer.android.com/reference/android/app/Activity.html

关于java - Activity 重新启动后功能发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5366664/

相关文章:

java - 用 HashMap 填充 ArrayList

java - Android 的 LinearLayout for Swing

android - 向 greenDAO 实体添加自定义代码

android - 如何自定义放置自动完成小部件对话框设计以列出位置

javascript - 如果有人单击 `draggable` div 中的输入,我该如何拖动 div?

php - JQuery - 拖放文件 - 如何获取文件信息?

css - HTML5拖放鬼图的透明背景

java - 变量在一个实例中未使用,但在另一实例中未使用

java - java.util.zip.CRC32 中的 CRC-32 实现

android - 无法构建 tess-two 库