gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:54:27 25 4
gpt4 key购买 nike

我有一个益智游戏,其中的棋子在屏幕上拖动但不能重叠。如果它们尝试重叠,它们的位置将更改回未重叠的位置,并使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com